UNPKG

6.01 kBMarkdownView Raw
1# vueShapeImg
2
3[![install size](https://packagephobia.now.sh/badge?p=vueshapeimg)](https://packagephobia.now.sh/result?p=vueshapeimg)
4[![Codacy Badge](https://api.codacy.com/project/badge/Grade/e05ad3648e3a437ca43a9d166c6b5bfa)](https://www.codacy.com/app/ArchieHurry/vueShapeImg)
5
6> Picture clipping component made with vue. vue编写的图片裁剪组件
7
8[If you like, please give me a star (๑•̀ㅂ•́)و✧](https://github.com/ArchieHurry/vueShapeImg)
9
10[如果你喜欢它,可以给我星星吗 (๑•̀ㅂ•́)و✧](https://github.com/ArchieHurry/vueShapeImg)
11
12![Demonstration](http://www.bqmyweb.cn/vueshapeimg/1.3.1.gif)
13
14## English <a href="https://github.com/ArchieHurry/vueShapeImg/blob/master/README_ZH.md">中文文档</a>
15
16## [OnlineDemo](http://www.bqmyweb.cn/vueshapeimg/)
17
18### How to use
19
20``` bash
21import vueShapeImg from 'vueshapeimg'
22Vue.use(vueShapeImg)
23
24<vue-shape-img></vue-shape-img>
25
26```
27
28### vueShapeImg API Doc
29
30#### props
31
32|Parameters|Description|Type|Optional values|Default value|
33|----------|--------|----------|----------|----------|
34|useFrame|Clipping pictures using marquee boxes|Boolean|Y|false|
35|canResizeFrame|Whether the box can be scaled and moved|Boolean|Y|true|
36|initRange|Left,Top,Width and Height of box initialization|Array|Y|\[width\*0.25,height\*0.25,width\*0.5,height\*0.5\]|
37|height|-|Number|Y|500|
38|width|-|Number|Y|500|
39|timelyGetRange|Timely get user's select range|Boolean|Y|false|
40|timelyImageData|Timely converting pictures that capture the user's range of choice into imageData|Boolean|Y|false|
41
42##### tips: You can't use imageData directly,show it in canvas putImageData
43
44#### methods
45
46|Method name|Description|Parameters|
47|----------|--------|----------|
48|rotate|Rotate the picture on canvas|deg(Arbitrary integer)|
49|setImgSrc|Use network picture. Pictures with different domain names are best not to be used|imgSrc(Picture links or Base64 files)|
50|fileClick|Use local picture|-|
51|showMask|Open the mask layer, select the area|-|
52|closeMask|Close the mask layer|-|
53|getRange|Get the scope of user selection|-|
54|setRange|Set the scope of user selection|\[left,top,width,height\]|
55|getImg|Get pictures of the range selected by the user|type('base64'/'blob'), imgType('image/jpeg' Or other image types),encoderOptions(0.00 - 1.00)|
56
57##### tips: getImg -> imgType -> 'image/jpeg' can compress pictures better
58
59#### emit
60
61|emit name|Description
62|----------|--------|
63|imageDataChange|if props timelyImageData is true, this emit can return the imageData in timely|
64|rangeChange|if props timelyGetRange is true, this emit can return the range in timely|
65|error|error tips: -1(Picture format error), -2(Picture loading failed), -3(Pictures are cross-domain resources)|
66
67##### tips: You can not use imageData directly,show it in canvas putImageData
68
69### Example [OnlineDemo](http://www.bqmyweb.cn/vueshapeimg/)
70
71<pre><code>
72 &lt;template&gt;
73 &lt;div id=&quot;app&quot;&gt;
74 &lt;div style=&quot;width: 500px;&quot;&gt;
75 &lt;button @click=&quot;$refs.vueShapeImg2.fileClick()&quot;&gt;localImg&lt;/button&gt;
76 &lt;button @click=&quot;$refs.vueShapeImg2.setImgSrc(&#x27;http://www.bqmyweb.cn/vueshapeimg/demo.png&#x27;)&quot;&gt;networkImg&lt;/button&gt;
77 &lt;button @click=&quot;$refs.vueShapeImg2.showMask()&quot;&gt;startCrop&lt;/button&gt;
78 &lt;button @click=&quot;$refs.vueShapeImg2.closeMask()&quot;&gt;endCrop&lt;/button&gt;
79 &lt;button @click=&quot;getImg2&quot;&gt;getImg&lt;/button&gt;
80 &lt;button @click=&quot;$refs.vueShapeImg2.setRange([200,200,200,200])&quot;&gt;setRange&lt;/button&gt;
81 &lt;button @click=&quot;$refs.vueShapeImg2.rotate(10)&quot;&gt;rotate10&lt;/button&gt;
82 &lt;button @click=&quot;$refs.vueShapeImg2.rotate(-10)&quot;&gt;rotate-10&lt;/button&gt;
83 &lt;p style=&quot;font-size: 18px;font-weight: bold;&quot;&gt;useFrame:true&lt;/p&gt;
84 &lt;vueShapeImg @error=&quot;imgError&quot; :height=&quot;400&quot; :width=&quot;400&quot; :useFrame=&quot;true&quot; :timelyImageData=&quot;true&quot; @imageDataChange=&quot;putImg2&quot; ref=&quot;vueShapeImg2&quot;&gt;&lt;/vueShapeImg&gt;
85 &lt;canvas id=&quot;canvas2&quot;&gt;&lt;/canvas&gt;
86 &lt;/div&gt;
87 &lt;/div&gt;
88 &lt;/template&gt;
89
90 &lt;script&gt;
91 export default {
92 name: &#x27;app&#x27;,
93 data () {
94 return {
95 canvas2: null,
96 canvas2Ctx: null
97 }
98 },
99 mounted () {
100 this.canvas2 = document.getElementById(&#x27;canvas2&#x27;)
101 this.canvas2Ctx = this.canvas2.getContext(&#x27;2d&#x27;)
102 },
103 methods: {
104 putImg2 (imgData) {
105 this.canvas2Ctx.clearRect(0, 0, 500, 500)
106 let obj = this.$refs.vueShapeImg2.getRange()
107 this.canvas2.width = obj.w
108 this.canvas2.height = obj.h
109 this.canvas2Ctx.putImageData(imgData, 0, 0)
110 },
111 getImg2 () {
112 console.log(this.$refs.vueShapeImg2.getImg(&#x27;base64&#x27;, &#x27;image/jpeg&#x27;, 0.7))
113 },
114 imgError (error) {
115 console.error(error)
116 },
117 }
118 }
119 &lt;/script&gt;
120</code></pre>
121
122### Browser support
123
124#### IE10+, chrome, firefox or other modern Browser
125
126### Upgraded content
127
128#### 1.4.1 (2019-5-24 09:41:00)
129
130Fixed the problem of dragging the whole canvas when the mouseup event was triggered on the clipping box when moving the picture in the frame mode in IE browser.
131
132#### 1.3.4 (2019-5-15 15:17:51)
133
134Limit the range of input parameters of setRange function
135
136#### 1.3.2 (2019-5-15 11:23:55)
137
138Fixed the problem of calling the rotate function picture to return to the center of the canvas after moving the picture in frame mode
139
140#### 1.3.1 (2019-5-15 10:47:35)
141
142Adding image rotate function
143
144#### 1.3.0 (2019-5-13 17:25:10)
145
146Fixed the problem of onmouseup pollute caused by using multiple vueShapeImg on the same page and clipping box always exist
147
148Fixed cross-domain error multiple times triggered when using cross-domain images for cropping in normal mode
149
150[more version info](https://github.com/ArchieHurry/vueShapeImg/blob/master/version.md)