-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
145 lines (101 loc) · 3.27 KB
/
main.js
File metadata and controls
145 lines (101 loc) · 3.27 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
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
const cookieTextureCount = 3; // Must be in the interval [0, 9]
const cookieCount = 50; // The number of cookie instances
const cookieBaseSize = 200; // In pixels
class Cookie {
static greatestId = 0;
constructor() {
Cookie.greatestId++;
this.id = Cookie.greatestId;
this.domEl = new Image();
this.x = 0;
this.y = 0;
this.setDistance(1.0);
this.rotation = 0.0;
this.flip = false;
this.setTextureIndex(0);
this.domEl.alt = `Cookie ${this.id}`;
this.domEl.style.width = `${cookieBaseSize}px`;
this.domEl.style.height = `${cookieBaseSize}px`;
this.domEl.style.position = "absolute";
this.updateTransform();
document.body.appendChild(this.domEl);
}
setTextureIndex(index) {
this.domEl.src = `textures/cookie-${index}.png`;
}
setPosition(x, y) {
this.x = x;
this.y = y;
}
setDistance(distance) {
this.distance = distance;
// Set the z index to one of a million integer values
this.domEl.style.zIndex = `${-Math.floor(1000000 * distance)}`;
}
setRotation(rotation) {
this.rotation = rotation;
}
setFlip(flip) {
this.flip = flip;
}
updateTransform() {
// Multiply the x scale by -1 if the cookie is flipped
let flipMult = this.flip ? -1.0 : 1.0;
let scale = 1.0 / this.distance;
// Transformation string for CSS
let translateString =
`translate(${this.x - cookieBaseSize / 2}px, \
${this.y - cookieBaseSize / 2}px)`;
let rotateString = `rotate(${this.rotation}deg)`;
let scaleString = `scale(${flipMult * scale}, ${scale})`;
this.domEl.style.transform =
`${translateString} ${rotateString} ${scaleString}`;
}
}
let windowWidth = window.innerWidth
let windowHeight = window.innerHeight;
let prevTime = Date.now();
let cookies = [];
let cookieSpeed = 0.125;
function setCookie(cookie) {
let x = Math.random() * windowWidth; // Distribute horizontally
let y = -cookieBaseSize * (1.0 + 5.0 * Math.random()); // Vertical spacing
cookie.setTextureIndex(Math.floor(cookieTextureCount * Math.random()));
cookie.setPosition(x, y);
cookie.setDistance(1.0 + 1.0 * Math.random());
cookie.setRotation(Math.random() * 360.0);
cookie.setFlip(Math.random() < 0.5);
cookie.updateTransform();
}
function ioLoop() {
// Update window dimensions
windowWidth = window.innerWidth
windowHeight = window.innerHeight;
let deltaTime = Date.now() - prevTime;
for (let c of cookies) {
if (c.y < windowHeight + 3.0 * cookieBaseSize) {
c.y += cookieSpeed * deltaTime / (c.distance * c.distance);
c.updateTransform();
} else {
setCookie(c);
}
}
prevTime = Date.now();
requestAnimationFrame(ioLoop);
}
function main() {
for (let i = 0; i < cookieCount; i++) {
let c = new Cookie(0);
setCookie(c);
cookies.push(c);
}
ioLoop();
}
document.addEventListener("keydown", (event) => {
if ("r" == event.key || "R" == event.key) {
for (let i = 0; i < cookieCount; i++) {
setCookie(cookies[i]);
}
}
});
main();