@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
29 lines (28 loc) • 898 B
JavaScript
;
class MouseHelperClass {
constructor() {
this._rectByCanvas = /* @__PURE__ */ new Map();
this._resetCacheBound = this._resetCache.bind(this);
globalThis.addEventListener("resize", this._resetCacheBound);
document.addEventListener("scroll", this._resetCacheBound);
}
static instance() {
return this._instance = this._instance || new MouseHelperClass();
}
setEventOffset(cursorPage, canvas, offset) {
let rect = this._rectByCanvas.get(canvas);
if (!rect) {
rect = canvas.getBoundingClientRect();
this._rectByCanvas.set(canvas, rect);
}
offset.offsetX = cursorPage.clientX - rect.left;
offset.offsetY = cursorPage.clientY - rect.top;
}
_resetCache() {
this._rectByCanvas.clear();
}
resetCacheForCanvas(canvas) {
this._rectByCanvas.delete(canvas);
}
}
export const MouseHelper = MouseHelperClass.instance();