-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpointer3k.js
174 lines (133 loc) · 4.39 KB
/
pointer3k.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
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
/**
* POINTER3000
* http://github.com/ezekielaquino/Pointer3000
* JS Utility to get all sorts of pointer properties
* MIT License
*/
'use strict';
(function() {
let keys, propsKeys, points, propElems;
let isConsole, initialPos, listener, update;
const defaults = {
points: {
pointA: {
cx: window.innerWidth / 2,
cy: window.innerHeight / 2
}
}
};
window.Pointer3k = function(arg) {
// If passed a string, we return the
// object containing the named point
if (typeof arg === 'string' || Number.isInteger(arg)) {
return window.Pointer3k.points[arg];
} else if (typeof arg == 'boolean') {
// we 'pause' value updating
update = arg;
} else {
// If passed an object, then we initialise
// the plugin and register the points
arg = arg || defaults;
keys = Object.keys(arg.points);
points = arg.points || {};
isConsole = arg.console;
initialPos = arg.initialPos || { pageX: window.innerWidth / 2, pageY: window.innerHeight / 2 };
listener = arg.listener || 'mousemove';
update = true;
// initial register, before initial mousemove
// so we populate the values for the console
if (isConsole) {
register();
initializeConsole(keys);
}
// register with initial position
register(initialPos);
// Listen to mousemove within the viewport
window.addEventListener(listener, function(event) {
if (update) {
register(event);
}
});
}
};
function register(event) {
event = event || {};
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
let point;
let cx = points[key].cx == 'center' ? defaults.points['pointA'].cx : points[key].cx;
let cy = points[key].cy == 'center' ? defaults.points['pointA'].cy : points[key].cy;
let x = (event.pageX || 0) - cx;
let y = (event.pageY || 0) - cy;
let rad = Math.atan2(y, x);
let deg = (rad * (180 / Math.PI) + 360) % 360;
let dist = Math.hypot(x, y);
point = {
cx: cx,
cy: cy,
x: x,
y: y,
rad: rad,
deg: deg,
dist: dist
};
propsKeys = Object.keys(point);
points[key] = point;
updateConsole(point, i);
}
window.Pointer3k.points = points;
}
function initializeConsole(keys) {
if (isConsole) {
const container = document.createElement('div');
// Create the parent container
if (!document.querySelector('.js-pgProps')) {
container.classList.add('js-pgProps');
container.style.position = 'fixed';
container.style.background = 'rgba(0, 0, 0, 0.7)';
container.style.color = 'white';
container.style.fontSize = '9px';
container.style.fontFamily = 'Arial';
container.style.width = 140 + 'px';
container.style.top = 0;
container.style.right = 0;
document.body.appendChild(container);
}
// Create each point's container
if (document.querySelectorAll('.js-pgPoint').length < keys.length) {
for (var i = 0; i < keys.length; i++) {
// Initialize console
const key = keys[i];
const propKeys = Object.keys(window.Pointer3k.points[key]);
const pointInfo = document.createElement('div');
const pointLabel = document.createElement('label');
pointInfo.classList.add('js-pgPoint');
pointLabel.innerHTML = key;
pointLabel.style.padding = '2px 5px';
pointInfo.appendChild(pointLabel);
for (let n = 0; n < propKeys.length; n++) {
const prop = document.createElement('div');
const propKey = propKeys[n];
prop.classList.add('js-pgProp');
prop.style.padding = '2px 5px 2px 15px';
prop.innerHTML = propKey + ':' + window.Pointer3k.points[propKey];
pointInfo.appendChild(prop);
}
container.appendChild(pointInfo);
}
}
propElems = document.querySelectorAll('.js-pgProp');
}
}
function updateConsole(point, index) {
if (propElems) {
for (var j = 0; j < propsKeys.length; j++) {
const i = (index * propsKeys.length) + j;
const elem = propElems[i];
const key = propsKeys[j];
const value = point[key];
elem.innerHTML = key + ':' + value;
}
}
}
})();