From e8a9aa275c9afabf187079f0f469831ab16615ec Mon Sep 17 00:00:00 2001 From: Terry Cai Date: Sat, 26 Jan 2019 17:42:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8D=95=E4=BD=8D=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- painter.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/painter.js b/painter.js index 61b669d..c024c4a 100644 --- a/painter.js +++ b/painter.js @@ -235,4 +235,28 @@ function setStringPrototype() { } return res; }; + String.prototype.toRpx = function (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`); + return 0; + } + const unit = results[2]; + const value = parseFloat(this); + + let res = 0; + if (unit === 'rpx') { + res = Math.round(value); + } else if (unit === 'px') { + res = Math.round(value / screenK); + } + return res; + } + }