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; + } + }