-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (78 loc) · 2.1 KB
/
Copy pathscript.js
File metadata and controls
92 lines (78 loc) · 2.1 KB
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
import { settings, applyPageSettings } from './settings.js';
import { registerCallback } from './fpsCounter.js';
/**
* @type {HTMDElement}
*/
export const parentEl = document.querySelector('#div_container');
/**
* @type {[HTMLElement]}
*/
export let divs = [];
export function clearDivs() { divs = [];}
export function orderDivs() {
clearDivs();
for (let i = 0; i < parentEl.children.length; i++) {
divs.push(parentEl.children[i]);
}
}
orderDivs();
function getCurRects() {
const rects = [];
for (const div of divs) {
const rect = div.getBoundingClientRect();
rects.push(rect);
}
return rects;
}
function animate(applyChange) {
// For every element, get the current rect, then move it to the next position
const curRects = getCurRects().map(r => ({x: r.x, y: r.y}));
// Prepend last div to the front
if (applyChange) applyChange();
const newRects = getCurRects().map(r => ({x: r.x, y: r.y}))
// Now, animate!
for (let i = 0; i < divs.length; i++) {
const div = divs[i];
const curRect = curRects[i];
const newRect = newRects[i];
const dx = curRect.x - newRect.x;
const dy = curRect.y - newRect.y;
div.animate([
{
transform: `translate(${dx}px, ${dy}px)`
},
{
transform: 'none'
}
], {
duration: 500,
easing: 'ease-in-out'
});
}
orderDivs();
}
orderDivs();
setInterval(() => {
animate(settings?.animation);
}, 750);
function applyChange(input) {
let value = +input.value;
if (input.step < 1)
value = value.toFixed(2);
input.nextElementSibling.value = value;
}
const inputs = document.querySelectorAll('input:has(+ output)');
inputs.forEach(i => {
i.addEventListener('input', _ => {
applyChange(i);
});
applyChange(i);
});
document.forms[0]?.addEventListener('submit', e => {
e.preventDefault();
applyPageSettings();
});
applyPageSettings();
registerCallback(fps => {
document.querySelector('#fps').textContent = fps;
});