-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontpage.js
186 lines (142 loc) Β· 4.22 KB
/
frontpage.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
console.clear()
let width = window.innerWidth;
let height = window.innerHeight;
const body = document.body;
const elButton = document.querySelector(".treat-button");
const elWrapper = document.querySelector(".treat-wrapper");
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const treatmojis = ["π","π","π","π","π","π","π","π","π","π","π","π","π","π","π","π"];
const treats = [];
const radius = 15;
const Cd = 0.47; // Dimensionless
const rho = 1.22; // kg / m^3
const A = Math.PI * radius * radius / 10000; // m^2
const ag = 9.81; // m / s^2
const frameRate = 1 / 60;
function createTreat() /* create a treat */ {
const vx = getRandomArbitrary(-10, 10); // x velocity
const vy = getRandomArbitrary(-10, 1); // y velocity
const el = document.createElement("div");
el.className = "treat";
const inner = document.createElement("span");
inner.className = "inner";
inner.innerText = treatmojis[getRandomInt(0, treatmojis.length - 1)];
el.appendChild(inner);
elWrapper.appendChild(el);
const rect = el.getBoundingClientRect();
const lifetime = getRandomArbitrary(2000, 3000);
el.style.setProperty("--lifetime", lifetime);
const treat = {
el,
absolutePosition: { x: rect.left, y: rect.top },
position: { x: rect.left, y: rect.top },
velocity: { x: vx, y: vy },
mass: 0.1, //kg
radius: el.offsetWidth, // 1px = 1cm
restitution: -.7,
lifetime,
direction: vx > 0 ? 1 : -1,
animating: true,
remove() {
this.animating = false;
this.el.parentNode.removeChild(this.el);
location.replace("game.html");
},
animate() {
const treat = this;
let Fx =
-0.5 *
Cd *
A *
rho *
treat.velocity.x *
treat.velocity.x *
treat.velocity.x /
Math.abs(treat.velocity.x);
let Fy =
-0.5 *
Cd *
A *
rho *
treat.velocity.y *
treat.velocity.y *
treat.velocity.y /
Math.abs(treat.velocity.y);
Fx = isNaN(Fx) ? 0 : Fx;
Fy = isNaN(Fy) ? 0 : Fy;
// Calculate acceleration ( F = ma )
var ax = Fx / treat.mass;
var ay = ag + Fy / treat.mass;
// Integrate to get velocity
treat.velocity.x += ax * frameRate;
treat.velocity.y += ay * frameRate;
// Integrate to get position
treat.position.x += treat.velocity.x * frameRate * 100;
treat.position.y += treat.velocity.y * frameRate * 100;
treat.checkBounds();
treat.update();
},
checkBounds() {
if (treat.position.y > height - treat.radius) {
treat.velocity.y *= treat.restitution;
treat.position.y = height - treat.radius;
}
if (treat.position.x > width - treat.radius) {
treat.velocity.x *= treat.restitution;
treat.position.x = width - treat.radius;
treat.direction = -1;
}
if (treat.position.x < treat.radius) {
treat.velocity.x *= treat.restitution;
treat.position.x = treat.radius;
treat.direction = 1;
}
},
update() {
const relX = this.position.x - this.absolutePosition.x;
const relY = this.position.y - this.absolutePosition.y;
this.el.style.setProperty("--x", relX);
this.el.style.setProperty("--y", relY);
this.el.style.setProperty("--direction", this.direction);
}
};
setTimeout(() => {
treat.remove();
}, lifetime);
return treat;
}
function animationLoop() {
var i = treats.length;
while (i--) {
treats[i].animate();
if (!treats[i].animating) {
treats.splice(i, 1);
}
}
requestAnimationFrame(animationLoop);
}
animationLoop();
function addTreats() {
//cancelAnimationFrame(frame);
if (treats.length > 40) {
return;
}
for (let i = 0; i < 10; i++) {
treats.push(createTreat());
}
}
elButton.addEventListener("click", addTreats);
window.addEventListener("resize", () => {
width = window.innerWidth;
height = window.innerHeight;
});
function playthe(){
let dead = new Audio();
let entry = new Audio();
entry.src = "audio/up.mp3";
}