diff options
Diffstat (limited to 'assets')
| -rw-r--r-- | assets/main.css | 13 | ||||
| -rw-r--r-- | assets/trees.js | 79 |
2 files changed, 59 insertions, 33 deletions
diff --git a/assets/main.css b/assets/main.css index b9625c1..183a948 100644 --- a/assets/main.css +++ b/assets/main.css @@ -80,11 +80,16 @@ b, strong { font-weight: 700; } #renderer { position: fixed; + inset: 0; padding: 0; margin: 0; - top: 0; - bottom: 0; - left: 0; - right: 0; + overflow: hidden; + pointer-events: none; z-index: -5; } + +#renderer canvas { + display: block; + width: 100%; + height: 100%; +} 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); }); })(); |
