1
Fork 0
mirror of https://github.com/RGBCube/Site synced 2025-08-01 13:37:49 +00:00

blog: fix animation

This commit is contained in:
RGBCube 2025-06-03 02:45:54 +03:00
parent 5efa81ca2a
commit 3732704d74
Signed by: RGBCube
SSH key fingerprint: SHA256:CzqbPcfwt+GxFYNnFVCqoN5Itn4YFrshg1TrnACpA5M

View file

@ -90,7 +90,7 @@ Blog Articles
</script> </script>
<script> <script>
{ {
let typed = ""; let typed = "";
const target = "rix"; const target = "rix";
@ -105,20 +105,23 @@ Blog Articles
} }
}); });
let data = null; const randomStrip = () => ({
xFactor: Math.random() * 0.95,
const strips = Array.from({ length: 60 }, () => ({ y: Math.random() * -window.innerHeight - 100,
x: Math.floor(Math.random() * window.innerWidth * 1.05), deltaY: Math.random() * 1.7 + 1,
y: -100,
dY: Math.floor(Math.random() * 7) + 3,
size: Math.floor(Math.random() * 16) + 8, size: Math.floor(Math.random() * 16) + 8,
})); });
const strips = Array.from({ length: 60 }, () => randomStrip());
let data = null;
const toggle = () => { const toggle = () => {
if (data) { if (data) {
clearInterval(data.interval); cancelAnimationFrame(data.animationFrameId);
data.canvas.remove();
data.element.remove(); window.removeEventListener("resize", data.handleResize);
data = null; data = null;
return; return;
@ -126,8 +129,8 @@ Blog Articles
data = {}; data = {};
const canvas = document.body.appendChild(document.createElement("canvas")); data.canvas = document.body.appendChild(document.createElement("canvas"));
const { canvas } = data;
canvas.style.position = "fixed"; canvas.style.position = "fixed";
canvas.style.top = "0"; canvas.style.top = "0";
canvas.style.left = "0"; canvas.style.left = "0";
@ -136,64 +139,64 @@ Blog Articles
canvas.style.zIndex = "999999999"; canvas.style.zIndex = "999999999";
canvas.style.pointerEvents = "none"; canvas.style.pointerEvents = "none";
canvas.width = window.innerWidth; data.context = canvas.getContext("2d");
canvas.height = window.innerHeight; const { context } = data;
data.element = canvas;
const context = canvas.getContext("2d");
context.globalCompositeOperation = "lighter"; context.globalCompositeOperation = "lighter";
const drawStrip = (strip) => { const handleResize = () => {
let { x, y } = strip; canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
};
for (let i = 0; i < 20; i++) { handleResize();
switch (i) { window.addEventListener("resize", handleResize);
case 0: context.fillStyle = "#cefbe4"; break; data.handleResize = handleResize;
case 1: context.fillStyle = "#81ec72"; break;
case 3: context.fillStyle = "#5cd646"; break;
case 7: context.fillStyle = "#54d13c"; break;
case 13: context.fillStyle = "#4ccc32"; break;
case 17: context.fillStyle = "#43c728"; break;
}
const characters = ["诶", "比", "西", "迪", "伊", "吉", "艾", "杰", "开", "哦", "屁", "提", "维"]; const animate = () => {
const character = characters[Math.floor(Math.random()*characters.length)]; const { canvas, context } = data;
context.fillText(character, x, y);
// Deterministic but still random.
y -= strips[i].size;
}
}
const draw = () => {
context.clearRect(0, 0, canvas.width, canvas.height); context.clearRect(0, 0, canvas.width, canvas.height);
context.shadowOffsetX = 0; context.shadowOffsetX = 0;
context.shadowOffsetY = 0; context.shadowOffsetY = 0;
context.shadowBlur = 8; context.shadowBlur = 8;
context.shadowColor = "#94f475"; context.shadowColor = "#94f475";
for (const strip of strips) {
context.font = `${strip.size}px sans`;
context.textBaseline = "top"; context.textBaseline = "top";
context.textAlign = "center"; context.textAlign = "center";
if (strip.y > canvas.height * 1.7) { for (const strip of strips) {
strip.x = Math.floor(Math.random() * canvas.width); context.font = `${strip.size}px sans`;
strip.y = -100;
strip.dY = Math.floor(Math.random() * 7) + 3; if (strip.y > canvas.height + (strip.size * 30)) {
strip.size = Math.floor(Math.random() * 16) + 8; Object.assign(strip, randomStrip())
} }
drawStrip(strip); let { y: yCopy } = strip;
for (let i = 0; i < 20; i++) {
strip.y += strip.dY; switch (true) {
case i < 1: context.fillStyle = "#cefbe4"; break;
case i < 2: context.fillStyle = "#81ec72"; break;
case i < 4: context.fillStyle = "#5cd646"; break;
case i < 8: context.fillStyle = "#54d13c"; break;
case i < 14: context.fillStyle = "#4ccc32"; break;
case i < 18: context.fillStyle = "#43c728"; break;
} }
const characters = ["诶", "比", "西", "迪", "伊", "吉", "艾", "杰", "开", "哦", "屁", "提", "维"];
const character = characters[Math.floor(Math.random() * characters.length)];
context.fillText(character, strip.xFactor * canvas.width, yCopy);
// Deterministic but still random.
yCopy -= strips[i].size;
}
strip.y += strip.deltaY;
}
data.animationFrameId = requestAnimationFrame(animate);
}; };
draw(); animate();
data.interval = setInterval(draw, 70);
}; };
} }
</script> </script>