@spearwolf/twopoint5d
Version:
a library to create 2.5d realtime graphics and pixelart with three.js
36 lines • 1.33 kB
JavaScript
export const postFixID = Math.round(Math.random() * (1 << 24)).toString(16);
export const globalStylesID = `display3--${postFixID}`;
let sheet = null;
const installedRules = new Map();
export class Stylesheets {
static getGlobalSheet(root = document.head) {
if (sheet === null) {
const styleEl = document.createElement('style');
styleEl.setAttribute('id', globalStylesID);
root.appendChild(styleEl);
sheet = styleEl.sheet;
}
return sheet;
}
static installRule(name, css, root = document.head) {
const sheet = Stylesheets.getGlobalSheet(root);
const className = `${name}-${postFixID}`;
const selector = `.${className}`;
let index = sheet.cssRules.length;
if (installedRules.has(name)) {
const prevRule = installedRules.get(name);
if (prevRule.css === css) {
return className;
}
index = prevRule.index;
}
sheet.insertRule(`${selector} {${css}}`, index);
return className;
}
static addRule(element, name, css, root = document.head) {
const className = Stylesheets.installRule(name, css, root);
element.classList.add(className);
return className;
}
}
//# sourceMappingURL=Stylesheets.js.map