@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
43 lines (42 loc) • 1.36 kB
JavaScript
"use strict";
import { Vector2 } from "three";
const _cursorDelta = new Vector2();
const _currentCursorPos = new Vector2();
export class CursorMoveMonitor {
constructor() {
this._lastCursorPosSet = false;
this._movedCursorDistance = 0;
this._lastCursorPos = new Vector2();
this._bound = {
pointermove: this._onPointermove.bind(this)
};
}
addPointermoveEventListener(cursorRef) {
this.cursorRef = cursorRef;
this._movedCursorDistance = 0;
this._lastCursorPosSet = false;
document.addEventListener("pointermove", this._bound.pointermove);
document.addEventListener("touchmove", this._bound.pointermove);
}
removeEventListener() {
document.removeEventListener("pointermove", this._bound.pointermove);
document.removeEventListener("touchmove", this._bound.pointermove);
}
movedCursorDistance() {
return this._movedCursorDistance;
}
_onPointermove() {
if (!this.cursorRef) {
return;
}
const cursor = this.cursorRef.value;
if (this._lastCursorPosSet == false) {
this._lastCursorPos.copy(cursor);
this._lastCursorPosSet = true;
}
_currentCursorPos.copy(cursor);
_cursorDelta.copy(_currentCursorPos).sub(this._lastCursorPos);
this._movedCursorDistance += _cursorDelta.manhattanLength() / 2;
this._lastCursorPos.copy(_currentCursorPos);
}
}