From 7dac57c93bc89a725459f6e81a501f1a9544c494 Mon Sep 17 00:00:00 2001 From: CPPAlien Date: Tue, 10 Dec 2019 11:40:30 +0800 Subject: [PATCH] format and fix bugs --- lib/pen.js | 69 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/lib/pen.js b/lib/pen.js index 60b0707..f991d52 100644 --- a/lib/pen.js +++ b/lib/pen.js @@ -85,7 +85,13 @@ export default class Painter { } } - _border({ borderRadius = 0, width, height, borderWidth = 0, borderStyle = 'solid' }) { + _border({ + borderRadius = 0, + width, + height, + borderWidth = 0, + borderStyle = 'solid' + }) { let r1 = 0, r2 = 0, r3 = 0, @@ -144,7 +150,12 @@ export default class Painter { // globalAlpha 在 1.9.90 起支持,低版本下无效,但把 fillStyle 设为了 white,相对默认的 black 要好点 this.ctx.globalAlpha = 0; this.ctx.fillStyle = 'white'; - this._border({ borderRadius, width, height, borderStyle }) + this._border({ + borderRadius, + width, + height, + borderStyle + }) this.ctx.fill(); // 在 ios 的 6.6.6 版本上 clip 有 bug,禁掉此类型上的 clip,也就意味着,在此版本微信的 ios 设备下无法使用 border 属性 if (!(getApp().systemInfo && @@ -175,7 +186,13 @@ export default class Painter { this.ctx.save(); this._preProcess(view, true); this.ctx.strokeStyle = (borderColor || 'black'); - this._border({ borderRadius, width, height, borderWidth, borderStyle }) + this._border({ + borderRadius, + width, + height, + borderWidth, + borderStyle + }) this.ctx.stroke(); this.ctx.restore(); } @@ -196,7 +213,9 @@ export default class Painter { } const fontWeight = view.css.fontWeight === 'bold' ? 'bold' : 'normal'; const textStyle = view.css.textStyle === 'italic' ? 'italic' : 'normal'; - const fontSize = view.css.fontSize ? view.css.fontSize.toPx() : '20rpx'.toPx(); + if (!view.css.fontSize) { + view.css.fontSize = '20rpx'; + } this.ctx.font = `${textStyle} ${fontWeight} ${view.css.fontSize.toPx()}px ${view.css.fontFamily ? view.css.fontFamily : 'sans-serif'}`; // 计算行数 let lines = 0; @@ -335,11 +354,11 @@ export default class Painter { left = x - width } var top = y; - if (verticalAlign === 'center') { - top = y - height / 2; - } else if (verticalAlign === 'bottom') { - top = y - height - } + if (verticalAlign === 'center') { + top = y - height / 2; + } else if (verticalAlign === 'bottom') { + top = y - height + } view.rect = { left: left, top: top, @@ -348,7 +367,7 @@ export default class Painter { x: view.css && view.css.right ? x - width : x, y: y }; - + view.rect.left = view.rect.left - paddings[3]; view.rect.top = view.rect.top - paddings[0]; view.rect.right = view.rect.right + paddings[1]; @@ -547,7 +566,7 @@ export default class Painter { } let lineIndex = 0; for (let j = 0; j < textArray.length; ++j) { - const preLineLength = Math.floor(textArray[j].length / linesArray[j]); + const preLineLength = Math.ceil(textArray[j].length / linesArray[j]); let start = 0; let alreadyCount = 0; for (let i = 0; i < linesArray[j]; ++i) { @@ -638,7 +657,17 @@ export default class Painter { this.ctx.stroke(); } if (!this.isMoving) { - this.callbackInfo.lineArray ? this.callbackInfo.lineArray.push({ text, x, y, measuredWith }) : this.callbackInfo.lineArray = [{ text, x, y, measuredWith }] + this.callbackInfo.lineArray ? this.callbackInfo.lineArray.push({ + text, + x, + y, + measuredWith + }) : this.callbackInfo.lineArray = [{ + text, + x, + y, + measuredWith + }] } } } @@ -658,8 +687,18 @@ export default class Painter { } else { this.ctx.fillStyle = view.css.color; } - const { borderRadius, borderStyle, borderWidth } = view.css - this._border({ borderRadius, width, height, borderWidth, borderStyle }) + const { + borderRadius, + borderStyle, + borderWidth + } = view.css + this._border({ + borderRadius, + width, + height, + borderWidth, + borderStyle + }) this.ctx.fill(); this.ctx.restore(); this._doBorder(view, width, height); @@ -685,4 +724,4 @@ export default class Painter { _getAngle(angle) { return Number(angle) * Math.PI / 180; } -} +} \ No newline at end of file