-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedium_snow.js
95 lines (82 loc) · 2.99 KB
/
medium_snow.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
const life_per_tick = 1000 / 60;
const max_snowflakes = Math.min(18, (screen.width / 1280) * 18);
const snowflakes = [];
const period = [
(n) => 5 * Math.sin(n),
(n) => 8 * Math.cos(n),
(n) => 5 * (Math.sin(n) + Math.cos(2 * n)),
(n) => 2 * (Math.sin(0.25 * n) - Math.cos(0.75 * n) + 1),
(n) => 5 * (Math.sin(0.75 * n) + Math.cos(0.25 * n) - 1),
];
const have_fun = ["⛄", "🦌", "🎁", "💵", "🍪", "💰", "🎄"];
function ready(fn) {
if (
document.attachEvent
? document.readyState === "complete"
: document.readyState !== "loading"
) {
fn();
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
function resetSnowflake(snowflake) {
let x = (snowflake.dataset.origX = Math.random() * 100);
let y = (snowflake.dataset.origY = 0);
let z = (snowflake.dataset.origZ =
Math.random() < 0.1 ? Math.ceil(Math.random() * 100) + 25 : 0);
let life = (snowflake.dataset.life = Math.ceil(Math.random() * 4000) + 6000);
snowflake.dataset.origLife = Math.ceil(Math.random() * 4000) + 6000;
snowflake.dataset.origLife = life;
snowflake.style.transform = `translate3d(${x}vw, ${y}vh, ${z}px)`;
snowflake.style.opacity = 0.8;
snowflake.dataset.periodFunction = Math.floor(Math.random() * period.length);
if (Math.random() < 0.001) {
snowflake.textContent = have_fun[Math.floor(Math.random() * have_fun.length)];
}
}
function updatePositions() {
snowflakes.forEach((snowflake) => {
let origLife = parseFloat(snowflake.dataset.origLife);
let curLife = parseFloat(snowflake.dataset.life);
let dt = (origLife - curLife) / origLife;
if (dt <= 1.0) {
let p = period[parseInt(snowflake.dataset.periodFunction)];
let x = p(dt * 2 * Math.PI) + parseFloat(snowflake.dataset.origX);
let y = 100 * dt;
let z = parseFloat(snowflake.dataset.origZ);
snowflake.style.transform = `translate3d(${x}vw, ${y}vh, ${z}px)`;
if (dt >= 0.5) {
snowflake.style.opacity = 1.0 - (dt - 0.5) * 2;
}
curLife -= life_per_tick;
snowflake.dataset.life = curLife;
} else {
resetSnowflake(snowflake);
}
});
window.requestAnimationFrame(updatePositions);
}
export const mediumSnowflakesWrapper = document.createElement("div");
mediumSnowflakesWrapper.classList.add("medium_snowflakes_wrapper");
mediumSnowflakesWrapper.setAttribute("aria-hidden", "true");
mediumSnowflakesWrapper.setAttribute("role", "presentation");
function appendSnow() {
let i = 0;
const addSnowflake = () => {
let snowflake = document.createElement("span");
snowflake.classList.add("medium_snowflake");
snowflake.setAttribute("aria-hidden", "true");
snowflake.setAttribute("role", "presentation");
snowflake.textContent = "❄";
resetSnowflake(snowflake);
snowflakes.push(snowflake);
mediumSnowflakesWrapper.appendChild(snowflake);
if (i++ <= max_snowflakes) {
setTimeout(addSnowflake, Math.ceil(Math.random() * 300) + 100);
}
};
addSnowflake();
updatePositions();
}
ready(appendSnow);