Commit 6236ca27 by 李伟康

提交代码

1 parent f995062f
const _ctx_ = Symbol('_ctx_')
const _pixelRatio_ = Symbol('_pixelRatio_')
const _canvasObject_ = Symbol('_canvasObject_')
class Draw {
[_ctx_] = null;
[_pixelRatio_] = null
[_pixelRatio_] = null;
[_canvasObject_] = null;
constructor(_this_, canvasObject) {
this[_canvasObject_] = canvasObject
this[_ctx_] = _this_.ctx.getContext('2d')
this[_pixelRatio_] = window.devicePixelRatio || 2
let width = this.toRpx(canvasObject.width)
......@@ -15,6 +18,7 @@ class Draw {
this[_ctx_].fillRect(0, 0, width, height)
this[_ctx_].save()
}
drawImages (imageParams, callback) {
this[_ctx_].beginPath()
this[_ctx_].save()
......@@ -43,7 +47,6 @@ class Draw {
let img = new Image()
img.src = url
img.onload = () => {
console.log(this[_ctx_])
this[_ctx_].drawImage(img, x, y, width, height)
this[_ctx_].restore()
callback()
......@@ -76,17 +79,17 @@ class Draw {
lineHeight = this.toRpx(lineHeight || fontSize || 12)
textWidth = this.toRpx(textWidth || 0)
fontSize = this.toRpx(fontSize || 12)
maxWidth = this.toRpx(maxWidth || width)
width = this.toRpx(width)
maxWidth = this.toRpx(maxWidth || width || this[_canvasObject_].width)
width = this.toRpx(width || this[_canvasObject_].width)
x = this.toRpx(x)
y = this.toRpx(y)
this[_ctx_].beginPath()
this[_ctx_].globalAlpha = opacity
this[_ctx_].setFontSize(fontSize)
this[_ctx_].font = `${fontSize}px sans-serif`
this[_ctx_].fillStyle = color || '#000'
this[_ctx_].strokeStyle = color || '#000'
this[_ctx_].setTextBaseline(textBaseline)
this[_ctx_].setTextAlign(textAlign)
this[_ctx_].textBaseline = textBaseline
this[_ctx_].textAlign = textAlign
this[_ctx_].lineWidth = this.toRpx(2)
let fontX = this.getFontWidth({
textAlign,
......@@ -124,6 +127,8 @@ class Draw {
if (textWidth && text.length * fontSize + fontX > textWidth) maxLength = parseInt((width - textWidth - fontX) / fontSize) - 1
if (text.length * fontSize + fontX > width && !maxWidth) maxLength = parseInt((width - fontX) / fontSize) - 1
newText = maxLength !== -1 ? `${text.substring(0, maxLength)}...` : text
console.log('_____________', this[_ctx_], maxWidth)
this[_ctx_].fillText(newText, x, y, maxWidth)
if (checkFontWeight(fontWeight)) this[_ctx_].strokeText(newText, x, y, maxWidth)
if (checkTextDecoration(textDecoration)) {
......
......@@ -36,11 +36,30 @@ export default {
},
methods: {
drawOrderImg () {
let labelList = [
{
text: '姓名:',
x: 20,
y: 109,
fontSize: 14,
textBaseline: 'top',
textAlign: 'left',
}
]
this.ctx = document.getElementById('canvas')
const draw = new Draw(this, {
width: 343,
height: 422
})
setTimeout(() => {
labelList && labelList.length > 0 && (labelList.map(e => {
e.color = '#096'
draw.drawText(e)
}))
this.img = this.ctx.toDataURL('image/png')
}, 500)
return
draw.drawImages({
url: require('@/assets/images/order-bg.png'),
x: 0,
......@@ -48,19 +67,21 @@ export default {
width: 343,
height: 422,
}, () => {
labelList && labelList.length > 0 && (labelList.map(e => {
e.color = '#096'
draw.drawText(e)
}))
draw.drawLine({
startX: 20,
startY: 335,
endX: 303,
endX: 323,
endY: 335,
width: 10,
color: '#096'
width: 1,
color: '#F1F1F1'
})
})
setTimeout(() => {
this.ctx.getContext('2d').fillText('newText', 20, 109)
this.img = this.ctx.toDataURL('image/png')
}, 500)
})
}
}
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!