2025-04-18 19:29:23 +08:00

54 lines
1.5 KiB
JavaScript

(function() {
var libutil= {};
libutil.isNumber=function(v) {
return typeof v === "number";
};
libutil.clamp=function(val, min, max) {
if(window.EnableDebugMode) {
if(max<min) {
console.warn("ibutil.clamp(): the max value is less than the min value");
var min1 = Math.min(min, max);
var max1 = Math.max(min, max);
return Math.min(Math.max(val, min1), max1);
}
}
return Math.min(Math.max(val, min), max);
};
libutil.calcresize=function(cw, ch, ew, eh) { //return=[width,height,left,top]
if(cw/ew<ch/eh) {
return [cw, cw/ew*eh, 0, (ch-cw/ew*eh)/2];
} else {
return [ch/eh*ew, ch, (cw-ch/eh*ew)/2, 0];
}
};
libutil.encodeHtmlSpecChars=function(html) {
var elem=document.createElement("div");
var txt=document.createTextNode(html);
elem.appendChild(txt);
return elem.innerHTML;
};
libutil.decodeHtmlSpecChars=function(str) {
var elem=document.createElement("div");
elem.innerHTML=str;
return elem.innerText||elem.textContent;
};
libutil.preloadFont=function(name) {
var elem=document.createElement("div");
elem.style.fontFamily=name;
elem.innerHTML=String(Math.random());
document.body.appendChild(elem);
setTimeout(function() {
document.body.removeChild(elem);
}, 0);
};
window.libutil=libutil;
})();