Commit 43f3af47 by 李伟康

canvas增加批量

1 parent 81a805af
...@@ -8,11 +8,8 @@ class Draw { ...@@ -8,11 +8,8 @@ class Draw {
[_canvasObject_] = null; [_canvasObject_] = null;
[_canvas_] = null [_canvas_] = null
constructor(canvasObject) { constructor(canvasObject) {
wx.getSystemInfo().then(({
pixelRatio
}) => {
this[_canvasObject_] = canvasObject this[_canvasObject_] = canvasObject
this[_pixelRatio_] = canvasObject.pixelRatio || pixelRatio || 2 this[_pixelRatio_] = canvasObject.pixelRatio || 2
const height = this.toRpx(canvasObject.height), const height = this.toRpx(canvasObject.height),
width = this.toRpx(canvasObject.width), width = this.toRpx(canvasObject.width),
canvas = wx.createOffscreenCanvas({ canvas = wx.createOffscreenCanvas({
...@@ -32,9 +29,8 @@ class Draw { ...@@ -32,9 +29,8 @@ class Draw {
color: canvasObject.backgroundColor || 'rgb(255, 255, 255)' color: canvasObject.backgroundColor || 'rgb(255, 255, 255)'
}) })
this[_ctx_].save() 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.onload = resolve
image.src = `${url}?v=${Date.now()}` image.src = `${url}?v=${Date.now()}`
}).then(() => { return new Promise(resolve => {
image.onload = () => {
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!