summaryrefslogtreecommitdiff
path: root/assets/trees.js
blob: ffead63a107e9c94f8ffbb087ebe584cc2647ef8 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
(() => {
  const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
  if (reducedMotion.matches) return;

  const renderer = document.getElementById("renderer");
  if (!renderer) return;

  const canvas = document.createElement("canvas");
  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;

  const BRANCH_LENGTH = 5;
  const BRANCH_START = 20;
  const MAX_CHILDREN = 3;
  const CHILD_CHANGE = 0.4;
  const CHANCE_DEGRADATION = 4000;
  const NEXT_SOFT_CAP = 400;
  const BRANCH_MAX_ANGLE = R_10;
  const BRANCH_STAY_CHANCE = 0.5;
  const BRANCH_LIMIT_PADDING = 24;
  const FRAME_INTERVAL = 1000 / 30;

  let current = [];
  let next = [];
  let cycle = 0;
  let width;
  let height;
  let branchScale;
  let branchLength;
  let lastFrameTime;
  let animationFrame;
  let resizeFrame;

  function setup() {
    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);
    context.scale(pixelRatio, pixelRatio);
    context.strokeStyle = "rgba(127, 127, 127, " + 20 / 255 + ")";
    context.lineWidth = 1;
    context.lineCap = "round";

    const start = (x, y, r) => {
      for (let i = 0; i < BRANCH_START; i++) {
        let length = Math.random() * branchLength;
        let angle =
          r + Math.random() * (BRANCH_MAX_ANGLE - BRANCH_MAX_ANGLE * -1) + BRANCH_MAX_ANGLE * -1;

        current.push({
          x,
          y,
          x2: x + length * Math.cos(angle),
          y2: y + length * Math.sin(angle),
          angle,
        });
      }
    };

    start(
      (Math.random() * 0.6 + 0.2) * width,
      height + branchLength,
      -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() {
    let currentLen = current.length;
    while (currentLen--) {
      let index = Math.floor(Math.random() * currentLen);
      let branch = current[index];
      current.splice(index, 1);

      // Queue branch for next cycle.
      if (Math.random() <= BRANCH_STAY_CHANCE) {
        next.push(branch);
        continue;
      }

      context.beginPath();
      context.moveTo(branch.x, branch.y);
      context.lineTo(branch.x2, branch.y2);
      context.stroke();

      // Stop if soft cap is reached.
      if (next.length >= NEXT_SOFT_CAP) continue;

      if (
        branch.x2 > width + BRANCH_LIMIT_PADDING ||
        branch.x2 < -BRANCH_LIMIT_PADDING &&
        branch.y2 > height + BRANCH_LIMIT_PADDING ||
        branch.y2 < -BRANCH_LIMIT_PADDING
      ) {
        continue;
      }

      let amount = MAX_CHILDREN;

      while (amount--) {
        if (Math.random() > CHILD_CHANGE - cycle / CHANCE_DEGRADATION) continue;

        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) +
          BRANCH_MAX_ANGLE * -1;
        let ex = branch.x2 + length * Math.cos(angle);
        let ey = branch.y2 + length * Math.sin(angle);

        next.push({
          x: sx,
          y: sy,
          x2: ex,
          y2: ey,
          angle,
        });
      }
    }

    current = next;
    next = [];
    cycle++;
  }

  function animate(timestamp) {
    const elapsed = timestamp - lastFrameTime;
    if (elapsed >= FRAME_INTERVAL) {
      lastFrameTime = timestamp - (elapsed % FRAME_INTERVAL);
      draw();
    }
    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.
  animationFrame = requestAnimationFrame(() => {
    animationFrame = requestAnimationFrame(restart);
  });
})();