Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 1x 1x 1x 1x | import {DomWatcher} from '../dom/dom-watcher';
import {Vector} from '../mathf/vector';
import {Raf} from '../raf/raf';
import {
type EventCallback,
type EventDispatcher,
EventManager,
} from '../ui/events';
const CURSOR_MOVE_EVENTS: string[] = ['mousemove', 'touchstart', 'touchmove'];
export enum CachedMouseTrackerEvent {
UPDATE = 'update',
}
/**
* Interface filled by either mousemove or touchmove events.
*/
interface ClientPositionData {
clientX: number;
clientY: number;
}
/**
* Tracks the client mouse position per frame.
*
* This improves performance and ensures that all frame-synced animations are
* always using the same position value by delaying mouse position updates until
* the preRead step of the RAF loop.
*
* If needed could be extended to:
* - Track the page position and screen position.
* - Track whether or not the mouse is pressed.
* - Track velocity and gestures.
*
* If any of these needs arise this can serve as a reference:
* https://github.com/angusm/toolbox-v2/blob/130326ee6b26e652d4bf7442c65ffbfeccc31649/src/toolbox/utils/cached-vectors/cursor.ts
*/
export class CachedMouseTracker implements EventDispatcher {
// Use static variables so that independent instances can operate as a
// singleton but be disposed separately without the user needing to worry
// about the singleton pattern.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static uses: Set<any> = new Set();
private static nextClientPosition: Vector | null;
private static clientPosition: Vector | null;
private static domWatcher: DomWatcher;
private static raf: Raf;
private static eventManager: EventManager;
/**
* Updates the currently tracked position from either a touch event or a mouse
* event.
* @param event
*/
private static updatePosition(event: Event): void {
if (event instanceof MouseEvent) {
this.updatePositionFromEvent(event);
} else if (event instanceof TouchEvent) {
this.updatePositionFromTouchEvent(event);
}
}
/**
* Update the tracked position from the touch event.
* @param touchEvent
*/
private static updatePositionFromTouchEvent(touchEvent: TouchEvent): void {
if (touchEvent.touches.length > 0) {
this.updatePositionFromEvent(touchEvent.touches[0]);
}
}
/**
* Update the tracked position using event information.
* @param data
*/
private static updatePositionFromEvent(data: ClientPositionData): void {
this.nextClientPosition = new Vector(data.clientX, data.clientY);
}
private static loop(): void {
this.raf.preRead(() => {
if (
this.nextClientPosition &&
this.clientPosition?.equals(this.nextClientPosition)
) {
return;
}
this.clientPosition = this.nextClientPosition;
this.eventManager.dispatch(CachedMouseTrackerEvent.UPDATE);
});
}
constructor() {
if (CachedMouseTracker.uses.size === 0) {
CachedMouseTracker.raf = new Raf(() => CachedMouseTracker.loop());
CachedMouseTracker.clientPosition = Vector.ZERO;
CachedMouseTracker.nextClientPosition = Vector.ZERO;
CachedMouseTracker.domWatcher = new DomWatcher();
CachedMouseTracker.eventManager = new EventManager();
CURSOR_MOVE_EVENTS.forEach((cursorMoveEvent) => {
CachedMouseTracker.domWatcher.add({
element: window,
on: cursorMoveEvent,
eventOptions: {passive: true},
callback: (e: Event) => CachedMouseTracker.updatePosition(e),
});
});
CachedMouseTracker.raf.start();
}
CachedMouseTracker.uses.add(this);
}
/**
* Add a callback to the given event.
*/
on(event: string, callback: EventCallback): void {
CachedMouseTracker.eventManager.on(event, callback);
}
/**
* Remove a callback from the given event.
*/
off(event: string, callback: EventCallback): void {
CachedMouseTracker.eventManager.off(event, callback);
}
/**
* Return the current cached mouse position as determined by clientX and
* clientY values.
*/
getClientPosition(): Vector | null {
return CachedMouseTracker.clientPosition;
}
/**
* Dispose of the currently in use tracker.
*/
dispose(): void {
CachedMouseTracker.uses.delete(this);
if (CachedMouseTracker.uses.size === 0) {
CachedMouseTracker.domWatcher.dispose();
CachedMouseTracker.raf.dispose();
CachedMouseTracker.clientPosition = null;
CachedMouseTracker.eventManager.dispose();
}
}
}
|