-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (40 loc) · 1.4 KB
/
script.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
const images = document.getElementsByClassName("image");
document.addEventListener("DOMContentLoaded", () => {
const baseUrl = "https://nekos.best/api/v2/neko";
const numImages = 10;
for (let i = 0; i < numImages; i++) {
const img = document.querySelector(`img[data-index="${i}"]`);
console.log(img);
fetch(baseUrl)
.then(response => response.json())
.then(data => {
const imageUrl = data.results[0].url;
img.src = imageUrl;
img.dataset.status = "active";
})
.catch(error => console.error(error));
}
});
let globalIndex = 0,
last = { x: 0, y: 0 };
const activate = (image, x, y) => {
image.style.left = `${x}px`;
image.style.top = `${y}px`;
image.style.zIndex = globalIndex;
image.dataset.status = "active";
last = { x, y };
}
const distanceFromLast = (x, y) => {
return Math.hypot(x - last.x, y - last.y);
}
const handleOnMove = e => {
if(distanceFromLast(e.clientX, e.clientY) > (window.innerWidth / 20)) {
const lead = images[globalIndex % images.length],
tail = images[(globalIndex - 5) % images.length];
activate(lead, e.clientX, e.clientY);
if(tail) tail.dataset.status = "inactive";
globalIndex++;
}
}
window.onmousemove = e => handleOnMove(e);
window.ontouchmove = e => handleOnMove(e.touches[0]);