-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (44 loc) · 1.32 KB
/
script.js
File metadata and controls
58 lines (44 loc) · 1.32 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
const year = document.getElementById('year');
if (year) {
year.textContent = new Date().getFullYear();
}
const pixelDog = document.getElementById('pixelDog');
const dogPlayground = document.querySelector('.dog-playground');
if (pixelDog && dogPlayground) {
let x = 0;
let direction = 1;
let lastTime = 0;
let gallopTimer = 0;
const speed = 110; // px per second
const animateDog = (time) => {
if (!lastTime) {
lastTime = time;
}
const dt = (time - lastTime) / 1000;
lastTime = time;
const dogWidth = pixelDog.getBoundingClientRect().width;
const maxX = Math.max(0, dogPlayground.clientWidth - dogWidth);
x += direction * speed * dt;
if (x <= 0) {
x = 0;
direction = 1;
} else if (x >= maxX) {
x = maxX;
direction = -1;
}
gallopTimer += dt;
if (gallopTimer >= 0.16) {
pixelDog.classList.toggle('is-gallop');
gallopTimer = 0;
}
const facing = direction === 1 ? 1 : -1;
pixelDog.style.transform = `translateX(${x}px) scaleX(${facing})`;
requestAnimationFrame(animateDog);
};
requestAnimationFrame(animateDog);
window.addEventListener('resize', () => {
const dogWidth = pixelDog.getBoundingClientRect().width;
const maxX = Math.max(0, dogPlayground.clientWidth - dogWidth);
x = Math.min(x, maxX);
});
}