更新单位转换函数
This commit is contained in:
parent
5a78e68a26
commit
e8a9aa275c
24
painter.js
24
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user