-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathwxml2canvas.js
545 lines (500 loc) · 14.1 KB
/
wxml2canvas.js
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/* eslint-disable */
const PROPERTIES = ['hover-class', 'hover-start-time', 'space', 'src']
const COMPUTED_STYLE = [
'color',
'font-size',
'font-weight',
'font-family',
'backgroundColor',
'border',
'border-radius',
'box-sizing',
'line-height',
]
const DEFAULT_BORDER = '0px none rgb(0, 0, 0)'
const DEFAULT_BORDER_RADIUS = '0px'
// default z-index??
const DEFAULT_RANK = {
view: 0,
image: 1,
text: 2,
}
const drawWrapper = (context, data) => {
const { backgroundColor, width, height } = data
context.setFillStyle(backgroundColor)
context.fillRect(0, 0, width, height)
}
// todo: do more for different language
const strLen = str => {
let count = 0
for (let i = 0, len = str.length; i < len; i++) {
count += str.charCodeAt(i) < 256 ? 1 : 2
}
return count / 2
}
const isMuitlpleLine = (data, text) => {
const { 'font-size': letterWidth, width } = data
const length = strLen(text)
const rowlineLength = length * parseInt(letterWidth, 10)
return rowlineLength > width
}
const drawMutipleLine = (context, data, text) => {
const {
'font-size': letterWidth,
width,
left,
top,
'line-height': lineHeightAttr,
} = data
const lineHieght = lineHeightAttr === 'normal' ? Math.round(1.2 * letterWidth) : lineHeightAttr
const rowLetterCount = Math.floor(width / parseInt(letterWidth, 10))
const length = strLen(text)
for (let i = 0; i < length; i += rowLetterCount) {
const lineText = text.substring(i, i + rowLetterCount)
const rowNumber = Math.floor(i / rowLetterCount)
const rowTop = top + rowNumber * parseInt(lineHieght, 10)
context.fillText(lineText, left, rowTop)
}
}
// enable color, font, for now only support chinese
const drawText = (context, data) => {
const {
dataset: { text },
left,
top,
color,
'font-weight': fontWeight,
'font-size': fontSize,
'font-family': fontFamily,
} = data
const canvasText = Array.isArray(text) ? text[0] : text
context.font = `${fontWeight} ${Math.round(
parseFloat(fontSize),
)}px ${fontFamily}`
context.setFillStyle(color)
if (isMuitlpleLine(data, canvasText)) {
drawMutipleLine(context, data, canvasText)
} else {
context.fillText(canvasText, left, top)
}
context.restore()
}
const getImgInfo = src =>
new Promise((resolve, reject) => {
wx.getImageInfo({
src,
success(res) {
resolve(res)
},
})
})
const hasBorder = border => border !== DEFAULT_BORDER
const hasBorderRadius = borderRadius => borderRadius !== DEFAULT_BORDER_RADIUS
const getBorderAttributes = border => {
let borderColor, borderStyle
let borderWidth = 0
if (hasBorder) {
borderWidth = parseInt(border.split(/\s/)[0], 10)
borderStyle = border.split(/\s/)[1]
borderColor = border.match(/(rgb).*/gi)[0]
}
return {
borderWidth,
borderStyle,
borderColor,
}
}
const getImgRect = (imgData, borderWidth) => {
const { width, height, left, top } = imgData
const imgWidth = width - 2 * borderWidth
const imgHeight = height - 2 * borderWidth
const imgLeft = left + borderWidth
const imgTop = top + borderWidth
return {
imgWidth,
imgHeight,
imgLeft,
imgTop,
}
}
const getArcCenterPosition = imgData => {
const { width, height, left, top } = imgData
const coordX = width / 2 + left
const coordY = height / 2 + top
return {
coordX,
coordY,
}
}
const getArcRadius = (imgData, borderWidth = 0) => {
const { width } = imgData
return width / 2 - borderWidth / 2
}
const getCalculatedImagePosition = (imgData, naturalWidth, naturalHeight) => {
const { border } = imgData
const { borderWidth } = getBorderAttributes(border)
const { imgWidth, imgHeight, imgLeft, imgTop } = getImgRect(
imgData,
borderWidth,
)
const ratio = naturalWidth / naturalHeight
// tweak for real width and position => center center
const realWidth = ratio > 0 ? imgWidth : imgHeight * ratio
const realHeight = ratio > 0 ? imgWidth * (1 / ratio) : imgHeight
const offsetLeft = ratio > 0 ? 0 : (imgWidth - realWidth) / 2
const offsetTop = ratio > 0 ? (imgHeight - realHeight) / 2 : 0
return {
realWidth,
realHeight,
left: imgLeft + offsetLeft,
top: imgTop + offsetTop,
}
}
const drawArcImage = (context, imgData) => {
const { src } = imgData
const { coordX, coordY } = getArcCenterPosition(imgData)
return getImgInfo(src).then(res => {
const { width: naturalWidth, height: naturalHeight } = res
const arcRadius = getArcRadius(imgData)
context.save()
context.beginPath()
context.arc(coordX, coordY, arcRadius, 0, 2 * Math.PI)
context.closePath()
context.clip()
const { left, top, realWidth, realHeight } = getCalculatedImagePosition(
imgData,
naturalWidth,
naturalHeight,
)
context.drawImage(
src,
0,
0,
naturalWidth,
naturalHeight,
left,
top,
realWidth,
realHeight,
)
context.restore()
})
}
const drawRectImage = (context, imgData) => {
const { src, width, height, left, top } = imgData
return getImgInfo(src).then(res => {
const { width: naturalWidth, height: naturalHeight } = res
context.save()
context.beginPath()
context.rect(left, top, width, height)
context.closePath()
context.clip()
const {
left: realLeft,
top: realTop,
realWidth,
realHeight,
} = getCalculatedImagePosition(imgData, naturalWidth, naturalHeight)
context.drawImage(
src,
0,
0,
naturalWidth,
naturalHeight,
realLeft,
realTop,
realWidth,
realHeight,
)
context.restore()
})
}
const drawArcBorder = (context, imgData) => {
const { border } = imgData
const { coordX, coordY } = getArcCenterPosition(imgData)
const { borderWidth, borderColor } = getBorderAttributes(border)
const arcRadius = getArcRadius(imgData, borderWidth)
context.save()
context.beginPath()
context.setLineWidth(borderWidth)
context.setStrokeStyle(borderColor)
context.arc(coordX, coordY, arcRadius, 0, 2 * Math.PI)
context.stroke()
context.restore()
}
const drawRectBorder = (context, imgData) => {
const { border } = imgData
const { left, top, width, height } = imgData
const { borderWidth, borderColor } = getBorderAttributes(border)
const correctedBorderWidth = borderWidth + 1 // draw may cause empty 0.5 space
context.save()
context.beginPath()
context.setLineWidth(correctedBorderWidth)
context.setStrokeStyle(borderColor)
context.rect(
left + borderWidth / 2,
top + borderWidth / 2,
width - borderWidth,
height - borderWidth,
)
context.stroke()
context.restore()
}
// image, enable border-radius: 50%, border, bgColor
const drawImage = (context, imgData) => {
const { border, 'border-radius': borderRadius } = imgData
let drawImagePromise
if (hasBorderRadius(borderRadius)) {
drawImagePromise = drawArcImage(context, imgData)
} else {
drawImagePromise = drawRectImage(context, imgData)
}
return drawImagePromise.then(() => {
if (hasBorder(border)) {
if (hasBorderRadius(borderRadius)) {
return drawArcBorder(context, imgData)
} else {
return drawRectBorder(context, imgData)
}
}
return Promise.resolve()
})
}
// e.g. 10%, 4px
const getBorderRadius = imgData => {
const { width, height, 'border-radius': borderRadiusAttr } = imgData
const borderRadius = parseInt(borderRadiusAttr, 10)
if (borderRadiusAttr.indexOf('%') !== -1) {
const borderRadiusX = parseInt(borderRadius / 100 * width, 10)
const borderRadiusY = parseInt(borderRadius / 100 * height, 10)
return {
isCircle: borderRadiusX === borderRadiusY,
borderRadius: borderRadiusX,
borderRadiusX,
borderRadiusY,
}
} else {
return {
isCircle: true,
borderRadius,
}
}
}
const drawViewArcBorder = (context, imgData) => {
const { width, height, left, top, backgroundColor, border } = imgData
const { borderRadius } = getBorderRadius(imgData)
const { borderWidth, borderColor } = getBorderAttributes(border)
// console.log('🐞-imgData', imgData)
context.beginPath()
context.moveTo(left + borderRadius, top)
context.lineTo(left + width - borderRadius, top)
context.arcTo(
left + width,
top,
left + width,
top + borderRadius,
borderRadius,
)
context.lineTo(left + width, top + height - borderRadius)
context.arcTo(
left + width,
top + height,
left + width - borderRadius,
top + height,
borderRadius,
)
context.lineTo(left + borderRadius, top + height)
context.arcTo(
left,
top + height,
left,
top + height - borderRadius,
borderRadius,
)
context.lineTo(left, top + borderRadius)
context.arcTo(left, top, left + borderRadius, top, borderRadius)
context.closePath()
if (backgroundColor) {
context.setFillStyle(backgroundColor)
context.fill()
}
if (borderColor && borderWidth) {
context.setLineWidth(borderWidth)
context.setStrokeStyle(borderColor)
context.stroke()
}
}
const drawViewBezierBorder = (context, imgData) => {
const { width, height, left, top, backgroundColor, border } = imgData
const { borderWidth, borderColor } = getBorderAttributes(border)
const { borderRadiusX, borderRadiusY } = getBorderRadius(imgData)
context.beginPath()
context.moveTo(left + borderRadiusX, top)
context.lineTo(left + width - borderRadiusX, top)
context.quadraticCurveTo(left + width, top, left + width, top + borderRadiusY)
context.lineTo(left + width, top + height - borderRadiusY)
context.quadraticCurveTo(
left + width,
top + height,
left + width - borderRadiusX,
top + height,
)
context.lineTo(left + borderRadiusX, top + height)
context.quadraticCurveTo(
left,
top + height,
left,
top + height - borderRadiusY,
)
context.lineTo(left, top + borderRadiusY)
context.quadraticCurveTo(left, top, left + borderRadiusX, top)
context.closePath()
if (backgroundColor) {
context.setFillStyle(backgroundColor)
context.fill()
}
if (borderColor && borderWidth) {
context.setLineWidth(borderWidth)
context.setStrokeStyle(borderColor)
context.stroke()
}
}
// enable border, border-radius, bgColor, position
const drawView = (context, imgData) => {
const { isCircle } = getBorderRadius(imgData)
if (isCircle) {
drawViewArcBorder(context, imgData)
} else {
drawViewBezierBorder(context, imgData)
}
}
const isTextElement = item => {
const { dataset: { text }, type } = item
return Boolean(text) || type === 'text'
}
const isImageElement = item => {
const { src, type } = item
return Boolean(src) || type === 'image'
}
const isViewElement = item => {
const { type } = item
return type === 'view'
}
const formatElementData = elements =>
elements.map(element => {
if (isTextElement(element)) {
element.type = 'text'
element.rank = DEFAULT_RANK.text
} else if (isImageElement(element)) {
element.type = 'image'
element.rank = DEFAULT_RANK.image
} else {
element.type = 'view'
element.rank = DEFAULT_RANK.view
}
return element
})
// todo: use z-index as order to draw??
const getSortedElementsData = elements =>
elements.sort((a, b) => {
if (a.rank < b.rank) {
return -1
} else if (a.rank > b.rank) {
return 1
}
return 0
})
const drawElements = (context, storeItems) => {
const itemPromise = []
storeItems.forEach(item => {
if (isTextElement(item)) {
const text = drawText(context, item)
itemPromise.push(text)
} else if (isImageElement(item)) {
const image = drawImage(context, item)
itemPromise.push(image)
} else {
const view = drawView(context, item)
itemPromise.push(view)
}
})
return itemPromise
}
// storeObject: { 0: [...], 1: [...] }
// chain call promise based on Object key
const drawElementBaseOnIndex = (context, storeObject, key = 0, drawPromise) => {
if (typeof drawPromise === 'undefined') {
drawPromise = Promise.resolve()
}
const objectKey = key // note: key is changing when execute promise then
const chainPromise = drawPromise.then(() => {
const nextPromise = storeObject[objectKey]
? Promise.all(drawElements(context, storeObject[objectKey]))
: Promise.resolve()
return nextPromise
})
if (key >= Object.keys(storeObject).length) {
return chainPromise
} else {
return drawElementBaseOnIndex(context, storeObject, key + 1, chainPromise)
}
}
const drawCanvas = (canvasId, wrapperData, innerData) => {
const context = wx.createCanvasContext(canvasId)
context.setTextBaseline('top')
// todo: use this after weixin fix stupid clip can't work bug in fillRect
// for now, just set canvas background as a compromise
drawWrapper(context, wrapperData[0])
const storeObject = {}
const sortedElementData = getSortedElementsData(formatElementData(innerData)) // fake z-index
sortedElementData.forEach(item => {
if (!storeObject[item.rank]) {
// initialize
storeObject[item.rank] = []
}
if (isTextElement(item) || isImageElement(item) || isViewElement(item)) {
storeObject[item.rank].push(item)
}
})
// note: draw is async
return drawElementBaseOnIndex(context, storeObject).then(
() =>
new Promise((resolve, reject) => {
context.draw(true, () => {
resolve()
})
}),
)
}
const wxSelectorQuery = element =>
new Promise((resolve, reject) => {
try {
wx
.createSelectorQuery()
.selectAll(element)
.fields(
{
dataset: true,
size: true,
rect: true,
properties: PROPERTIES,
computedStyle: COMPUTED_STYLE,
},
res => {
resolve(res)
},
)
.exec()
} catch (error) {
reject(error)
}
})
const wxml2canvas = (wrapperId, elementsClass, canvasId) => {
const getWrapperElement = wxSelectorQuery(wrapperId)
const getInnerElements = wxSelectorQuery(elementsClass)
return Promise.all([getWrapperElement, getInnerElements]).then(data => {
return drawCanvas(canvasId, data[0], data[1])
})
}
// export default wxml2canvas
module.exports = wxml2canvas