@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
48 lines (47 loc) • 1.56 kB
JavaScript
;
import { isVector2Valid } from "../Vector";
import { MouseHelper } from "./MouseHelper";
const _offset = { offsetX: 0, offsetY: 0 };
export class CursorHelper {
setCursorForCPU(context, target) {
this.setCursor(context, target);
target.x = target.x * 2 - 1;
target.y = -target.y * 2 + 1;
}
setCursorForGPU(context, target) {
this.setCursor(context, target);
target.y = 1 - target.y;
}
setCursor(context, target) {
var _a;
const canvas = (_a = context.viewer) == null ? void 0 : _a.canvas();
if (!canvas) {
return;
}
const event = context.event;
if (event instanceof PointerEvent || event instanceof MouseEvent || event instanceof DragEvent) {
MouseHelper.setEventOffset(event, canvas, _offset);
}
if (globalThis.TouchEvent && event instanceof TouchEvent) {
const touch = event.touches[0];
if (touch) {
MouseHelper.setEventOffset(touch, canvas, _offset);
}
}
this._updateFromCursor(canvas, target);
}
_updateFromCursor(canvas, target) {
if (canvas.offsetWidth <= 0 || canvas.offsetHeight <= 0) {
console.warn("_updateFromCursor: zero size canvas");
target.set(0, 0);
} else {
target.x = _offset.offsetX / canvas.offsetWidth;
target.y = _offset.offsetY / canvas.offsetHeight;
}
if (!isVector2Valid(target)) {
console.warn("invalid number detected");
console.warn(target.toArray(), _offset.offsetX, _offset.offsetY, canvas.offsetWidth, canvas.offsetHeight);
return;
}
}
}