Commit 6236ca27 by 李伟康

提交代码

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