From fc86eeb33fa378e7e9d0dead879ba97b0cfab159 Mon Sep 17 00:00:00 2001 From: 0JARVIS0 <709406687@qq.com> Date: Wed, 20 Nov 2019 11:10:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pen.js | 29 ++++++++++++----------------- painter.js | 15 +++++++++------ 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/lib/pen.js b/lib/pen.js index 45c07f1..2e1383b 100644 --- a/lib/pen.js +++ b/lib/pen.js @@ -81,34 +81,29 @@ 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, r4 = 0 if (borderRadius) { - const border = borderRadius.split(' ') - if (border.length === 1) { - r1 = r2 = r3 = r4 = Math.min(borderRadius === '0' ? 0 : borderRadius.toPx(), width / 2, height / 2); - } else if (border.length === 4) { - r1 = Math.min(border[0] === '0' ? 0 : border[0].toPx(), width / 2, height / 2); - r2 = Math.min(border[1] === '0' ? 0 : border[1].toPx(), width / 2, height / 2); - r3 = Math.min(border[2] === '0' ? 0 : border[2].toPx(), width / 2, height / 2); - r4 = Math.min(border[3] === '0' ? 0 : border[3].toPx(), width / 2, height / 2); + const border = borderRadius.split(/\s+/) + if (border.length === 4) { + r1 = Math.min(border[0].toPx(), width / 2, height / 2); + r2 = Math.min(border[1].toPx(), width / 2, height / 2); + r3 = Math.min(border[2].toPx(), width / 2, height / 2); + r4 = Math.min(border[3].toPx(), width / 2, height / 2); + } else { + r1 = r2 = r3 = r4 = Math.min(borderRadius && borderRadius.toPx(), width / 2, height / 2); } } - const lineWidth = borderWidth === '0' ? 0 : borderWidth.toPx(); + const lineWidth = borderWidth && borderWidth.toPx(); this.ctx.lineWidth = lineWidth; if (borderStyle === 'dashed') { - this.ctx.setLineDash([2 * lineWidth, 2 * lineWidth]); + this.ctx.setLineDash([lineWidth * 4 / 3, lineWidth * 4 / 3]); // this.ctx.lineDashOffset = 2 * lineWidth } else if (borderStyle === 'dotted') { this.ctx.setLineDash([lineWidth, lineWidth]); - } else if (borderStyle !== 'solid') { - const segments = borderStyle.split(/\s+/) - if (segments.length === 2) { - this.ctx.setLineDash([segments[0].toPx(), segments[1].toPx()]) - } } const notSolid = borderStyle !== 'solid' this.ctx.beginPath(); @@ -641,7 +636,7 @@ export default class Painter { if (!view.css || !view.css.shadow) { return; } - const box = view.css.shadow.replace(/,\s+/g, ',').split(' '); + const box = view.css.shadow.replace(/,\s+/g, ',').split(/\s+/); if (box.length > 4) { console.error('shadow don\'t spread option'); return; diff --git a/painter.js b/painter.js index c8f8c96..b52b6bc 100644 --- a/painter.js +++ b/painter.js @@ -27,7 +27,7 @@ Component({ }, palette: { type: Object, - observer: function (newVal, oldVal) { + observer: function(newVal, oldVal) { if (this.isNeedRefresh(newVal, oldVal)) { this.paintCount = 0; this.startPaint(); @@ -36,7 +36,7 @@ Component({ }, dancePalette: { type: Object, - observer: function (newVal, oldVal) { + observer: function(newVal, oldVal) { if (!this.isEmpty(newVal)) { this.initDancePalette(newVal); } @@ -53,7 +53,7 @@ Component({ }, action: { type: Object, - observer: function (newVal, oldVal) { + observer: function(newVal, oldVal) { if (newVal) { this.doAction(newVal) } @@ -624,10 +624,10 @@ Component({ setTimeout(() => { wx.canvasToTempFilePath({ canvasId: 'photo', - success: function (res) { + success: function(res) { that.getImageInfo(res.tempFilePath); }, - fail: function (error) { + fail: function(error) { console.error(`canvasToTempFilePath failed, ${JSON.stringify(error)}`); that.triggerEvent('imgErr', { error: error @@ -679,6 +679,9 @@ function setStringPrototype(screenK, scale) { * @param {Boolean} minus 是否支持负数 */ String.prototype.toPx = function toPx(minus) { + if (this === '0') { + return 0 + } let reg; if (minus) { reg = /^-?[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g; @@ -701,4 +704,4 @@ function setStringPrototype(screenK, scale) { } return res; }; -} \ No newline at end of file +}