-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
91 lines (77 loc) · 2.16 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
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
class Logo {
constructor(element) {
this.element = element;
this.velocity = 0.02;
this.reset();
}
get x() {
return parseFloat(getComputedStyle(this.element).getPropertyValue("--x"));
}
set x(value) {
this.element.style.setProperty("--x", value);
}
get y() {
return parseFloat(getComputedStyle(this.element).getPropertyValue("--y"));
}
set y(value) {
this.element.style.setProperty("--y", value);
}
rect() {
return this.element.getBoundingClientRect();
}
reset() {
this.x = 50;
this.y = 50;
this.direction = { x: 0 };
while (
Math.abs(this.direction.x) <= 0.2 ||
Math.abs(this.direction.x) >= 0.9
) {
const heading = getRandomNumberBetween(0, 2 * Math.PI);
this.direction = { x: Math.cos(heading), y: Math.sin(heading) };
}
}
update(delta) {
this.x += this.direction.x * this.velocity * delta;
this.y += this.direction.y * this.velocity * delta;
const rect = this.rect();
let centerY = (rect.height * 50) / window.innerHeight;
let centerX = (rect.width * 50) / window.innerWidth;
if (rect.bottom >= window.innerHeight) {
this.y = 100 - centerY;
this.direction.y *= -1;
changeColor();
} else if (rect.top <= 0) {
this.y = centerY;
this.direction.y *= -1;
changeColor();
}
if (rect.right >= window.innerWidth) {
this.x = 100 - centerX;
this.direction.x *= -1;
changeColor();
} else if (rect.left <= 0) {
this.x = centerX;
this.direction.x *= -1;
changeColor();
}
}
}
const logo = new Logo(document.querySelector("svg"));
let lastTime;
function update(time) {
if (lastTime !== undefined) {
const delta = time - lastTime;
logo.update(delta);
}
lastTime = time;
window.requestAnimationFrame(update);
}
window.requestAnimationFrame(update);
function getRandomNumberBetween(min, max) {
return Math.random() * (max - min) + min;
}
function changeColor() {
const random = getRandomNumberBetween(0, 360);
document.documentElement.style.setProperty("--hue", random);
}