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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | import * as is from '../is/is';
import * as mathf from '../mathf/mathf';
import {Vector} from '../mathf/vector';
import {DomWatcher} from './dom-watcher';
/**
* Class that helps with mouse tracking.
* TODO (uxder): This is from a really old class. I need to upgrade it.
* Could also still be buggy.
* @unstable
* @hidden
*/
export class MouseTracker {
/**
* The root element to calculate the center position of the effect.
*/
private rootElement_: Element;
/**
* The vector position of the mouse.
*/
public position: Vector;
/**
* The basic dimensions of the root element.
* @type {Object}
*/
private dimensions_: Record<string, number>;
/**
* The callback for when there is mouse movement.
*/
private moveCallBack: Function;
/**
* The current mouse position data.
*/
public mouseData: Object;
/**
* Instance of domWatcher.
*/
private watcher: DomWatcher;
/**
* The last known windowY offset.
*/
private windowY: number;
private lastWindowY: number;
private lastPosition: Vector;
/**
* @constructor
*/
constructor(
rootElement: Element,
moveCallBack: Function,
disableMobile: boolean
) {
this.rootElement_ = rootElement;
this.dimensions_ = {};
this.moveCallBack = moveCallBack;
this.mouseData = {};
this.position = Vector.ZERO;
this.windowY = window.scrollY;
this.lastWindowY = window.scrollY;
this.lastPosition = Vector.ZERO;
this.calculateRootElementDimensions_();
this.watcher = new DomWatcher();
this.watcher.add({
element: window,
on: 'resize',
callback: this.calculateRootElementDimensions_.bind(this),
eventOptions: {passive: true},
});
this.watcher.add({
element: window,
on: 'scroll',
callback: this.onScroll.bind(this),
eventOptions: {passive: true},
});
if (!disableMobile && is.supportingDeviceOrientation()) {
this.watcher.add({
element: window,
on: 'deviceorientation',
callback: this.onDeviceOrientation.bind(this),
eventOptions: {passive: true},
});
}
this.watcher.add({
element: document.body,
on: 'mousemove',
callback: this.onMouseMove.bind(this),
eventOptions: {passive: true},
});
this.watcher.add({
element: document.body,
on: 'touchmove',
callback: this.onMouseMove.bind(this),
eventOptions: {passive: true},
});
}
/**
* Calculates the base dimensions of the rootElement.
*/
calculateRootElementDimensions_() {
const docRect = document.body.getBoundingClientRect();
const rect = this.rootElement_.getBoundingClientRect();
// Calculate the center point.
const xCenter = rect.left + rect.width / 2;
const yCenter = rect.top + rect.height / 2;
this.dimensions_ = {
width: rect.width,
height: rect.height,
halfWidth: rect.width / 2,
halfHeight: rect.height / 2,
top: rect.top,
left: rect.left,
xCenter: xCenter,
yCenter: yCenter,
docWidth: docRect.width,
docHeight: docRect.height,
};
}
/**
* On scroll update the Y position so that the mouse
* position always updates.
*/
onScroll() {
this.windowY = window.scrollY;
const offset = this.windowY - this.lastWindowY;
const y = this.lastPosition.y + offset;
this.updateMouseData(this.lastPosition.x, y);
this.moveCallBack && this.moveCallBack(this.mouseData);
}
/**
* Handles the mouseRootElement move.
* @type {MouseEvent}
*/
private onMouseMove(e: MouseEvent) {
const x = e.pageX || e.clientX;
const y = e.pageY || e.clientY;
this.lastWindowY = window.scrollY;
this.lastPosition = this.position.clone();
this.updateMouseData(x, y);
this.moveCallBack && this.moveCallBack(this.mouseData);
}
private updateMouseData(x: number, y: number) {
this.position.x = x;
this.position.y = y;
this.mouseData = {
position: this.position,
x: x,
y: y,
deltaX: x - this.dimensions_.xCenter,
deltaY: y - this.dimensions_.yCenter,
percentageX:
((x - this.dimensions_.xCenter) / this.dimensions_.docWidth) * 100,
percentageY:
((y - this.dimensions_.yCenter) / this.dimensions_.docHeight) * 100,
};
}
/**
* Handles the device orientation.
* @type {MouseEvent}
*/
private onDeviceOrientation(event: DeviceOrientationEvent) {
const x = mathf.clamp(-50, 50, <number>event.gamma);
const y = mathf.clamp(-50, 50, <number>event.beta);
this.mouseData = {
x: 0,
y: 0,
deltaX: 0,
deltaY: 0,
percentageX: x,
percentageY: y,
};
this.moveCallBack && this.moveCallBack(this.mouseData);
}
/**
* Gets the mouse positions.
*/
getMousePosition(): Object {
return this.mouseData;
}
dispose() {
this.watcher.dispose();
}
}
|