forked from rDrama/rDrama
1
0
Fork 0
rDrama/files/assets/events/homoween/js/upsidedown.js

66 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-10-13 17:22:59 +00:00
let st = init("canvas"),
2023-09-28 23:58:09 +00:00
w = (canvas.width = innerWidth),
2023-09-29 06:11:18 +00:00
h = (canvas.height = innerHeight);
2023-09-28 23:58:09 +00:00
class firefly {
constructor() {
this.x = Math.random() * w;
this.y = Math.random() * h;
this.s = Math.random() * 2;
this.ang = Math.random() * 2 * Math.PI;
this.v = (this.s * this.s) / 4;
}
move() {
this.x += this.v * Math.cos(this.ang);
this.y += this.v * Math.sin(this.ang);
this.ang += (Math.random() * 20 * Math.PI) / 180 - (10 * Math.PI) / 180;
}
show() {
st.beginPath();
st.arc(this.x, this.y, this.s, 0, 2 * Math.PI);
st.fillStyle = "#fff";
st.fill();
}
}
let f = [];
2023-10-14 20:52:17 +00:00
let num_fireflies = 10
if (screen_width >= 768) {
num_fireflies = 50
}
2023-09-28 23:58:09 +00:00
function draw() {
2023-10-14 20:52:17 +00:00
if (f.length < num_fireflies) {
2023-09-28 23:58:09 +00:00
for (let j = 0; j < 10; j++) {
f.push(new firefly());
}
}
//animation
for (let i = 0; i < f.length; i++) {
f[i].move();
f[i].show();
if (f[i].x < 0 || f[i].x > w || f[i].y < 0 || f[i].y > h) {
f.splice(i, 1);
}
}
}
function init(elemid) {
let canvas = document.getElementById(elemid),
st = canvas.getContext("2d"),
w = (canvas.width = innerWidth),
2023-09-29 06:11:18 +00:00
h = (canvas.height = innerHeight);
2023-09-28 23:58:09 +00:00
st.fillStyle = "rgba(30,30,30,1)";
st.fillRect(0, 0, w, h);
return st;
}
function loop() {
st.clearRect(0, 0, w, h);
draw();
}
loop();
setInterval(loop, 1000 / 60);