summaryrefslogtreecommitdiff
path: root/assets/trees.js
diff options
context:
space:
mode:
authorNinoReichardt <nino.reichardt@oakk.nl>2026-11-17 20:46:59 +0100
committerNinoReichardt <nino.reichardt@oakk.nl>2026-11-17 20:46:59 +0100
commitdf39fc1496a23e50a5b3c9d7a32493610ca8847f (patch)
tree78be9993b366f9e58f18b20efcc537e40be777ed /assets/trees.js
parentfaae66a14bd256b760d464d2437951e3574c6391 (diff)
downloadnire-web-df39fc1496a23e50a5b3c9d7a32493610ca8847f.tar.gz
nire-web-df39fc1496a23e50a5b3c9d7a32493610ca8847f.zip
feat: smaller and less trees on smaller devices
Diffstat (limited to 'assets/trees.js')
-rw-r--r--assets/trees.js79
1 files changed, 50 insertions, 29 deletions
diff --git a/assets/trees.js b/assets/trees.js
index f65d3af..ffead63 100644
--- a/assets/trees.js
+++ b/assets/trees.js
@@ -9,6 +9,10 @@
const context = canvas.getContext("2d");
if (!context) return;
+ const smallScreen = window.matchMedia("(max-width: 768px)");
+ canvas.setAttribute("aria-hidden", "true");
+ renderer.appendChild(canvas);
+
const R_10 = Math.PI / 18;
const R_90 = Math.PI / 2;
const R_180 = Math.PI;
@@ -29,27 +33,32 @@
let cycle = 0;
let width;
let height;
+ let branchScale;
+ let branchLength;
let lastFrameTime;
+ let animationFrame;
+ let resizeFrame;
function setup() {
- width = window.innerWidth;
- height = window.innerHeight;
+ current = [];
+ next = [];
+ cycle = 0;
+ width = renderer.clientWidth;
+ height = renderer.clientHeight;
+ branchScale = smallScreen.matches ? 0.7 : 1;
+ branchLength = BRANCH_LENGTH * branchScale;
const pixelRatio = window.devicePixelRatio || 1;
canvas.width = Math.floor(width * pixelRatio);
canvas.height = Math.floor(height * pixelRatio);
- canvas.style.width = width + "px";
- canvas.style.height = height + "px";
- canvas.setAttribute("aria-hidden", "true");
context.scale(pixelRatio, pixelRatio);
context.strokeStyle = "rgba(127, 127, 127, " + 20 / 255 + ")";
context.lineWidth = 1;
context.lineCap = "round";
- renderer.appendChild(canvas);
const start = (x, y, r) => {
for (let i = 0; i < BRANCH_START; i++) {
- let length = Math.random() * BRANCH_LENGTH;
+ let length = Math.random() * branchLength;
let angle =
r + Math.random() * (BRANCH_MAX_ANGLE - BRANCH_MAX_ANGLE * -1) + BRANCH_MAX_ANGLE * -1;
@@ -65,20 +74,22 @@
start(
(Math.random() * 0.6 + 0.2) * width,
- height + BRANCH_LENGTH,
+ height + branchLength,
-R_90,
);
- start(
- width + BRANCH_LENGTH,
- (Math.random() * 0.6 + 0.2) * height,
- R_180,
- );
- start(
- 0 - BRANCH_LENGTH,
- (Math.random() * 0.6 + 0.2) * height,
- -R_90 * 4,
- );
- start((Math.random() * 0.6 + 0.2) * width, 0 - BRANCH_LENGTH, R_90);
+ if (!smallScreen.matches) {
+ start(
+ width + branchLength,
+ (Math.random() * 0.6 + 0.2) * height,
+ R_180,
+ );
+ start(
+ 0 - branchLength,
+ (Math.random() * 0.6 + 0.2) * height,
+ -R_90 * 4,
+ );
+ }
+ start((Math.random() * 0.6 + 0.2) * width, 0 - branchLength, R_90);
}
function draw() {
@@ -116,9 +127,9 @@
while (amount--) {
if (Math.random() > CHILD_CHANGE - cycle / CHANCE_DEGRADATION) continue;
- let length = Math.random() * BRANCH_LENGTH;
- let sx = branch.x2 + 1 * Math.cos(branch.angle);
- let sy = branch.y2 + 1 * Math.sin(branch.angle);
+ let length = Math.random() * branchLength;
+ let sx = branch.x2 + branchScale * Math.cos(branch.angle);
+ let sy = branch.y2 + branchScale * Math.sin(branch.angle);
let angle =
branch.angle +
Math.random() * (BRANCH_MAX_ANGLE - BRANCH_MAX_ANGLE * -1) +
@@ -147,15 +158,25 @@
lastFrameTime = timestamp - (elapsed % FRAME_INTERVAL);
draw();
}
- if (current.length > 0) requestAnimationFrame(animate);
+ if (current.length > 0) animationFrame = requestAnimationFrame(animate);
+ }
+
+ function restart(timestamp) {
+ cancelAnimationFrame(animationFrame);
+ setup();
+ lastFrameTime = timestamp;
+ animationFrame = requestAnimationFrame(animate);
}
+ window.addEventListener("resize", () => {
+ // CSS handles height-only changes without restarting trees during mobile scrolling.
+ if (renderer.clientWidth === width) return;
+ cancelAnimationFrame(resizeFrame);
+ resizeFrame = requestAnimationFrame(restart);
+ });
+
// Let the page paint before starting the decorative canvas.
- requestAnimationFrame(() => {
- requestAnimationFrame((timestamp) => {
- setup();
- lastFrameTime = timestamp;
- requestAnimationFrame(animate);
- });
+ animationFrame = requestAnimationFrame(() => {
+ animationFrame = requestAnimationFrame(restart);
});
})();