sadais-piui 1.2.4-beta2 → 1.2.4-beta5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +512 -512
- package/LICENSE +20 -20
- package/README.md +166 -166
- package/components/pi-action-sheet/index.vue +247 -247
- package/components/pi-badge/index.vue +136 -136
- package/components/pi-bottom-bar/index.vue +139 -139
- package/components/pi-button/index.vue +492 -492
- package/components/pi-calendar/index.vue +178 -178
- package/components/pi-calendar-panel/index.vue +436 -432
- package/components/pi-canvas/index.vue +400 -400
- package/components/pi-card/index.vue +127 -127
- package/components/pi-checkbox/index.vue +345 -345
- package/components/pi-checkbox-group/index.vue +215 -215
- package/components/pi-circle-progress/index.vue +224 -224
- package/components/pi-code-input/index.vue +262 -262
- package/components/pi-count-down/index.vue +245 -245
- package/components/pi-count-up/index.vue +209 -209
- package/components/pi-date-picker/index.vue +366 -366
- package/components/pi-divider/index.vue +109 -109
- package/components/pi-dropdown/components/dropdown-tab.vue +62 -62
- package/components/pi-dropdown/index.vue +316 -316
- package/components/pi-dropdown-item/index.vue +233 -233
- package/components/pi-form/index.vue +237 -237
- package/components/pi-form-item/index.vue +534 -534
- package/components/pi-gap/index.vue +72 -72
- package/components/pi-grid/index.vue +92 -92
- package/components/pi-grid-item/index.vue +220 -220
- package/components/pi-icon/index.vue +147 -147
- package/components/pi-img/index.vue +267 -267
- package/components/pi-img-cropper/canvas.js +112 -112
- package/components/pi-img-cropper/index.vue +357 -357
- package/components/pi-img-cropper/touch.js +163 -163
- package/components/pi-img-cropper/vector.js +34 -34
- package/components/pi-input/index.vue +299 -299
- package/components/pi-line/index.vue +78 -78
- package/components/pi-line-progress/index.vue +198 -198
- package/components/pi-list/index.vue +115 -115
- package/components/pi-list-item/index.vue +331 -331
- package/components/pi-loading/index.vue +188 -188
- package/components/pi-marquee/index.vue +281 -281
- package/components/pi-marquee-item/index.vue +47 -47
- package/components/pi-mask/index.vue +198 -198
- package/components/pi-modal/index.vue +303 -303
- package/components/pi-navbar/index.vue +421 -421
- package/components/pi-notify/index.vue +239 -239
- package/components/pi-number-keyboard/index.vue +189 -189
- package/components/pi-opentype-button/index.vue +501 -501
- package/components/pi-pagination/index.vue +277 -277
- package/components/pi-picker/index.vue +317 -317
- package/components/pi-popup/index.vue +353 -353
- package/components/pi-popup-select/index.vue +291 -291
- package/components/pi-radio/index.vue +387 -387
- package/components/pi-radio-group/index.vue +262 -262
- package/components/pi-rate/index.vue +212 -212
- package/components/pi-regions-select/index.vue +318 -318
- package/components/pi-search/index.vue +351 -351
- package/components/pi-section/index.vue +218 -218
- package/components/pi-select/index.vue +391 -391
- package/components/pi-skeleton/index.vue +280 -280
- package/components/pi-status-bar/index.vue +56 -56
- package/components/pi-stepper/index.vue +394 -394
- package/components/pi-steps/index.vue +299 -299
- package/components/pi-swiper/index.vue +344 -344
- package/components/pi-switch/index.vue +227 -227
- package/components/pi-tabs/index.vue +398 -398
- package/components/pi-upload-img/index.vue +508 -506
- package/components/pi-waterfall/index.vue +130 -130
- package/components/pi-webview/index.vue +82 -82
- package/config/actionSheet.js +25 -25
- package/config/badge.js +9 -9
- package/config/bottomBar.js +11 -11
- package/config/button.js +29 -29
- package/config/calendar.js +19 -19
- package/config/calendarPanel.js +24 -24
- package/config/card.js +8 -8
- package/config/checkbox.js +14 -14
- package/config/checkboxGroup.js +14 -14
- package/config/circleProgress.js +13 -13
- package/config/codeInput.js +12 -12
- package/config/countDown.js +12 -12
- package/config/countUp.js +13 -13
- package/config/datePicker.js +22 -22
- package/config/divider.js +10 -10
- package/config/dropdown.js +21 -21
- package/config/dropdownItem.js +26 -26
- package/config/form.js +27 -27
- package/config/formItem.js +25 -25
- package/config/gap.js +8 -8
- package/config/grid.js +9 -9
- package/config/gridItem.js +12 -12
- package/config/icon.js +11 -11
- package/config/img.js +21 -21
- package/config/imgCropper.js +26 -26
- package/config/index.js +28 -28
- package/config/input.js +25 -25
- package/config/line.js +8 -8
- package/config/lineProgress.js +14 -14
- package/config/list.js +12 -12
- package/config/listItem.js +25 -25
- package/config/loading.js +10 -10
- package/config/marquee.js +8 -8
- package/config/mask.js +12 -12
- package/config/modal.js +36 -36
- package/config/navbar.js +32 -32
- package/config/notify.js +13 -13
- package/config/numberKeyboard.js +21 -21
- package/config/pagination.js +10 -10
- package/config/picker.js +19 -19
- package/config/popup.js +23 -23
- package/config/popupSelect.js +35 -35
- package/config/radio.js +15 -15
- package/config/radioGroup.js +16 -16
- package/config/rate.js +15 -15
- package/config/regionsData.js +58 -58
- package/config/regionsSelect.js +18 -18
- package/config/search.js +27 -27
- package/config/section.js +23 -23
- package/config/select.js +28 -28
- package/config/skeleton.js +13 -13
- package/config/statusBar.js +5 -5
- package/config/stepper.js +19 -19
- package/config/steps.js +19 -19
- package/config/swiper.js +22 -22
- package/config/switch.js +13 -13
- package/config/tabs.js +23 -23
- package/config/uploadImg.js +63 -63
- package/config/waterfall.js +6 -6
- package/index.js +41 -41
- package/mixin/component-custom.js +24 -24
- package/mixin/emitter.js +45 -45
- package/mixin/props-sync.js +65 -65
- package/mixin/value-sync.js +32 -32
- package/mixin/wx-code.js +30 -30
- package/package.json +25 -25
- package/plugins/async-validator.js +1511 -1511
- package/scss/animation.scss +260 -260
- package/scss/icon/icon.scss +594 -594
- package/scss/index.scss +5 -5
- package/scss/mixin.scss +47 -47
- package/tools/index.js +11 -11
|
@@ -1,400 +1,400 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<view ref="root" :class="[customClass, 'wrapper']" :style="[customStyle]">
|
|
3
|
-
<canvas
|
|
4
|
-
ref="canvas"
|
|
5
|
-
:style="[canvasStyle]"
|
|
6
|
-
type="2d"
|
|
7
|
-
class="content"
|
|
8
|
-
:canvas-id="canvasId"
|
|
9
|
-
:width="canvasw"
|
|
10
|
-
:height="canvash"
|
|
11
|
-
@touchstart="start"
|
|
12
|
-
@touchmove="move"
|
|
13
|
-
@touchcancel="end"
|
|
14
|
-
@touchend="end"
|
|
15
|
-
@mousedown="start"
|
|
16
|
-
@mousemove="move"
|
|
17
|
-
@mouseup="end"
|
|
18
|
-
/>
|
|
19
|
-
<canvas
|
|
20
|
-
ref="cropCanvas"
|
|
21
|
-
type="2d"
|
|
22
|
-
class="copy"
|
|
23
|
-
:style="[cropStyle]"
|
|
24
|
-
:canvas-id="cropId"
|
|
25
|
-
:width="cropw"
|
|
26
|
-
:height="croph"
|
|
27
|
-
/>
|
|
28
|
-
</view>
|
|
29
|
-
</template>
|
|
30
|
-
<script>
|
|
31
|
-
export default {
|
|
32
|
-
name: 'PiCanvas',
|
|
33
|
-
props: {
|
|
34
|
-
// 自定义class
|
|
35
|
-
customClass: {
|
|
36
|
-
type: String,
|
|
37
|
-
// `''`
|
|
38
|
-
default: ''
|
|
39
|
-
},
|
|
40
|
-
// 自定义style
|
|
41
|
-
customStyle: {
|
|
42
|
-
type: Object,
|
|
43
|
-
// `{}`
|
|
44
|
-
default: () => ({})
|
|
45
|
-
},
|
|
46
|
-
// 是否最小化空白区域
|
|
47
|
-
minimal: {
|
|
48
|
-
type: Boolean,
|
|
49
|
-
// `false`
|
|
50
|
-
default: false
|
|
51
|
-
},
|
|
52
|
-
// 当minimal:true时,指定pandding
|
|
53
|
-
padding: {
|
|
54
|
-
type: Object,
|
|
55
|
-
// `{ top: 8, right: 8, bottom: 8, left: 8 }`
|
|
56
|
-
default: () => ({ top: 8, right: 8, bottom: 8, left: 8 })
|
|
57
|
-
},
|
|
58
|
-
// 画布背景色
|
|
59
|
-
bgColor: {
|
|
60
|
-
type: String,
|
|
61
|
-
// `#ffffff`
|
|
62
|
-
default: '#ffffff'
|
|
63
|
-
},
|
|
64
|
-
// 画笔颜色
|
|
65
|
-
color: {
|
|
66
|
-
type: String,
|
|
67
|
-
// `#333333`
|
|
68
|
-
default: '#333333'
|
|
69
|
-
},
|
|
70
|
-
// 线条宽度
|
|
71
|
-
lineWidth: {
|
|
72
|
-
type: Number,
|
|
73
|
-
// `1`
|
|
74
|
-
default: 1
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
data() {
|
|
78
|
-
return {
|
|
79
|
-
isBegin: false, // 标记是否先触发了touchstart or mousedown
|
|
80
|
-
canvasId: '', // 内容画布的ID
|
|
81
|
-
cropId: '', // 裁剪画布的ID
|
|
82
|
-
ctx: null, // 内容画布的上下文
|
|
83
|
-
canvasw: 0, // 内容画布的宽度
|
|
84
|
-
canvash: 0, // 内容画布的高度
|
|
85
|
-
cropw: 0, // 裁剪画布的宽度
|
|
86
|
-
croph: 0 // 裁剪画布的高度
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
computed: {
|
|
90
|
-
isUni() {
|
|
91
|
-
return window.uni != null
|
|
92
|
-
},
|
|
93
|
-
canvasStyle() {
|
|
94
|
-
return {
|
|
95
|
-
width: `${this.canvasw}px`,
|
|
96
|
-
height: `${this.canvash}px`
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
cropStyle() {
|
|
100
|
-
return {
|
|
101
|
-
width: `${this.cropw}px`,
|
|
102
|
-
height: `${this.croph}px`
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
},
|
|
106
|
-
created() {
|
|
107
|
-
this.$nextTick(() => {
|
|
108
|
-
this.initCanvas()
|
|
109
|
-
})
|
|
110
|
-
},
|
|
111
|
-
methods: {
|
|
112
|
-
generateId() {
|
|
113
|
-
// 产生16位随机的字母、数字混合的随机ID
|
|
114
|
-
const tmps = '0123456789'
|
|
115
|
-
.split('')
|
|
116
|
-
.concat('abcdefghijklmnopqrstuvwxyz'.split(''))
|
|
117
|
-
.concat('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''))
|
|
118
|
-
let n = tmps.length
|
|
119
|
-
while (n--) {
|
|
120
|
-
const idx = Math.floor(Math.random() * n)
|
|
121
|
-
const tmp = tmps[idx]
|
|
122
|
-
tmps[idx] = tmps[n]
|
|
123
|
-
tmps[n] = tmp
|
|
124
|
-
}
|
|
125
|
-
return tmps.join('').substr(0, 16)
|
|
126
|
-
},
|
|
127
|
-
initCanvas() {
|
|
128
|
-
this.canvasId = this.generateId()
|
|
129
|
-
this.cropId = this.generateId()
|
|
130
|
-
if (!this.isUni) {
|
|
131
|
-
// 非uni环境
|
|
132
|
-
const root = this.$refs.root
|
|
133
|
-
const style = window.getComputedStyle(root)
|
|
134
|
-
this.canvasw = parseInt(style.width, 10)
|
|
135
|
-
this.canvash = parseInt(style.height, 10)
|
|
136
|
-
const canvas = this.$refs.canvas
|
|
137
|
-
this.ctx = canvas.getContext('2d')
|
|
138
|
-
this.$nextTick(() => {
|
|
139
|
-
this.clear()
|
|
140
|
-
})
|
|
141
|
-
} else {
|
|
142
|
-
// uni环境:无法操作dom 只能通过它提供的API获取布局信息
|
|
143
|
-
const query = uni.createSelectorQuery().in(this)
|
|
144
|
-
query
|
|
145
|
-
.select('.wrapper')
|
|
146
|
-
.boundingClientRect(res => {
|
|
147
|
-
this.canvasw = res.width - 2
|
|
148
|
-
this.canvash = res.height - 2
|
|
149
|
-
this.$nextTick(() => {
|
|
150
|
-
this.ctx = uni.createCanvasContext(this.canvasId, this)
|
|
151
|
-
this.clear()
|
|
152
|
-
})
|
|
153
|
-
})
|
|
154
|
-
.exec()
|
|
155
|
-
}
|
|
156
|
-
},
|
|
157
|
-
/**
|
|
158
|
-
* @vuese
|
|
159
|
-
* 清空画布内容
|
|
160
|
-
*/
|
|
161
|
-
clear() {
|
|
162
|
-
this.initRange()
|
|
163
|
-
this.initPen()
|
|
164
|
-
this.ctx.clearRect(0, 0, this.canvasw, this.canvash)
|
|
165
|
-
this.ctx.fillRect(0, 0, this.canvasw, this.canvash)
|
|
166
|
-
if (this.isUni) {
|
|
167
|
-
this.ctx.draw()
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
initPen() {
|
|
171
|
-
// 初始化画笔
|
|
172
|
-
if (this.isUni) {
|
|
173
|
-
this.ctx.setFillStyle(this.bgColor)
|
|
174
|
-
this.ctx.setStrokeStyle(this.color)
|
|
175
|
-
this.ctx.setLineWidth(this.lineWidth)
|
|
176
|
-
} else {
|
|
177
|
-
this.ctx.fillStyle = this.bgColor
|
|
178
|
-
this.ctx.strokeStyle = this.color
|
|
179
|
-
this.ctx.lineWidth = this.lineWidth
|
|
180
|
-
}
|
|
181
|
-
},
|
|
182
|
-
initRange() {
|
|
183
|
-
// 记录画布上左上角的点 s: start
|
|
184
|
-
this.s = { x: Number.MAX_VALUE, y: Number.MAX_VALUE }
|
|
185
|
-
// 记录画布右下角的点 e: end
|
|
186
|
-
this.e = { x: 0, y: 0 }
|
|
187
|
-
},
|
|
188
|
-
async getImageData(x, y, w, h) {
|
|
189
|
-
if (this.isUni) {
|
|
190
|
-
return new Promise(resolve => {
|
|
191
|
-
uni.canvasGetImageData({
|
|
192
|
-
canvasId: this.canvasId,
|
|
193
|
-
x,
|
|
194
|
-
y,
|
|
195
|
-
width: w,
|
|
196
|
-
height: h,
|
|
197
|
-
success: res => {
|
|
198
|
-
resolve(res.data)
|
|
199
|
-
},
|
|
200
|
-
fail: err => {
|
|
201
|
-
resolve(null)
|
|
202
|
-
}
|
|
203
|
-
})
|
|
204
|
-
})
|
|
205
|
-
} else {
|
|
206
|
-
return this.ctx.getImageData(x, y, w, h)
|
|
207
|
-
}
|
|
208
|
-
},
|
|
209
|
-
/**
|
|
210
|
-
* @vuese
|
|
211
|
-
* 裁剪画布内容
|
|
212
|
-
*/
|
|
213
|
-
async crop() {
|
|
214
|
-
if (this.s.x === Number.MAX_VALUE) {
|
|
215
|
-
// 标明画布无内容 不进行裁剪操作
|
|
216
|
-
return
|
|
217
|
-
}
|
|
218
|
-
if (this.minimal) {
|
|
219
|
-
// 需要最小化空白区域,计算裁剪区域
|
|
220
|
-
let x = this.s.x - (this.padding.left || 0)
|
|
221
|
-
let y = this.s.y - (this.padding.top || 0)
|
|
222
|
-
x = x < 0 ? 0 : x
|
|
223
|
-
y = y < 0 ? 0 : y
|
|
224
|
-
|
|
225
|
-
let x1 = this.e.x + (this.padding.right || 0)
|
|
226
|
-
let y1 = this.e.y + (this.padding.bottom || 0)
|
|
227
|
-
x1 = x1 > this.canvasw ? this.canvasw : x1
|
|
228
|
-
y1 = y1 > this.canvash ? this.canvash : y1
|
|
229
|
-
|
|
230
|
-
// 获取裁剪区域的RGBA像素点数据
|
|
231
|
-
const imageData = await this.getImageData(x, y, x1 - x, y1 - y)
|
|
232
|
-
this.emitCrop(imageData, x1 - x, y1 - y)
|
|
233
|
-
} else {
|
|
234
|
-
// 直接获取整张画布区域的像素点数据
|
|
235
|
-
const imageData = await this.getImageData(0, 0, this.canvasw, this.canvash)
|
|
236
|
-
this.emitCrop(imageData, this.canvasw, this.canvash)
|
|
237
|
-
}
|
|
238
|
-
},
|
|
239
|
-
async getBase64(imageData, w, h) {
|
|
240
|
-
// 原理:将imageData放入裁剪画布中,然后即可生成base64
|
|
241
|
-
if (this.isUni) {
|
|
242
|
-
// uni环境下 按uni提供的API进行操作
|
|
243
|
-
return new Promise(resolve => {
|
|
244
|
-
uni.canvasPutImageData(
|
|
245
|
-
{
|
|
246
|
-
canvasId: this.cropId,
|
|
247
|
-
data: imageData,
|
|
248
|
-
x: 0,
|
|
249
|
-
y: 0,
|
|
250
|
-
width: w,
|
|
251
|
-
height: h,
|
|
252
|
-
success: res => {
|
|
253
|
-
uni.canvasToTempFilePath({
|
|
254
|
-
canvasId: this.cropId,
|
|
255
|
-
x: 0,
|
|
256
|
-
y: 0,
|
|
257
|
-
success: res => {
|
|
258
|
-
resolve(res.tempFilePath)
|
|
259
|
-
},
|
|
260
|
-
fail: err => {
|
|
261
|
-
resolve('')
|
|
262
|
-
}
|
|
263
|
-
})
|
|
264
|
-
},
|
|
265
|
-
fail: err => {
|
|
266
|
-
resolve('')
|
|
267
|
-
}
|
|
268
|
-
},
|
|
269
|
-
this
|
|
270
|
-
)
|
|
271
|
-
})
|
|
272
|
-
} else {
|
|
273
|
-
// 否则 按常规尝试进行
|
|
274
|
-
const canvas = this.$refs.cropCanvas
|
|
275
|
-
ctx = canvas.getContext('2d')
|
|
276
|
-
ctx.putImageData(imageData, 0, 0)
|
|
277
|
-
const base64 = canvas.toDataURL()
|
|
278
|
-
return base64
|
|
279
|
-
}
|
|
280
|
-
},
|
|
281
|
-
emitCrop(imageData, w, h) {
|
|
282
|
-
if (!imageData) {
|
|
283
|
-
return
|
|
284
|
-
}
|
|
285
|
-
this.cropw = w
|
|
286
|
-
this.croph = h
|
|
287
|
-
setTimeout(async () => {
|
|
288
|
-
// $nextTick的目的是为了让裁剪画布的宽高生效后 再进行操作
|
|
289
|
-
const base64 = await this.getBase64(imageData, w, h)
|
|
290
|
-
const results = base64.match(/data:(.*?);.*?,(.*)/)
|
|
291
|
-
const mineType = results[1]
|
|
292
|
-
const suffix = mineType.split('/')[1]
|
|
293
|
-
const binstr = window.atob(results[2])
|
|
294
|
-
let n = binstr.length
|
|
295
|
-
const bytes = new Uint8Array(n)
|
|
296
|
-
while (n--) {
|
|
297
|
-
bytes[n] = binstr.charCodeAt(n)
|
|
298
|
-
}
|
|
299
|
-
const blob = new Blob([bytes], { type: mineType })
|
|
300
|
-
// export
|
|
301
|
-
// arg `{base64, blob, width, height, style}`
|
|
302
|
-
this.$emit('export', {
|
|
303
|
-
base64,
|
|
304
|
-
blob,
|
|
305
|
-
width: w,
|
|
306
|
-
height: h,
|
|
307
|
-
style: { width: `${w}px`, height: `${h}px` }
|
|
308
|
-
})
|
|
309
|
-
}, 100)
|
|
310
|
-
},
|
|
311
|
-
/**
|
|
312
|
-
* 随着画笔移动,不断调用此方法更新:包裹内容的矩形的左上角&右下角的坐标
|
|
313
|
-
*/
|
|
314
|
-
updateRange(x, y) {
|
|
315
|
-
if (x < this.s.x) {
|
|
316
|
-
this.s.x = x
|
|
317
|
-
} else if (x > this.e.x) {
|
|
318
|
-
this.e.x = x
|
|
319
|
-
}
|
|
320
|
-
if (y < this.s.y) {
|
|
321
|
-
this.s.y = y
|
|
322
|
-
} else if (y > this.e.y) {
|
|
323
|
-
this.e.y = y
|
|
324
|
-
}
|
|
325
|
-
},
|
|
326
|
-
start(e) {
|
|
327
|
-
// 适配移动端的写法: e.touches移动端才有
|
|
328
|
-
const touches = (e && e.changedTouches) || []
|
|
329
|
-
let x = 0,
|
|
330
|
-
y = 0
|
|
331
|
-
const touch = touches[0]
|
|
332
|
-
if (/^touch/.test(e.type)) {
|
|
333
|
-
x = touch.x
|
|
334
|
-
y = touch.y
|
|
335
|
-
} else {
|
|
336
|
-
x = e.offsetX
|
|
337
|
-
y = e.offsetY
|
|
338
|
-
}
|
|
339
|
-
// 为了清空之前残留的路径
|
|
340
|
-
this.ctx.beginPath()
|
|
341
|
-
this.ctx.moveTo(x, y)
|
|
342
|
-
this.ctx.stroke()
|
|
343
|
-
if (this.isUni) {
|
|
344
|
-
this.ctx.draw(true)
|
|
345
|
-
}
|
|
346
|
-
this.isBegin = true
|
|
347
|
-
this.updateRange(x, y)
|
|
348
|
-
this.prev = { x, y }
|
|
349
|
-
},
|
|
350
|
-
move(e) {
|
|
351
|
-
if (!this.isBegin) {
|
|
352
|
-
return
|
|
353
|
-
}
|
|
354
|
-
let x = 0,
|
|
355
|
-
y = 0
|
|
356
|
-
const touches = (e && e.changedTouches) || []
|
|
357
|
-
const touch = touches[0]
|
|
358
|
-
if (/^touch/.test(e.type)) {
|
|
359
|
-
x = touch.x
|
|
360
|
-
y = touch.y
|
|
361
|
-
} else {
|
|
362
|
-
x = e.offsetX
|
|
363
|
-
y = e.offsetY
|
|
364
|
-
}
|
|
365
|
-
this.ctx.moveTo(this.prev.x, this.prev.y)
|
|
366
|
-
this.ctx.lineTo(x, y)
|
|
367
|
-
this.ctx.stroke()
|
|
368
|
-
if (this.isUni) {
|
|
369
|
-
this.ctx.draw(true)
|
|
370
|
-
}
|
|
371
|
-
this.updateRange(x, y)
|
|
372
|
-
this.prev = { x, y }
|
|
373
|
-
},
|
|
374
|
-
end(e) {
|
|
375
|
-
// this.move(e)
|
|
376
|
-
this.isBegin = false
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
</script>
|
|
381
|
-
|
|
382
|
-
<style lang="scss" scoped>
|
|
383
|
-
.wrapper {
|
|
384
|
-
position: relative;
|
|
385
|
-
width: 100%;
|
|
386
|
-
height: 100%;
|
|
387
|
-
border: 1px solid #aaaaaa;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
.content {
|
|
391
|
-
position: relative;
|
|
392
|
-
margin: 0 auto;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
.copy {
|
|
396
|
-
position: absolute;
|
|
397
|
-
top: -50000px;
|
|
398
|
-
left: -50000px;
|
|
399
|
-
}
|
|
400
|
-
</style>
|
|
1
|
+
<template>
|
|
2
|
+
<view ref="root" :class="[customClass, 'wrapper']" :style="[customStyle]">
|
|
3
|
+
<canvas
|
|
4
|
+
ref="canvas"
|
|
5
|
+
:style="[canvasStyle]"
|
|
6
|
+
type="2d"
|
|
7
|
+
class="content"
|
|
8
|
+
:canvas-id="canvasId"
|
|
9
|
+
:width="canvasw"
|
|
10
|
+
:height="canvash"
|
|
11
|
+
@touchstart="start"
|
|
12
|
+
@touchmove="move"
|
|
13
|
+
@touchcancel="end"
|
|
14
|
+
@touchend="end"
|
|
15
|
+
@mousedown="start"
|
|
16
|
+
@mousemove="move"
|
|
17
|
+
@mouseup="end"
|
|
18
|
+
/>
|
|
19
|
+
<canvas
|
|
20
|
+
ref="cropCanvas"
|
|
21
|
+
type="2d"
|
|
22
|
+
class="copy"
|
|
23
|
+
:style="[cropStyle]"
|
|
24
|
+
:canvas-id="cropId"
|
|
25
|
+
:width="cropw"
|
|
26
|
+
:height="croph"
|
|
27
|
+
/>
|
|
28
|
+
</view>
|
|
29
|
+
</template>
|
|
30
|
+
<script>
|
|
31
|
+
export default {
|
|
32
|
+
name: 'PiCanvas',
|
|
33
|
+
props: {
|
|
34
|
+
// 自定义class
|
|
35
|
+
customClass: {
|
|
36
|
+
type: String,
|
|
37
|
+
// `''`
|
|
38
|
+
default: ''
|
|
39
|
+
},
|
|
40
|
+
// 自定义style
|
|
41
|
+
customStyle: {
|
|
42
|
+
type: Object,
|
|
43
|
+
// `{}`
|
|
44
|
+
default: () => ({})
|
|
45
|
+
},
|
|
46
|
+
// 是否最小化空白区域
|
|
47
|
+
minimal: {
|
|
48
|
+
type: Boolean,
|
|
49
|
+
// `false`
|
|
50
|
+
default: false
|
|
51
|
+
},
|
|
52
|
+
// 当minimal:true时,指定pandding
|
|
53
|
+
padding: {
|
|
54
|
+
type: Object,
|
|
55
|
+
// `{ top: 8, right: 8, bottom: 8, left: 8 }`
|
|
56
|
+
default: () => ({ top: 8, right: 8, bottom: 8, left: 8 })
|
|
57
|
+
},
|
|
58
|
+
// 画布背景色
|
|
59
|
+
bgColor: {
|
|
60
|
+
type: String,
|
|
61
|
+
// `#ffffff`
|
|
62
|
+
default: '#ffffff'
|
|
63
|
+
},
|
|
64
|
+
// 画笔颜色
|
|
65
|
+
color: {
|
|
66
|
+
type: String,
|
|
67
|
+
// `#333333`
|
|
68
|
+
default: '#333333'
|
|
69
|
+
},
|
|
70
|
+
// 线条宽度
|
|
71
|
+
lineWidth: {
|
|
72
|
+
type: Number,
|
|
73
|
+
// `1`
|
|
74
|
+
default: 1
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
data() {
|
|
78
|
+
return {
|
|
79
|
+
isBegin: false, // 标记是否先触发了touchstart or mousedown
|
|
80
|
+
canvasId: '', // 内容画布的ID
|
|
81
|
+
cropId: '', // 裁剪画布的ID
|
|
82
|
+
ctx: null, // 内容画布的上下文
|
|
83
|
+
canvasw: 0, // 内容画布的宽度
|
|
84
|
+
canvash: 0, // 内容画布的高度
|
|
85
|
+
cropw: 0, // 裁剪画布的宽度
|
|
86
|
+
croph: 0 // 裁剪画布的高度
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
computed: {
|
|
90
|
+
isUni() {
|
|
91
|
+
return window.uni != null
|
|
92
|
+
},
|
|
93
|
+
canvasStyle() {
|
|
94
|
+
return {
|
|
95
|
+
width: `${this.canvasw}px`,
|
|
96
|
+
height: `${this.canvash}px`
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
cropStyle() {
|
|
100
|
+
return {
|
|
101
|
+
width: `${this.cropw}px`,
|
|
102
|
+
height: `${this.croph}px`
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
created() {
|
|
107
|
+
this.$nextTick(() => {
|
|
108
|
+
this.initCanvas()
|
|
109
|
+
})
|
|
110
|
+
},
|
|
111
|
+
methods: {
|
|
112
|
+
generateId() {
|
|
113
|
+
// 产生16位随机的字母、数字混合的随机ID
|
|
114
|
+
const tmps = '0123456789'
|
|
115
|
+
.split('')
|
|
116
|
+
.concat('abcdefghijklmnopqrstuvwxyz'.split(''))
|
|
117
|
+
.concat('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''))
|
|
118
|
+
let n = tmps.length
|
|
119
|
+
while (n--) {
|
|
120
|
+
const idx = Math.floor(Math.random() * n)
|
|
121
|
+
const tmp = tmps[idx]
|
|
122
|
+
tmps[idx] = tmps[n]
|
|
123
|
+
tmps[n] = tmp
|
|
124
|
+
}
|
|
125
|
+
return tmps.join('').substr(0, 16)
|
|
126
|
+
},
|
|
127
|
+
initCanvas() {
|
|
128
|
+
this.canvasId = this.generateId()
|
|
129
|
+
this.cropId = this.generateId()
|
|
130
|
+
if (!this.isUni) {
|
|
131
|
+
// 非uni环境
|
|
132
|
+
const root = this.$refs.root
|
|
133
|
+
const style = window.getComputedStyle(root)
|
|
134
|
+
this.canvasw = parseInt(style.width, 10)
|
|
135
|
+
this.canvash = parseInt(style.height, 10)
|
|
136
|
+
const canvas = this.$refs.canvas
|
|
137
|
+
this.ctx = canvas.getContext('2d')
|
|
138
|
+
this.$nextTick(() => {
|
|
139
|
+
this.clear()
|
|
140
|
+
})
|
|
141
|
+
} else {
|
|
142
|
+
// uni环境:无法操作dom 只能通过它提供的API获取布局信息
|
|
143
|
+
const query = uni.createSelectorQuery().in(this)
|
|
144
|
+
query
|
|
145
|
+
.select('.wrapper')
|
|
146
|
+
.boundingClientRect(res => {
|
|
147
|
+
this.canvasw = res.width - 2
|
|
148
|
+
this.canvash = res.height - 2
|
|
149
|
+
this.$nextTick(() => {
|
|
150
|
+
this.ctx = uni.createCanvasContext(this.canvasId, this)
|
|
151
|
+
this.clear()
|
|
152
|
+
})
|
|
153
|
+
})
|
|
154
|
+
.exec()
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
/**
|
|
158
|
+
* @vuese
|
|
159
|
+
* 清空画布内容
|
|
160
|
+
*/
|
|
161
|
+
clear() {
|
|
162
|
+
this.initRange()
|
|
163
|
+
this.initPen()
|
|
164
|
+
this.ctx.clearRect(0, 0, this.canvasw, this.canvash)
|
|
165
|
+
this.ctx.fillRect(0, 0, this.canvasw, this.canvash)
|
|
166
|
+
if (this.isUni) {
|
|
167
|
+
this.ctx.draw()
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
initPen() {
|
|
171
|
+
// 初始化画笔
|
|
172
|
+
if (this.isUni) {
|
|
173
|
+
this.ctx.setFillStyle(this.bgColor)
|
|
174
|
+
this.ctx.setStrokeStyle(this.color)
|
|
175
|
+
this.ctx.setLineWidth(this.lineWidth)
|
|
176
|
+
} else {
|
|
177
|
+
this.ctx.fillStyle = this.bgColor
|
|
178
|
+
this.ctx.strokeStyle = this.color
|
|
179
|
+
this.ctx.lineWidth = this.lineWidth
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
initRange() {
|
|
183
|
+
// 记录画布上左上角的点 s: start
|
|
184
|
+
this.s = { x: Number.MAX_VALUE, y: Number.MAX_VALUE }
|
|
185
|
+
// 记录画布右下角的点 e: end
|
|
186
|
+
this.e = { x: 0, y: 0 }
|
|
187
|
+
},
|
|
188
|
+
async getImageData(x, y, w, h) {
|
|
189
|
+
if (this.isUni) {
|
|
190
|
+
return new Promise(resolve => {
|
|
191
|
+
uni.canvasGetImageData({
|
|
192
|
+
canvasId: this.canvasId,
|
|
193
|
+
x,
|
|
194
|
+
y,
|
|
195
|
+
width: w,
|
|
196
|
+
height: h,
|
|
197
|
+
success: res => {
|
|
198
|
+
resolve(res.data)
|
|
199
|
+
},
|
|
200
|
+
fail: err => {
|
|
201
|
+
resolve(null)
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
} else {
|
|
206
|
+
return this.ctx.getImageData(x, y, w, h)
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
/**
|
|
210
|
+
* @vuese
|
|
211
|
+
* 裁剪画布内容
|
|
212
|
+
*/
|
|
213
|
+
async crop() {
|
|
214
|
+
if (this.s.x === Number.MAX_VALUE) {
|
|
215
|
+
// 标明画布无内容 不进行裁剪操作
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
if (this.minimal) {
|
|
219
|
+
// 需要最小化空白区域,计算裁剪区域
|
|
220
|
+
let x = this.s.x - (this.padding.left || 0)
|
|
221
|
+
let y = this.s.y - (this.padding.top || 0)
|
|
222
|
+
x = x < 0 ? 0 : x
|
|
223
|
+
y = y < 0 ? 0 : y
|
|
224
|
+
|
|
225
|
+
let x1 = this.e.x + (this.padding.right || 0)
|
|
226
|
+
let y1 = this.e.y + (this.padding.bottom || 0)
|
|
227
|
+
x1 = x1 > this.canvasw ? this.canvasw : x1
|
|
228
|
+
y1 = y1 > this.canvash ? this.canvash : y1
|
|
229
|
+
|
|
230
|
+
// 获取裁剪区域的RGBA像素点数据
|
|
231
|
+
const imageData = await this.getImageData(x, y, x1 - x, y1 - y)
|
|
232
|
+
this.emitCrop(imageData, x1 - x, y1 - y)
|
|
233
|
+
} else {
|
|
234
|
+
// 直接获取整张画布区域的像素点数据
|
|
235
|
+
const imageData = await this.getImageData(0, 0, this.canvasw, this.canvash)
|
|
236
|
+
this.emitCrop(imageData, this.canvasw, this.canvash)
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
async getBase64(imageData, w, h) {
|
|
240
|
+
// 原理:将imageData放入裁剪画布中,然后即可生成base64
|
|
241
|
+
if (this.isUni) {
|
|
242
|
+
// uni环境下 按uni提供的API进行操作
|
|
243
|
+
return new Promise(resolve => {
|
|
244
|
+
uni.canvasPutImageData(
|
|
245
|
+
{
|
|
246
|
+
canvasId: this.cropId,
|
|
247
|
+
data: imageData,
|
|
248
|
+
x: 0,
|
|
249
|
+
y: 0,
|
|
250
|
+
width: w,
|
|
251
|
+
height: h,
|
|
252
|
+
success: res => {
|
|
253
|
+
uni.canvasToTempFilePath({
|
|
254
|
+
canvasId: this.cropId,
|
|
255
|
+
x: 0,
|
|
256
|
+
y: 0,
|
|
257
|
+
success: res => {
|
|
258
|
+
resolve(res.tempFilePath)
|
|
259
|
+
},
|
|
260
|
+
fail: err => {
|
|
261
|
+
resolve('')
|
|
262
|
+
}
|
|
263
|
+
})
|
|
264
|
+
},
|
|
265
|
+
fail: err => {
|
|
266
|
+
resolve('')
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
this
|
|
270
|
+
)
|
|
271
|
+
})
|
|
272
|
+
} else {
|
|
273
|
+
// 否则 按常规尝试进行
|
|
274
|
+
const canvas = this.$refs.cropCanvas
|
|
275
|
+
ctx = canvas.getContext('2d')
|
|
276
|
+
ctx.putImageData(imageData, 0, 0)
|
|
277
|
+
const base64 = canvas.toDataURL()
|
|
278
|
+
return base64
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
emitCrop(imageData, w, h) {
|
|
282
|
+
if (!imageData) {
|
|
283
|
+
return
|
|
284
|
+
}
|
|
285
|
+
this.cropw = w
|
|
286
|
+
this.croph = h
|
|
287
|
+
setTimeout(async () => {
|
|
288
|
+
// $nextTick的目的是为了让裁剪画布的宽高生效后 再进行操作
|
|
289
|
+
const base64 = await this.getBase64(imageData, w, h)
|
|
290
|
+
const results = base64.match(/data:(.*?);.*?,(.*)/)
|
|
291
|
+
const mineType = results[1]
|
|
292
|
+
const suffix = mineType.split('/')[1]
|
|
293
|
+
const binstr = window.atob(results[2])
|
|
294
|
+
let n = binstr.length
|
|
295
|
+
const bytes = new Uint8Array(n)
|
|
296
|
+
while (n--) {
|
|
297
|
+
bytes[n] = binstr.charCodeAt(n)
|
|
298
|
+
}
|
|
299
|
+
const blob = new Blob([bytes], { type: mineType })
|
|
300
|
+
// export
|
|
301
|
+
// arg `{base64, blob, width, height, style}`
|
|
302
|
+
this.$emit('export', {
|
|
303
|
+
base64,
|
|
304
|
+
blob,
|
|
305
|
+
width: w,
|
|
306
|
+
height: h,
|
|
307
|
+
style: { width: `${w}px`, height: `${h}px` }
|
|
308
|
+
})
|
|
309
|
+
}, 100)
|
|
310
|
+
},
|
|
311
|
+
/**
|
|
312
|
+
* 随着画笔移动,不断调用此方法更新:包裹内容的矩形的左上角&右下角的坐标
|
|
313
|
+
*/
|
|
314
|
+
updateRange(x, y) {
|
|
315
|
+
if (x < this.s.x) {
|
|
316
|
+
this.s.x = x
|
|
317
|
+
} else if (x > this.e.x) {
|
|
318
|
+
this.e.x = x
|
|
319
|
+
}
|
|
320
|
+
if (y < this.s.y) {
|
|
321
|
+
this.s.y = y
|
|
322
|
+
} else if (y > this.e.y) {
|
|
323
|
+
this.e.y = y
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
start(e) {
|
|
327
|
+
// 适配移动端的写法: e.touches移动端才有
|
|
328
|
+
const touches = (e && e.changedTouches) || []
|
|
329
|
+
let x = 0,
|
|
330
|
+
y = 0
|
|
331
|
+
const touch = touches[0]
|
|
332
|
+
if (/^touch/.test(e.type)) {
|
|
333
|
+
x = touch.x
|
|
334
|
+
y = touch.y
|
|
335
|
+
} else {
|
|
336
|
+
x = e.offsetX
|
|
337
|
+
y = e.offsetY
|
|
338
|
+
}
|
|
339
|
+
// 为了清空之前残留的路径
|
|
340
|
+
this.ctx.beginPath()
|
|
341
|
+
this.ctx.moveTo(x, y)
|
|
342
|
+
this.ctx.stroke()
|
|
343
|
+
if (this.isUni) {
|
|
344
|
+
this.ctx.draw(true)
|
|
345
|
+
}
|
|
346
|
+
this.isBegin = true
|
|
347
|
+
this.updateRange(x, y)
|
|
348
|
+
this.prev = { x, y }
|
|
349
|
+
},
|
|
350
|
+
move(e) {
|
|
351
|
+
if (!this.isBegin) {
|
|
352
|
+
return
|
|
353
|
+
}
|
|
354
|
+
let x = 0,
|
|
355
|
+
y = 0
|
|
356
|
+
const touches = (e && e.changedTouches) || []
|
|
357
|
+
const touch = touches[0]
|
|
358
|
+
if (/^touch/.test(e.type)) {
|
|
359
|
+
x = touch.x
|
|
360
|
+
y = touch.y
|
|
361
|
+
} else {
|
|
362
|
+
x = e.offsetX
|
|
363
|
+
y = e.offsetY
|
|
364
|
+
}
|
|
365
|
+
this.ctx.moveTo(this.prev.x, this.prev.y)
|
|
366
|
+
this.ctx.lineTo(x, y)
|
|
367
|
+
this.ctx.stroke()
|
|
368
|
+
if (this.isUni) {
|
|
369
|
+
this.ctx.draw(true)
|
|
370
|
+
}
|
|
371
|
+
this.updateRange(x, y)
|
|
372
|
+
this.prev = { x, y }
|
|
373
|
+
},
|
|
374
|
+
end(e) {
|
|
375
|
+
// this.move(e)
|
|
376
|
+
this.isBegin = false
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
</script>
|
|
381
|
+
|
|
382
|
+
<style lang="scss" scoped>
|
|
383
|
+
.wrapper {
|
|
384
|
+
position: relative;
|
|
385
|
+
width: 100%;
|
|
386
|
+
height: 100%;
|
|
387
|
+
border: 1px solid #aaaaaa;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
.content {
|
|
391
|
+
position: relative;
|
|
392
|
+
margin: 0 auto;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
.copy {
|
|
396
|
+
position: absolute;
|
|
397
|
+
top: -50000px;
|
|
398
|
+
left: -50000px;
|
|
399
|
+
}
|
|
400
|
+
</style>
|