优化流程

This commit is contained in:
0JARVIS0 2019-11-20 11:10:21 +08:00
parent 67058f7997
commit fc86eeb33f
2 changed files with 21 additions and 23 deletions

View File

@ -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, let r1 = 0,
r2 = 0, r2 = 0,
r3 = 0, r3 = 0,
r4 = 0 r4 = 0
if (borderRadius) { if (borderRadius) {
const border = borderRadius.split(' ') const border = borderRadius.split(/\s+/)
if (border.length === 1) { if (border.length === 4) {
r1 = r2 = r3 = r4 = Math.min(borderRadius === '0' ? 0 : borderRadius.toPx(), width / 2, height / 2); r1 = Math.min(border[0].toPx(), width / 2, height / 2);
} else if (border.length === 4) { r2 = Math.min(border[1].toPx(), width / 2, height / 2);
r1 = Math.min(border[0] === '0' ? 0 : border[0].toPx(), width / 2, height / 2); r3 = Math.min(border[2].toPx(), width / 2, height / 2);
r2 = Math.min(border[1] === '0' ? 0 : border[1].toPx(), width / 2, height / 2); r4 = Math.min(border[3].toPx(), width / 2, height / 2);
r3 = Math.min(border[2] === '0' ? 0 : border[2].toPx(), width / 2, height / 2); } else {
r4 = Math.min(border[3] === '0' ? 0 : border[3].toPx(), width / 2, height / 2); 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; this.ctx.lineWidth = lineWidth;
if (borderStyle === 'dashed') { if (borderStyle === 'dashed') {
this.ctx.setLineDash([2 * lineWidth, 2 * lineWidth]); this.ctx.setLineDash([lineWidth * 4 / 3, lineWidth * 4 / 3]);
// this.ctx.lineDashOffset = 2 * lineWidth // this.ctx.lineDashOffset = 2 * lineWidth
} else if (borderStyle === 'dotted') { } else if (borderStyle === 'dotted') {
this.ctx.setLineDash([lineWidth, lineWidth]); 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' const notSolid = borderStyle !== 'solid'
this.ctx.beginPath(); this.ctx.beginPath();
@ -641,7 +636,7 @@ export default class Painter {
if (!view.css || !view.css.shadow) { if (!view.css || !view.css.shadow) {
return; return;
} }
const box = view.css.shadow.replace(/,\s+/g, ',').split(' '); const box = view.css.shadow.replace(/,\s+/g, ',').split(/\s+/);
if (box.length > 4) { if (box.length > 4) {
console.error('shadow don\'t spread option'); console.error('shadow don\'t spread option');
return; return;

View File

@ -27,7 +27,7 @@ Component({
}, },
palette: { palette: {
type: Object, type: Object,
observer: function (newVal, oldVal) { observer: function(newVal, oldVal) {
if (this.isNeedRefresh(newVal, oldVal)) { if (this.isNeedRefresh(newVal, oldVal)) {
this.paintCount = 0; this.paintCount = 0;
this.startPaint(); this.startPaint();
@ -36,7 +36,7 @@ Component({
}, },
dancePalette: { dancePalette: {
type: Object, type: Object,
observer: function (newVal, oldVal) { observer: function(newVal, oldVal) {
if (!this.isEmpty(newVal)) { if (!this.isEmpty(newVal)) {
this.initDancePalette(newVal); this.initDancePalette(newVal);
} }
@ -53,7 +53,7 @@ Component({
}, },
action: { action: {
type: Object, type: Object,
observer: function (newVal, oldVal) { observer: function(newVal, oldVal) {
if (newVal) { if (newVal) {
this.doAction(newVal) this.doAction(newVal)
} }
@ -624,10 +624,10 @@ Component({
setTimeout(() => { setTimeout(() => {
wx.canvasToTempFilePath({ wx.canvasToTempFilePath({
canvasId: 'photo', canvasId: 'photo',
success: function (res) { success: function(res) {
that.getImageInfo(res.tempFilePath); that.getImageInfo(res.tempFilePath);
}, },
fail: function (error) { fail: function(error) {
console.error(`canvasToTempFilePath failed, ${JSON.stringify(error)}`); console.error(`canvasToTempFilePath failed, ${JSON.stringify(error)}`);
that.triggerEvent('imgErr', { that.triggerEvent('imgErr', {
error: error error: error
@ -679,6 +679,9 @@ function setStringPrototype(screenK, scale) {
* @param {Boolean} minus 是否支持负数 * @param {Boolean} minus 是否支持负数
*/ */
String.prototype.toPx = function toPx(minus) { String.prototype.toPx = function toPx(minus) {
if (this === '0') {
return 0
}
let reg; let reg;
if (minus) { if (minus) {
reg = /^-?[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g; reg = /^-?[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g;
@ -701,4 +704,4 @@ function setStringPrototype(screenK, scale) {
} }
return res; return res;
}; };
} }