Commit 8de0cdad by 李伟康

提交代码

1 parent 6236ca27
...@@ -8,13 +8,13 @@ class Draw { ...@@ -8,13 +8,13 @@ class Draw {
constructor(_this_, canvasObject) { constructor(_this_, canvasObject) {
this[_canvasObject_] = canvasObject this[_canvasObject_] = canvasObject
this[_ctx_] = _this_.ctx.getContext('2d') this[_ctx_] = _this_.ctx.getContext('2d')
this[_pixelRatio_] = window.devicePixelRatio || 2 this[_pixelRatio_] = canvasObject.pixelRatio || window.devicePixelRatio || 2
let width = this.toRpx(canvasObject.width) let width = this.toRpx(canvasObject.width)
let height = this.toRpx(canvasObject.height) let height = this.toRpx(canvasObject.height)
_this_.ctx.width = width _this_.ctx.width = width
_this_.ctx.height = height _this_.ctx.height = height
this[_ctx_].clearRect(0, 0, width, height) this[_ctx_].clearRect(0, 0, width, height)
this[_ctx_].fillStyle = canvasObject.backgroundColor || '#fff' this[_ctx_].fillStyle = canvasObject.backgroundColor || 'transparent' || '#fff'
this[_ctx_].fillRect(0, 0, width, height) this[_ctx_].fillRect(0, 0, width, height)
this[_ctx_].save() this[_ctx_].save()
} }
...@@ -55,6 +55,9 @@ class Draw { ...@@ -55,6 +55,9 @@ class Draw {
toRpx (val) { toRpx (val) {
return this[_pixelRatio_] * val return this[_pixelRatio_] * val
} }
getTextWidth (text) {
return this[_ctx_].measureText(text).width
}
drawText (textParams) { drawText (textParams) {
let { let {
textDecoration, textDecoration,
...@@ -73,6 +76,7 @@ class Draw { ...@@ -73,6 +76,7 @@ class Draw {
x, x,
y, y,
} = textParams } = textParams
let text_width = this.getTextWidth(text)
textAlign = textAlign || 'left' textAlign = textAlign || 'left'
textBaseline = textBaseline || 'normal' textBaseline = textBaseline || 'normal'
opacity = typeof opacity === 'number' ? opacity : 1 opacity = typeof opacity === 'number' ? opacity : 1
...@@ -97,38 +101,12 @@ class Draw { ...@@ -97,38 +101,12 @@ class Draw {
text, text,
x, x,
}) })
if (lineNum > 1) { const drawOneLineText = () => {
if (text.length * fontSize + fontX > width) {
let maxLength = parseInt((width - fontX) / fontSize)
for (let i = 0; i < lineNum; i++) {
let newText = text.substring(i * maxLength, maxLength * i + maxLength)
if (i === lineNum - 1 && Math.ceil(text.length / maxLength) > lineNum) newText = `${newText.substring(0, maxLength - 1)}...`
let textHeight = y + i * (lineHeight || fontSize)
this[_ctx_].fillText(newText, x, textHeight, maxWidth)
if (checkFontWeight(fontWeight)) this[_ctx_].strokeText(newText, x, textHeight, maxWidth)
if (checkTextDecoration(textDecoration)) {
this.drawDecoration({
'y': textHeight,
textDecoration,
textBaseline,
textAlign,
fontSize,
newText,
opacity,
text,
x
})
}
}
}
} else {
let newText = text let newText = text
let maxLength = -1 let maxLength = -1
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)) {
...@@ -145,6 +123,57 @@ class Draw { ...@@ -145,6 +123,57 @@ class Draw {
}) })
} }
} }
if (lineNum > 1) {
let textArr = text.split(''),
textRowArr = [],
currentText = '',
viewWidth = width - fontX
if (text_width > viewWidth) {
textArr.map(e => {
currentText += e
if (this.getTextWidth(currentText) >= viewWidth && textRowArr.length < lineNum - 1) {
textRowArr.push(currentText)
currentText = ''
}
})
if (currentText.length) textRowArr.push(currentText)
textRowArr.map((e, index) => {
if (this.getTextWidth(e) > viewWidth && index == lineNum - 1) {
try {
let lastText = '', lastArr = e.split('')
lastArr.map(i => {
lastText += i
if (this.getTextWidth(lastText) > width - fontX - this.getTextWidth('...') - fontSize) throw lastText
})
} catch (error) {
e = `${error}...`
}
}
let textHeight = y + index * (lineHeight || fontSize)
this[_ctx_].fillText(e, x, textHeight, maxWidth)
if (checkFontWeight(fontWeight)) this[_ctx_].strokeText(e, x, textHeight, maxWidth)
if (checkTextDecoration(textDecoration)) {
this.drawDecoration({
'y': textHeight,
textDecoration,
textBaseline,
textAlign,
fontSize,
newText: e,
opacity,
text,
x
})
}
})
} else {
drawOneLineText()
}
} else {
drawOneLineText()
}
this[_ctx_].stroke() this[_ctx_].stroke()
} }
drawLine (lineParams) { drawLine (lineParams) {
...@@ -180,51 +209,49 @@ class Draw { ...@@ -180,51 +209,49 @@ class Draw {
y y
} = params } = params
let type = { let type = {
'underline': { underline: {
'top': y + fontSize, top: y + fontSize,
'middle': y + fontSize / 2, middle: y + fontSize / 2,
'bottom': y, bottom: y,
'normal': y, normal: y,
}, },
'overline': { overline: {
'top': y, top: y,
'middle': y - fontSize / 2, middle: y - fontSize / 2,
'bottom': y - fontSize, bottom: y - fontSize,
'normal': y, normal: y,
}, },
'line-through': { 'line-through': {
'top': y + fontSize / 2, top: y + fontSize / 2,
'middle': y - fontSize / 2, middle: y - fontSize / 2,
'bottom': y, bottom: y,
'normal': y, normal: y,
} }
} }
return type[textDecoration][textBaseline] return type[textDecoration][textBaseline]
} }
setLineX (params) { setLineX ({
let { textAlign,
textAlign, text,
text, x
x }) {
} = params
let textWidth = this[_ctx_].measureText(text).width let textWidth = this[_ctx_].measureText(text).width
let type = { let type = {
'left': x, left: x,
'center': x - textWidth / 2, center: x - textWidth / 2,
'right': x - textWidth right: x - textWidth
} }
return type[textAlign] return type[textAlign]
} }
drawRadiusRect (params) { drawRadiusRect ({
let { borderRadius,
borderRadius, height,
height, width,
width, x,
x, y,
y, }) {
} = params
let radius = typeof borderRadius === 'string' ? (Number(borderRadius.split('%')[0]) / 100 * width) / 2 : borderRadius / 2 let radius = typeof borderRadius === 'string' ? (Number(borderRadius.split('%')[0]) / 100 * width) / 2 : borderRadius / 2
radius = radius || 0 radius = radius || 0
this[_ctx_].beginPath() this[_ctx_].beginPath()
...@@ -234,17 +261,16 @@ class Draw { ...@@ -234,17 +261,16 @@ class Draw {
this[_ctx_].arc(arcX, arcY, radius, 2 * Math.PI * ((i - 1) / 4), 2 * Math.PI * (i / 4)) this[_ctx_].arc(arcX, arcY, radius, 2 * Math.PI * ((i - 1) / 4), 2 * Math.PI * (i / 4))
} }
} }
getFontWidth (params) { getFontWidth ({
let { textAlign,
textAlign, fontSize,
fontSize, text,
text, x
x }) {
} = params
let type = { let type = {
'left': x, left: x,
'center': x - fontSize * text.length / 2, center: x - fontSize * text.length / 2,
'right': x - fontSize * text.length right: x - fontSize * text.length
} }
return type[textAlign] < 0 ? 0 : type[textAlign] return type[textAlign] < 0 ? 0 : type[textAlign]
} }
...@@ -264,10 +290,6 @@ class Draw { ...@@ -264,10 +290,6 @@ class Draw {
this[_ctx_].moveTo(lineX, lineY) this[_ctx_].moveTo(lineX, lineY)
this[_ctx_].lineTo(lineX + this[_ctx_].measureText(newText).width, lineY) this[_ctx_].lineTo(lineX + this[_ctx_].measureText(newText).width, lineY)
} }
drawCanvas (callback) {
this[_ctx_].draw(false, callback)
}
} }
const checkFontWeight = (fontWeight = '0') => { const checkFontWeight = (fontWeight = '0') => {
return (fontWeight && (typeof fontWeight === 'string' || typeof fontWeight === 'number') && (fontWeight == 'bold' || fontWeight >= 700)) return (fontWeight && (typeof fontWeight === 'string' || typeof fontWeight === 'number') && (fontWeight == 'bold' || fontWeight >= 700))
......
...@@ -36,30 +36,21 @@ export default { ...@@ -36,30 +36,21 @@ export default {
}, },
methods: { methods: {
drawOrderImg () { drawOrderImg () {
let labelList = [ let labelList = ['姓名:', '手机:', '车牌号:', '品牌车型:', '商品名称:', '支付时间:', '支付定金']
{ let valueList = ['恒河猴',
text: '姓名:', '18888888888',
x: 20, '粤B12345',
y: 109, // '阿尔法・罗密欧阿尔法・罗密欧166 2004款 2.0T Spark 苟利国家生死岂因祸福避趋之',
fontSize: 14, '奥迪 奥迪A6L 2022款 55 TFSI quattro 尊享动感型',
textBaseline: 'top', '氛围灯', '2022年8月3日 17:43:48', '199元']
textAlign: 'left', let textYList = [109, 141, 173, 205, 261, 293, 352]
}
]
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,
pixelRatio: 1
}) })
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,
...@@ -67,9 +58,39 @@ export default { ...@@ -67,9 +58,39 @@ export default {
width: 343, width: 343,
height: 422, height: 422,
}, () => { }, () => {
labelList && labelList.length > 0 && (labelList.map(e => { labelList && labelList.length > 0 && (labelList.map((e, index) => {
e.color = '#096' let y = textYList[index] + 7,
draw.drawText(e) fontSize = 14,
textBaseline = 'top'
let label = {
text: e,
x: 20,
y,
color: '#999',
fontSize,
textBaseline
}
let value = {
text: valueList[index],
x: 98,
y,
color: '#282829',
fontSize,
textBaseline,
width: 303
}
if (e === '品牌车型:') {
value.lineNum = 2
value.lineHeight = 24
value.lineTextLength = 20
}
if (e === '支付定金') {
label.color = '#282829'
value.color = '#3162E6'
value.fontWeight = '600'
}
draw.drawText(label)
draw.drawText(value)
})) }))
draw.drawLine({ draw.drawLine({
startX: 20, startX: 20,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!