-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrackMouse.js
81 lines (62 loc) · 1.46 KB
/
TrackMouse.js
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
import {
throttle
} from 'min-dash';
/**
* Logs mouse coordinates and hover states to the console.
*
* @param {EventBus} eventBus
* @param {Canvas} canvas
*/
export default function TrackMouse(eventBus, canvas) {
var position;
var hover;
var changed = throttle(function changed() {
if (hover && position) {
console.log('TrackMouse', toLocalPoint(canvas, position), hover);
}
}, 500);
function updatePosition(_position) {
position = _position;
changed();
}
function updateHover(_hover) {
hover = _hover;
changed();
}
eventBus.on('element.hover', function(event) {
updateHover(event.element);
});
eventBus.on('element.out', function(event) {
updateHover(null);
});
canvas._container.addEventListener('mousemove', function(event) {
updatePosition({
x: event.clientX,
y: event.clientY
});
});
}
/**
* Add names of dependencies here for minification-safety.
*/
TrackMouse.$inject = [
'eventBus',
'canvas'
];
// helpers ////////////////
/**
* Convert global position to local coordinates.
*
* @param {Canvas} canvas
* @param {Point} globalPosition
*
* @return {Point}
*/
function toLocalPoint(canvas, globalPosition) {
var viewbox = canvas.viewbox();
var clientRect = canvas._container.getBoundingClientRect();
return {
x: viewbox.x + (globalPosition.x - clientRect.left) / viewbox.scale,
y: viewbox.y + (globalPosition.y - clientRect.top) / viewbox.scale
};
}