UNPKG

11 kBMarkdownView Raw
1# vueShapeImg
2
3> Picture clipping component made with vue
4
5[If you like, please give me a star (๑•̀ㅂ•́)و✧](https://github.com/ArchieHurry/vueShapeImg)
6
7[如果你喜欢它,可以给我星星吗 (๑•̀ㅂ•́)و✧](https://github.com/ArchieHurry/vueShapeImg)
8
9![Demonstration](http://www.bqmyweb.cn/vueshapeimg/1.3.1.gif)
10
11## <a name="english">English</a> <a href="#中文文档">中文文档</a>
12
13## [OnlineDemo](http://www.bqmyweb.cn/vueshapeimg/)
14
15### How to use
16
17``` bash
18import vueShapeImg from 'vueshapeimg'
19Vue.use(vueShapeImg)
20
21<vue-shape-img></vue-shape-img>
22
23```
24
25### vueShapeImg API Doc
26
27#### props
28
29| Parameters | Description | Type | Optional values | Default value |
30|---------- |-------- |---------- |---------- |---------- |
31|useFrame|Clipping pictures using marquee boxes| Boolean | Y | false |
32|canResizeFrame|Whether the box can be scaled and moved| Boolean | Y | true |
33|initRange|Left,Top,Width and Height of box initialization| Array | Y | [width * 0.25, height * 0.25, width * 0.5 ,height * 0.5] |
34|height |- |Number |Y |500 |
35|width |- |Number |Y |500 |
36|timelyGetRange |Timely get user's select range |Boolean |Y |false |
37|timelyImageData |Timely converting pictures that capture the user's range of choice into imageData |Boolean |Y |false |
38
39##### tips: You can't use imageData directly,show it in canvas putImageData
40
41#### methods
42
43| Method name | Description | Parameters |
44|---------- |-------- |---------- |
45|rotate|Rotate the picture on canvas|deg(Arbitrary integer) |
46|setImgSrc|Use network picture. Pictures with different domain names are best not to be used |imgSrc(Picture links or Base64 files)|
47|fileClick|Use local picture|-|
48|showMask|Open the mask layer, select the area|-|
49|closeMask|Close the mask layer|-|
50|getRange|Get the scope of user selection|-|
51|setRange|Set the scope of user selection| [left,top,width,height] |
52|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) |
53
54##### tips: getImg -> imgType -> 'image/jpeg' can compress pictures better.
55
56#### emit
57
58| emit name | Description
59|---------- |-------- |
60|imageDataChange|if props timelyImageData is true, this emit can return the imageData in timely|
61|rangeChange|if props timelyGetRange is true, this emit can return the range in timely|
62|error|error tips: -1(Picture format error), -2(Picture loading failed), -3(Pictures are cross-domain resources) |
63
64##### tips: You can't use imageData directly,show it in canvas putImageData
65
66### Example [OnlineDemo](http://www.bqmyweb.cn/vueshapeimg/)
67<pre><code>
68&lt;template&gt;
69 &lt;div id=&quot;app&quot;&gt;
70 &lt;div style=&quot;width: 500px;display: inline-block;float: left&quot;&gt;
71 &lt;button @click=&quot;$refs.vueShapeImg2.fileClick()&quot;&gt;localImg&lt;/button&gt;
72 &lt;button @click=&quot;$refs.vueShapeImg2.setImgSrc(&#x27;http://www.bqmyweb.cn/vueshapeimg/demo.png&#x27;)&quot;&gt;networkImg&lt;/button&gt;
73 &lt;button @click=&quot;$refs.vueShapeImg2.showMask()&quot;&gt;startCrop&lt;/button&gt;
74 &lt;button @click=&quot;$refs.vueShapeImg2.closeMask()&quot;&gt;endCrop&lt;/button&gt;
75 &lt;button @click=&quot;getImg2&quot;&gt;getImg&lt;/button&gt;
76 &lt;button @click=&quot;$refs.vueShapeImg2.setRange([200,200,200,200])&quot;&gt;setRange&lt;/button&gt;
77 &lt;button @click=&quot;$refs.vueShapeImg2.rotate(10)&quot;&gt;rotate10&lt;/button&gt;
78 &lt;button @click=&quot;$refs.vueShapeImg2.rotate(-10)&quot;&gt;rotate-10&lt;/button&gt;
79 &lt;p style=&quot;font-size: 18px;font-weight: bold;&quot;&gt;useFrame:true&lt;/p&gt;
80 &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;
81 &lt;canvas style=&quot;margin-left: 50px&quot; id=&quot;canvas2&quot;&gt;&lt;/canvas&gt;
82 &lt;/div&gt;
83 &lt;div style=&quot;clear: both&quot;&gt;&lt;/div&gt;
84 &lt;/div&gt;
85&lt;/template&gt;
86
87&lt;script&gt;
88export default {
89 name: &#x27;app&#x27;,
90 data () {
91 return {
92 canvas2: null,
93 canvas2Ctx: null
94 }
95 },
96 mounted () {
97 this.canvas2 = document.getElementById(&#x27;canvas2&#x27;)
98 this.canvas2Ctx = this.canvas2.getContext(&#x27;2d&#x27;)
99 },
100 methods: {
101 putImg2 (imgData) {
102 this.canvas2Ctx.clearRect(0, 0, 500, 500)
103 let obj = this.$refs.vueShapeImg2.getRange()
104 this.canvas2.width = obj.w
105 this.canvas2.height = obj.h
106 this.canvas2Ctx.putImageData(imgData, 0, 0)
107 },
108 getImg2 () {
109 console.log(this.$refs.vueShapeImg2.getImg(&#x27;base64&#x27;, &#x27;image/jpeg&#x27;, 0.7))
110 },
111 imgError (error) {
112 console.error(error)
113 },
114 }
115}
116&lt;/script&gt;
117</code></pre>
118
119
120### Browser support
121
122##### IE10+, chrome, firefox or other modern Browser
123
124
125## <a name="中文文档">中文文档</a> <a href="#english">English</a>
126
127> 使用vue编写的图片裁剪组件
128
129## [在线例子](http://www.bqmyweb.cn/vueshapeimg/)
130
131### 怎样使用
132
133``` bash
134import vueShapeImg from 'vueshapeimg'
135Vue.use(vueShapeImg)
136
137<vue-shape-img></vue-shape-img>
138
139```
140
141### 接口文档
142
143#### props
144
145| 名称 | 描述 | 类型 | 可选参数 | 默认值 |
146|---------- |-------- |---------- |---------- |---------- |
147|useFrame|是否使用选框裁剪图片| Boolean | Y | false |
148|canResizeFrame|选框是否可以缩放| Boolean | Y | true |
149|initRange|数组,依次为左边距,上边距,宽,高| Array | Y | [width * 0.25, height * 0.25, width * 0.5 ,height * 0.5] |
150|height |- |Number |Y |500 |
151|width |- |Number |Y |500 |
152|timelyGetRange |是否及时的获取用户选择的区域 |Boolean |Y |false |
153|timelyImageData |是否及时的返回用户选择的图像区域|Boolean |Y |false |
154
155##### 注意: 用户选择的图像区域不能直接使用,使用canvas的pitImageData可以展示
156
157#### methods
158
159|方法名 | 描述 | 参数 |
160|---------- |-------- |---------- |
161|rotate|旋转canvas上的图片|任意整数|
162|setImgSrc|如果使用的是网络图片,那么不同域名的图片最好不要使用 |imgSrc(任意可以被img标签加载的资源)|
163|fileClick|使用本地图片|-|
164|showMask|展开遮罩层,用户选择区域|-|
165|closeMask|关闭遮罩层|-|
166|getRange|获取用户选择的区域|-|
167|setRange|设置用户选框的大小| [left,top,width,height] |
168|getImg|获取用户选择的区域|type('base64'/'blob'), imgType('image/png' 或者其他任意的web可支持的图片格式),encoderOptions(图片质量默认0.92,可选0.00 - 1.00) |
169
170##### tips: getImg -> imgType -> 'image/jpeg' 可以更好的压缩图片
171
172#### emit
173
174| emit name | Description
175|---------- |-------- |
176|imageDataChange|如果timelyImageData为true,那么会及时返回用户选择区域的图像,图像是imageData,img不能直接展示|
177|rangeChange|如果timelyGetRange为true,那么会及时返回用户选择的区域|
178|error|错误提示: -1(图片格式错误), -2(图片加载失败), -3(图片为跨域资源) |
179
180##### tips: imageDataChange返回的值可以通过 canvas的putImageData展示
181
182### 举例 [在线例子](http://www.bqmyweb.cn/vueshapeimg/)
183 <pre><code>
184 &lt;template&gt;
185 &lt;div id=&quot;app&quot;&gt;
186 &lt;div style=&quot;width: 500px;display: inline-block;float: left&quot;&gt;
187 &lt;button @click=&quot;$refs.vueShapeImg2.fileClick()&quot;&gt;localImg&lt;/button&gt;
188 &lt;button @click=&quot;$refs.vueShapeImg2.setImgSrc(&#x27;http://www.bqmyweb.cn/vueshapeimg/demo.png&#x27;)&quot;&gt;networkImg&lt;/button&gt;
189 &lt;button @click=&quot;$refs.vueShapeImg2.showMask()&quot;&gt;startCrop&lt;/button&gt;
190 &lt;button @click=&quot;$refs.vueShapeImg2.closeMask()&quot;&gt;endCrop&lt;/button&gt;
191 &lt;button @click=&quot;getImg2&quot;&gt;getImg&lt;/button&gt;
192 &lt;button @click=&quot;$refs.vueShapeImg2.setRange([200,200,200,200])&quot;&gt;setRange&lt;/button&gt;
193 &lt;button @click=&quot;$refs.vueShapeImg2.rotate(10)&quot;&gt;rotate10&lt;/button&gt;
194 &lt;button @click=&quot;$refs.vueShapeImg2.rotate(-10)&quot;&gt;rotate-10&lt;/button&gt;
195 &lt;p style=&quot;font-size: 18px;font-weight: bold;&quot;&gt;useFrame:true&lt;/p&gt;
196 &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;
197 &lt;canvas style=&quot;margin-left: 50px&quot; id=&quot;canvas2&quot;&gt;&lt;/canvas&gt;
198 &lt;/div&gt;
199 &lt;div style=&quot;clear: both&quot;&gt;&lt;/div&gt;
200 &lt;/div&gt;
201 &lt;/template&gt;
202
203 &lt;script&gt;
204 export default {
205 name: &#x27;app&#x27;,
206 data () {
207 return {
208 canvas2: null,
209 canvas2Ctx: null
210 }
211 },
212 mounted () {
213 this.canvas2 = document.getElementById(&#x27;canvas2&#x27;)
214 this.canvas2Ctx = this.canvas2.getContext(&#x27;2d&#x27;)
215 },
216 methods: {
217 putImg2 (imgData) {
218 this.canvas2Ctx.clearRect(0, 0, 500, 500)
219 let obj = this.$refs.vueShapeImg2.getRange()
220 this.canvas2.width = obj.w
221 this.canvas2.height = obj.h
222 this.canvas2Ctx.putImageData(imgData, 0, 0)
223 },
224 getImg2 () {
225 console.log(this.$refs.vueShapeImg2.getImg(&#x27;base64&#x27;, &#x27;image/jpeg&#x27;, 0.7))
226 },
227 imgError (error) {
228 console.error(error)
229 },
230 }
231 }
232 &lt;/script&gt;
233
234</code></pre>
235
236
237### 浏览器支持情况
238
239##### IE10+, chrome, firefox 和其他现代浏览器
240
241### Upgraded content(升级的内容)
242
243#### 1.3.4(2019-5-15 15:17:51)
244
245- Limit the range of input parameters of setRange function
246
247限制了setRange 函数的输入参数范围
248
249#### 1.3.2(2019-5-15 11:23:55)
250
251- Fixed the problem of calling the rotate function picture to return to the center of the canvas after moving the picture in frame mode
252
253修复框架模式下图片移动后调用旋转函数图片返回画布中心的问题
254
255#### 1.3.1(2019-5-15 10:47:35)
256
257- Adding image rotate function
258
259添加了图片旋转功能
260
261#### 1.3.0(2019-5-13 17:25:10)
262
263- Fixed the problem of onmouseup pollute caused by using multiple vueShapeImg on the same page and clipping box always exist
264
265修复在同一页面中使用多个vueShapeImg导致onmouseup污染,裁剪框一直存在的问题
266
267- Fixed cross-domain error multiple times triggered when using cross-domain images for cropping in normal mode
268
269修复在普通模式下使用跨域图片进行裁剪时多次报错的问题
270
271#### 1.2.9(2019-5-13 17:05:41)
272
273- Fixed the problem of using timelyGetRange to report cross-domain errors in normal mode
274
275修复了普通模式下使用timelyGetRange报跨域错误的问题
276
277[more version info(更多版本信息)](https://github.com/ArchieHurry/vueShapeImg/blob/master/version.md)