From 7646ff55de385588116a15b2975cfa31b6e3c1b4 Mon Sep 17 00:00:00 2001 From: CPPAlien Date: Fri, 20 Jul 2018 15:57:33 +0800 Subject: [PATCH] =?UTF-8?q?top=20left=20right=20bottom=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=B4=9F=E6=95=B0=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pen.js | 8 ++++---- painter.js | 13 +++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/pen.js b/lib/pen.js index 9d25292..58e3303 100644 --- a/lib/pen.js +++ b/lib/pen.js @@ -116,8 +116,8 @@ export default class Painter { const lineHeight = view.css.lineHeight ? view.css.lineHeight.toPx() : view.css.fontSize.toPx(); height = lineHeight * lines; - x = view.css.right ? this.style.width - view.css.right.toPx() : (view.css.left ? view.css.left.toPx() : 0); - y = view.css.bottom ? this.style.height - height - view.css.bottom.toPx() : (view.css.top ? view.css.top.toPx() : 0); + x = view.css.right ? this.style.width - view.css.right.toPx(true) : (view.css.left ? view.css.left.toPx(true) : 0); + y = view.css.bottom ? this.style.height - height - view.css.bottom.toPx(true) : (view.css.top ? view.css.top.toPx(true) : 0); extra = { lines: lines, lineHeight: lineHeight }; } else { if (!(view.css.width && view.css.height)) { @@ -126,8 +126,8 @@ export default class Painter { } width = view.css.width.toPx(); height = view.css.height.toPx(); - x = view.css.right ? this.style.width - view.css.right.toPx() : (view.css.left ? view.css.left.toPx() : 0); - y = view.css.bottom ? this.style.height - height - view.css.bottom.toPx() : (view.css.top ? view.css.top.toPx() : 0); + x = view.css.right ? this.style.width - view.css.right.toPx(true) : (view.css.left ? view.css.left.toPx(true) : 0); + y = view.css.bottom ? this.style.height - height - view.css.bottom.toPx(true) : (view.css.top ? view.css.top.toPx(true) : 0); } const angle = view.css.rotate ? this._getAngle(view.css.rotate) : 0; // 当设置了 right 时,默认 align 用 right,反之用 left diff --git a/painter.js b/painter.js index 974e18e..31f5cff 100644 --- a/painter.js +++ b/painter.js @@ -188,8 +188,17 @@ let screenK = 0.5; function setStringPrototype() { /* eslint-disable no-extend-native */ - String.prototype.toPx = function toPx() { - const reg = /^[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g; + /** + * 是否支持负数 + * @param {Boolean} minus 是否支持负数 + */ + String.prototype.toPx = function toPx(minus) { + let reg; + if (minus) { + reg = /^-?[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g; + } else { + reg = /^[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g; + } const results = reg.exec(this); if (!this || !results) { console.error(`The size: ${this} is illegal`);