Commit 43f3af47 by 李伟康

canvas增加批量

1 parent 81a805af
...@@ -8,33 +8,29 @@ class Draw { ...@@ -8,33 +8,29 @@ class Draw {
[_canvasObject_] = null; [_canvasObject_] = null;
[_canvas_] = null [_canvas_] = null
constructor(canvasObject) { constructor(canvasObject) {
wx.getSystemInfo().then(({ this[_canvasObject_] = canvasObject
pixelRatio this[_pixelRatio_] = canvasObject.pixelRatio || 2
}) => { const height = this.toRpx(canvasObject.height),
this[_canvasObject_] = canvasObject width = this.toRpx(canvasObject.width),
this[_pixelRatio_] = canvasObject.pixelRatio || pixelRatio || 2 canvas = wx.createOffscreenCanvas({
const height = this.toRpx(canvasObject.height), type: '2d',
width = this.toRpx(canvasObject.width),
canvas = wx.createOffscreenCanvas({
type: '2d',
width,
height
}),
ctx = canvas.getContext('2d')
this[_canvas_] = canvas
this[_ctx_] = ctx
this[_ctx_].clearRect(0, 0, width, height)
this.drawFill({
x: 0,
y: 0,
width, width,
height, height
color: canvasObject.backgroundColor || 'rgb(255, 255, 255)' }),
}) ctx = canvas.getContext('2d')
this[_ctx_].save() this[_canvas_] = canvas
this[_ctx_] = ctx
this[_ctx_].clearRect(0, 0, width, height)
this.drawFill({
x: 0,
y: 0,
width,
height,
color: canvasObject.backgroundColor || 'rgb(255, 255, 255)'
}) })
this[_ctx_].save()
} }
drawImages(imageParams, callback) { async drawImages(imageParams, callback) {
this[_ctx_].beginPath() this[_ctx_].beginPath()
this[_ctx_].save() this[_ctx_].save()
let { let {
...@@ -60,13 +56,14 @@ class Draw { ...@@ -60,13 +56,14 @@ class Draw {
this[_ctx_].clip() this[_ctx_].clip()
} }
let image = this[_canvas_].createImage() let image = this[_canvas_].createImage()
new Promise(resolve => { image.src = `${url}?v=${Date.now()}`
image.onload = resolve return new Promise(resolve => {
image.src = `${url}?v=${Date.now()}` image.onload = () => {
}).then(() => { this[_ctx_].drawImage(image, x, y, width, height)
this[_ctx_].drawImage(image, x, y, width, height) this[_ctx_].restore()
this[_ctx_].restore() if (callback) callback()
if (callback) callback() resolve(imageParams)
}
}) })
} }
toRpx(val) { toRpx(val) {
...@@ -171,6 +168,7 @@ class Draw { ...@@ -171,6 +168,7 @@ class Draw {
height: boxHeight, height: boxHeight,
color: textParams.background color: textParams.background
}) })
} }
const drawOneLineText = () => { const drawOneLineText = () => {
let textArr = text.toString().split(''), let textArr = text.toString().split(''),
...@@ -249,10 +247,7 @@ class Draw { ...@@ -249,10 +247,7 @@ class Draw {
} else { } else {
drawOneLineText() drawOneLineText()
} }
} else { } else drawOneLineText()
drawOneLineText()
}
this[_ctx_].stroke() this[_ctx_].stroke()
} }
drawLine(lineParams, callback) { drawLine(lineParams, callback) {
...@@ -391,10 +386,33 @@ class Draw { ...@@ -391,10 +386,33 @@ class Draw {
}) })
} }
base64src(url).then(img => { base64src(url).then(img => {
console.log('img', img)
createLocalImg(img, callBack) createLocalImg(img, callBack)
}) })
} }
async begin(data = {}) {
return new Promise(async (resolve, reject) => {
const drawFun = () => {
data.textList && data.textList.length && data.textList.map(e => {
this.drawText(e)
})
data.lineList && data.lineList.length && data.lineList.map(e => {
this.drawLine(e)
})
wx.nextTick(() => {
this.toDataURL(res => resolve(res))
})
}
if (data.imageList && data.imageList.length) {
await this.drawImages(data.imageList[0])
if (data.imageList.length > 1)
for (let index = 1; index < data.imageList.length; index++) {
await this.drawImages(data.imageList[index])
if (index == data.imageList.length - 1) drawFun()
}
else drawFun()
}
})
}
} }
const checkTextDecoration = textDecoration => { const checkTextDecoration = textDecoration => {
return textDecoration && (textDecoration === 'underline' || textDecoration === 'overline' || textDecoration === 'line-through') return textDecoration && (textDecoration === 'underline' || textDecoration === 'overline' || textDecoration === 'line-through')
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!