Commit 43f3af47 by 李伟康

canvas增加批量

1 parent 81a805af
......@@ -8,33 +8,29 @@ class Draw {
[_canvasObject_] = null;
[_canvas_] = null
constructor(canvasObject) {
wx.getSystemInfo().then(({
pixelRatio
}) => {
this[_canvasObject_] = canvasObject
this[_pixelRatio_] = canvasObject.pixelRatio || pixelRatio || 2
const height = this.toRpx(canvasObject.height),
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,
this[_canvasObject_] = canvasObject
this[_pixelRatio_] = canvasObject.pixelRatio || 2
const height = this.toRpx(canvasObject.height),
width = this.toRpx(canvasObject.width),
canvas = wx.createOffscreenCanvas({
type: '2d',
width,
height,
color: canvasObject.backgroundColor || 'rgb(255, 255, 255)'
})
this[_ctx_].save()
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,
height,
color: canvasObject.backgroundColor || 'rgb(255, 255, 255)'
})
this[_ctx_].save()
}
drawImages(imageParams, callback) {
async drawImages(imageParams, callback) {
this[_ctx_].beginPath()
this[_ctx_].save()
let {
......@@ -60,13 +56,14 @@ class Draw {
this[_ctx_].clip()
}
let image = this[_canvas_].createImage()
new Promise(resolve => {
image.onload = resolve
image.src = `${url}?v=${Date.now()}`
}).then(() => {
this[_ctx_].drawImage(image, x, y, width, height)
this[_ctx_].restore()
if (callback) callback()
image.src = `${url}?v=${Date.now()}`
return new Promise(resolve => {
image.onload = () => {
this[_ctx_].drawImage(image, x, y, width, height)
this[_ctx_].restore()
if (callback) callback()
resolve(imageParams)
}
})
}
toRpx(val) {
......@@ -171,6 +168,7 @@ class Draw {
height: boxHeight,
color: textParams.background
})
}
const drawOneLineText = () => {
let textArr = text.toString().split(''),
......@@ -249,10 +247,7 @@ class Draw {
} else {
drawOneLineText()
}
} else {
drawOneLineText()
}
} else drawOneLineText()
this[_ctx_].stroke()
}
drawLine(lineParams, callback) {
......@@ -391,10 +386,33 @@ class Draw {
})
}
base64src(url).then(img => {
console.log('img', img)
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 => {
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!