Commit 8de0cdad by 李伟康

提交代码

1 parent 6236ca27
......@@ -8,13 +8,13 @@ class Draw {
constructor(_this_, canvasObject) {
this[_canvasObject_] = canvasObject
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 height = this.toRpx(canvasObject.height)
_this_.ctx.width = width
_this_.ctx.height = 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_].save()
}
......@@ -55,6 +55,9 @@ class Draw {
toRpx (val) {
return this[_pixelRatio_] * val
}
getTextWidth (text) {
return this[_ctx_].measureText(text).width
}
drawText (textParams) {
let {
textDecoration,
......@@ -73,6 +76,7 @@ class Draw {
x,
y,
} = textParams
let text_width = this.getTextWidth(text)
textAlign = textAlign || 'left'
textBaseline = textBaseline || 'normal'
opacity = typeof opacity === 'number' ? opacity : 1
......@@ -97,38 +101,12 @@ class Draw {
text,
x,
})
if (lineNum > 1) {
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 {
const drawOneLineText = () => {
let newText = text
let maxLength = -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
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)) {
......@@ -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()
}
drawLine (lineParams) {
......@@ -180,51 +209,49 @@ class Draw {
y
} = params
let type = {
'underline': {
'top': y + fontSize,
'middle': y + fontSize / 2,
'bottom': y,
'normal': y,
underline: {
top: y + fontSize,
middle: y + fontSize / 2,
bottom: y,
normal: y,
},
'overline': {
'top': y,
'middle': y - fontSize / 2,
'bottom': y - fontSize,
'normal': y,
overline: {
top: y,
middle: y - fontSize / 2,
bottom: y - fontSize,
normal: y,
},
'line-through': {
'top': y + fontSize / 2,
'middle': y - fontSize / 2,
'bottom': y,
'normal': y,
top: y + fontSize / 2,
middle: y - fontSize / 2,
bottom: y,
normal: y,
}
}
return type[textDecoration][textBaseline]
}
setLineX (params) {
let {
textAlign,
text,
x
} = params
setLineX ({
textAlign,
text,
x
}) {
let textWidth = this[_ctx_].measureText(text).width
let type = {
'left': x,
'center': x - textWidth / 2,
'right': x - textWidth
left: x,
center: x - textWidth / 2,
right: x - textWidth
}
return type[textAlign]
}
drawRadiusRect (params) {
let {
borderRadius,
height,
width,
x,
y,
} = params
drawRadiusRect ({
borderRadius,
height,
width,
x,
y,
}) {
let radius = typeof borderRadius === 'string' ? (Number(borderRadius.split('%')[0]) / 100 * width) / 2 : borderRadius / 2
radius = radius || 0
this[_ctx_].beginPath()
......@@ -234,17 +261,16 @@ class Draw {
this[_ctx_].arc(arcX, arcY, radius, 2 * Math.PI * ((i - 1) / 4), 2 * Math.PI * (i / 4))
}
}
getFontWidth (params) {
let {
textAlign,
fontSize,
text,
x
} = params
getFontWidth ({
textAlign,
fontSize,
text,
x
}) {
let type = {
'left': x,
'center': x - fontSize * text.length / 2,
'right': x - fontSize * text.length
left: x,
center: x - fontSize * text.length / 2,
right: x - fontSize * text.length
}
return type[textAlign] < 0 ? 0 : type[textAlign]
}
......@@ -264,10 +290,6 @@ class Draw {
this[_ctx_].moveTo(lineX, lineY)
this[_ctx_].lineTo(lineX + this[_ctx_].measureText(newText).width, lineY)
}
drawCanvas (callback) {
this[_ctx_].draw(false, callback)
}
}
const checkFontWeight = (fontWeight = '0') => {
return (fontWeight && (typeof fontWeight === 'string' || typeof fontWeight === 'number') && (fontWeight == 'bold' || fontWeight >= 700))
......
......@@ -36,30 +36,21 @@ export default {
},
methods: {
drawOrderImg () {
let labelList = [
{
text: '姓名:',
x: 20,
y: 109,
fontSize: 14,
textBaseline: 'top',
textAlign: 'left',
}
]
let labelList = ['姓名:', '手机:', '车牌号:', '品牌车型:', '商品名称:', '支付时间:', '支付定金']
let valueList = ['恒河猴',
'18888888888',
'粤B12345',
// '阿尔法・罗密欧阿尔法・罗密欧166 2004款 2.0T Spark 苟利国家生死岂因祸福避趋之',
'奥迪 奥迪A6L 2022款 55 TFSI quattro 尊享动感型',
'氛围灯', '2022年8月3日 17:43:48', '199元']
let textYList = [109, 141, 173, 205, 261, 293, 352]
this.ctx = document.getElementById('canvas')
const draw = new Draw(this, {
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({
url: require('@/assets/images/order-bg.png'),
x: 0,
......@@ -67,9 +58,39 @@ export default {
width: 343,
height: 422,
}, () => {
labelList && labelList.length > 0 && (labelList.map(e => {
e.color = '#096'
draw.drawText(e)
labelList && labelList.length > 0 && (labelList.map((e, index) => {
let y = textYList[index] + 7,
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({
startX: 20,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!