-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
374 lines (314 loc) · 11.5 KB
/
index.html
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<link rel="icon" href="data:,">
<title>popover-helper</title>
<link href="./style.css" rel="stylesheet" />
<!--inject:start-->
<script src="../dist/popover-helper.js"></script>
<!--inject:end-->
</head>
<body>
<h3>popover settings:</h3>
<div>
border color:
<input type="text" value="#ccc" class="popover-border-color" />
bg color:
<input type="text" value="#fff" class="popover-bg-color" />
color:
<input type="text" value="#000" class="popover-color" />
arrow size:
<input type="number" step="1" min="0" value="10" class="popover-arrow-size" />
</div>
<div>
positions:
<span class="positions"></span>
</div>
<div class="container">
<div class="target">target (move me)</div>
</div>
<div class="popover">
<div class="popover-content">popover content popover content popover content
popover content popover content popover content
popover content popover content popover content
popover content popover content popover content
popover content popover content popover content
popover content popover content popover content
popover content popover content popover content
</div>
</div>
<div>
<button class="random">random target</button>
</div>
<h3>size settings:</h3>
<div>
popover width/height:
<input type="text" value="300px" class="popover-width" size="10" />
x
<input type="text" value="200px" class="popover-height" size="10" />
</div>
<div>
container width/height:
<input type="text" value="800px" class="container-width" size="10" />
x
<input type="text" value="600px" class="container-height" size="10" />
</div>
<div>
target width/height:
<input type="text" value="120px" class="target-width" size="10" />
x
<input type="text" value="30px" class="target-height" size="10" />
</div>
<div>
target rect:
<select class="target-rect">
<option></option>
<option>x: 100, y: 100, width: 0, height: 0</option>
<option>x: 100, y: 100, width: 100, height: 30</option>
<option>x: 200, y: 100, width: 200, height: 100</option>
<option>x: "mouseX", y: "mouseY", width: 0, height: 0</option>
<option>x: "mouseX", y: "mouseY", width: 100, height: 30</option>
<option>x: "mouseX", y: "mouseY", width: 200, height: 100</option>
</select>
</div>
<div class="fps-detector"></div>
<script>
const {
getRect, getDefaultPositions, getBestPosition, getPositionStyle
} = window['popover-helper'];
// ===========================================================================================
const pos = localStorage.getItem('popover-positions') || '';
let positions = pos.split(',');
// all positions
const $positions = document.querySelector('.positions');
const defaultPositions = getDefaultPositions();
defaultPositions.forEach((item) => {
const label = document.createElement('label');
const input = document.createElement('input');
input.className = 'position';
input.setAttribute('type', 'checkbox');
input.setAttribute('value', item);
if (positions.includes(item)) {
input.setAttribute('checked', 'checked');
}
label.appendChild(input);
const span = document.createElement('span');
span.innerText = item;
label.appendChild(span);
$positions.appendChild(label);
});
const updatePositions = () => {
positions = [];
Array.from(document.querySelectorAll('.position')).forEach((elem) => {
if (elem.checked) {
positions.push(elem.value);
}
});
// console.log(positions);
localStorage.setItem('popover-positions', positions.join(','));
};
updatePositions();
$positions.addEventListener('click', () => {
updatePositions();
update();
});
// ===========================================================================================
const $container = document.querySelector('.container');
const $target = document.querySelector('.target');
const $popover = document.querySelector('.popover');
let positionInfo;
let virtualTargetRect;
// const log = (name, time) => {
// if (time > 0.5) {
// console.log(name, time);
// }
// };
const update = () => {
const bgColor = $popover.style.getPropertyValue('--popover-bg-color');
const borderColor = $popover.style.getPropertyValue('--popover-border-color');
const arrowSize = $popover.style.getPropertyValue('--popover-arrow-size');
// let start_time = performance.now();
// get 3 rect for calculation
const containerRect = getRect('.container');
const targetRect = getRect(virtualTargetRect || '.target');
const popoverRect = getRect('.popover');
// log('get rect', performance.now() - start_time);
// start_time = performance.now();
// console.log(positions);
positionInfo = getBestPosition(
containerRect,
targetRect,
popoverRect,
// allowed positions
positions,
// previous position info
positionInfo
);
// log('getBestPosition', performance.now() - start_time);
// start_time = performance.now();
// console.log(positionInfo.changed);
if (positionInfo.changed) {
$popover.style.left = `${positionInfo.left}px`;
$popover.style.top = `${positionInfo.top}px`;
// log('style top/left', performance.now() - start_time);
// start_time = performance.now();
}
const style = getPositionStyle(positionInfo, {
bgColor,
borderColor,
arrowSize: parseInt(arrowSize)
});
// log('getPositionStyle', performance.now() - start_time);
// start_time = performance.now();
// console.log(style.changed);
// performance optimized
if (style.changed) {
$popover.style.background = style.background;
// log('background', performance.now() - start_time);
}
};
// ===========================================================================================
const moveInfo = {
mx: 0,
my: 0,
tx: 0,
ty: 0
};
const updateTarget = (x, y) => {
if (x) {
localStorage.setItem('target-x', x);
} else {
x = localStorage.getItem('target-x');
}
if (y) {
localStorage.setItem('target-y', y);
} else {
y = localStorage.getItem('target-y');
}
$target.style.left = `${x}px`;
$target.style.top = `${y}px`;
};
updateTarget();
const mousemoveHandler = (e) => {
e.preventDefault();
const x = e.pageX - moveInfo.mx + moveInfo.tx;
const y = e.pageY - moveInfo.my + moveInfo.ty;
// console.log(x, y);
updateTarget(x, y);
update();
};
const mouseupHandler = (e) => {
document.removeEventListener('mousemove', mousemoveHandler);
document.removeEventListener('mouseup', mouseupHandler);
};
$target.addEventListener('mousedown', (e) => {
moveInfo.mx = e.pageX;
moveInfo.my = e.pageY;
moveInfo.tx = e.target.offsetLeft;
moveInfo.ty = e.target.offsetTop;
// console.log(moveInfo);
document.addEventListener('mousemove', mousemoveHandler);
document.addEventListener('mouseup', mouseupHandler);
});
let $virtualTarget;
const updateVirtualTarget = () => {
if (!virtualTargetRect) {
if ($virtualTarget) {
$virtualTarget.remove();
$virtualTarget = null;
}
return;
}
if (!$virtualTarget) {
$virtualTarget = document.createElement('div');
$virtualTarget.className = 'virtual-target';
document.body.appendChild($virtualTarget);
}
$virtualTarget.style.left = `${virtualTargetRect.x}px`;
$virtualTarget.style.top = `${virtualTargetRect.y}px`;
$virtualTarget.style.width = `${virtualTargetRect.width}px`;
$virtualTarget.style.height = `${virtualTargetRect.height}px`;
};
const virtualMouseMoveHandler = (e) => {
virtualTargetRect.x = e.pageX;
virtualTargetRect.y = e.pageY;
updateVirtualTarget();
update();
};
const virtualTargetHandler = (v) => {
if (!v) {
document.removeEventListener('mousemove', virtualMouseMoveHandler);
virtualTargetRect = null;
updateVirtualTarget();
update();
return;
}
virtualTargetRect = new Function(`return {${v}}`)();
console.log('virtualTargetRect', virtualTargetRect);
if (virtualTargetRect.x === 'mouseX') {
document.addEventListener('mousemove', virtualMouseMoveHandler);
} else {
document.removeEventListener('mousemove', virtualMouseMoveHandler);
updateVirtualTarget();
update();
}
};
const $targetRect = document.querySelector('.target-rect');
$targetRect.addEventListener('change', (e) => {
virtualTargetHandler(e.target.value);
});
// ===========================================================================================
// bind
const controls = [
'popover-border-color',
'popover-bg-color',
'popover-color',
'popover-arrow-size',
'popover-width',
'popover-height',
'container-width',
'container-height',
'target-width',
'target-height'
];
controls.forEach((id) => {
const $elem = document.querySelector(`.${id}`);
const doms = {
popover: $popover,
container: $container,
target: $target
};
const $dom = doms[id.split('-').shift()];
const value = localStorage.getItem(id);
if (value) {
$elem.value = value;
$dom.style.setProperty(`--${id}`, value);
}
$elem.addEventListener('change', (e) => {
const v = e.target.value;
localStorage.setItem(id, v);
$dom.style.setProperty(`--${id}`, v);
update();
});
});
// ===========================================================================================
const randomPosition = () => {
const x = Math.round(Math.random() * ($container.offsetWidth - $target.offsetWidth));
const y = Math.round(Math.random() * ($container.offsetHeight - $target.offsetHeight));
updateTarget(x, y);
};
update();
document.querySelector('.random').addEventListener('click', () => {
randomPosition();
update();
});
window.onload = () => {
const FPSDetector = window['fps-detector'].FPSDetector;
new FPSDetector('.fps-detector');
};
</script>
<script src="https://cdn.jsdelivr.net/npm/fps-detector@latest/dist/fps-detector.js"></script>
</body>
</html>