Blame view

node_modules/we-cropper/docs/api.md 5.75 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  ## API
  
  ### 构造器参数
  
  #### id
  
  - Type: `String`
  - Default: `-`
  
  用于手势操作的canvas组件标识符(必填)
  
  #### targetId
  
  - Type: `String`
  - Default: ```-```
  
  用于生成截图的canvas组件标识符(必填)
  
  #### pixelRatio
  
  - Type: `Number`
  - Default: ```wx.getSystemInfoSync().pixelRatio```
  
  设备像素比 (必填)
  
  #### width
  
  - Type: `Number`
  - Default: `-`
  
  容器宽度,单位:px
  
  #### height
  
  - Type: `Number`
  - Default: `-`
  
  容器高度,单位:px
  
  #### scale
  
  - Type: `Number`
  - Default: `2.5`
  
  最大缩放倍数
  
  #### src
  
  - Type: `String`
  - Default: `-`
  
  需裁剪的图片资源
  
  #### zoom
  
  - Type: `Number`
  - Default: `5`
  - Range: 1 ~ 10
  
  缩放系数
  
  #### cut
  
  - Type: `Object`
  - Options: 
      - x: `Number` 裁剪框x轴起点(默认 0)
      - y: `Number` 裁剪框y轴起点(默认 0)
      - width: `Number` 裁剪框宽度(默认 画布宽度),单位:px
      - height: `Number` 裁剪框高度(默认 画布高度),单位:px
  
  #### boundStyle
  
  - Type: `Object`
  - Options: 
      - color: `String` 裁剪框颜色(默认 ```#04b00f```
      - lineWidth: `Number` 裁剪框线条宽度(默认 1)
      - mask: `String` 遮罩层颜色(默认 ```rgba(0, 0, 0, 0.3)```
  
  #### onReady
  
  - Type: `Function`
  - Params:
      - `ctx`: 返回当前画布的上下文
      - `instance`: 返回weCropper实例
  - Example: 参考示例1-1
  
  钩子函数:weCropper实例化后会立即执行
  
  #### onLoad
  
  - Type: `Function`
  - Params:
      - `ctx`: 返回当前画布的上下文
      - `instance`: 返回weCropper实例
  - Example: 参考示例1-1
  
  钩子函数:图片加载完毕后会立即执行
  
  #### onBeforeDraw
  
  - Type: `Function`
  - Params:
      - `ctx`: 返回当前画布的上下文
      - `instance`: 返回weCropper实例
  - Example: 参考示例1-1
  
  钩子函数:在画布绘制之前执行
  
  > 示例 1-1
  
  ```javascript
      new weCropper({
        id: 'cropper',
        onReady (ctx) {
        	console.log(`weCropper is ready`)
        	console.log(`current canvas context:`, ctx)
        	wx.showToast({
        		title: '上传中',
              icon: 'loading',
              duration: 20000
        	})
        },
        onLoad (ctx) {
          console.log(`picture loaded`)
          console.log(`current canvas context:`, ctx)
          wx.hideToast()
        },
        onBeforeDraw (ctx) {
          console.log(`before canvas draw,i can do something`)
          console.log(`current canvas context:`, ctx)
        	//  那就尝试在图片上加个水印吧
          ctx.setFontSize(14)
          ctx.setFillStyle('#ffffff')
          ctx.fillText('@we-cropper', 265, 350)
        }
      })
  
  ```
  
  
  
  ### 方法
  
  #### wecropper.updateCanvas()
  
  更新画布视图
  
  #### wecropper.pushOrign(src)
  
  - src:
      - Type: `String`
  
  载入图片
  
  #### wecropper.getCropperImage(opt,callback)
  
  - opt: (可选参数)  继承了 ```wx.canvasToTempFilePath``` 参数
      - Type: `Object`
      - Options:
          - original `Boolean` 是否使用原图模式(默认值 `false`) `v1.3.3支持` 
          - quality `Number` 图片的质量,目前仅对 jpg 有效。取值范围为 (0, 1],不在范围内时当作 1.0 处理。`v1.3.3支持` 
          - fileType `String` 目标文件的类型  `v1.3.3支持` 
          - componentContext `Object` 在自定义组件下,当前组件实例的this,以操作组件内 <canvas> 组件 `v1.3.4支持`
  
  - callback:
      - Type: `Function`
      - Params:
          - `src`: 裁剪后的图片路径
          - `err`: 异常信息
  
  - return:
      - Type: `Promise`
          
  获取画布图片
  
  > 示例 2-1
  
  ```javascript
  // 通过 then 链式调用
  this.wecropper.getCropperImage()
    .then((src) => {
      wx.previewImage({
        current: '', // 当前显示图片的http链接
        urls: [src] // 需要预览的图片http链接列表
      })
    })
    .catch(() => {
      console.log('获取图片地址失败,请稍后重试')
    })
  
  // 亦可通过回调函数
  this.wecropper.getCropperImage(tempFilePath => {
    if (tempFilePath) {
      wx.previewImage({
        current: '', // 当前显示图片的http链接
        urls: [src] // 需要预览的图片http链接列表
      })
    } else {
      console.log('获取图片地址失败,请稍后重试')
    }
  })
  ```
  
  #### wecropper.getCropperBase64(callback)
  
  !> 基础库 1.9.0 开始支持,低版本需做兼容处理 
  
  - callback:
      - Type: `Function`
      - Params:
          - `base64`: 裁剪后的图片(base64编码)
          - `err`: 异常信息
          
  获取裁剪图片(base64编码)
          
  #### wecropper.on(event, callback)
  
  - event:
      - Type: `String`
      - Range: `ready、beforeImageLoad、imageLoad、beforeDraw`
      
  - callback:
      - Type: `Function`
      - Params:
          - `ctx`: 返回当前画布的上下文
      - Example: 参考示例2-1
      
  事件监听
  
  #### wecropper.touchStart(e)
  
  - e:
      - Type: `Object`
  
  接收(手指触摸动作开始)事件对象
  
  #### wecropper.touchMove(e)
  
  - e:
      - Type: `Object`
  
  接收(手指触摸后移动)事件对象
  
  #### wecropper.touchEnd(e)
  
  - e:
      - Type: `Object`
  
  接收(手指触摸动作结束)事件对象
  
  
  > 示例 3-1
  
  ```javascript
      new weCropper({
        id: 'cropper'
      })
      .on('ready', (ctx) => {
        console.log(`weCropper is ready`)
        console.log(`current canvas context:`, ctx)
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('load', (ctx) => {
        console.log(`picture loaded`)
        console.log(`current canvas context:`, ctx)
        wx.hideToast()
      })
      .on('beforeDraw', (ctx) => {
        console.log(`before canvas draw,i can do something`)
        console.log(`current canvas context:`, ctx)
        //  那就尝试在图片上加个水印吧
        ctx.setFontSize(14)
        ctx.setFillStyle('#ffffff')
        ctx.fillText('@we-cropper', 265, 350)
      })
  
  ```