Implementation Notes
Architecture
- Fish — A full class encapsulating position, angle, speed, colour palette, and tail phase. Each fish owns its own wandering timer and speed-noise phase.
- Bubble — Lightweight particle class that drifts upward with sinusoidal wobble and respawns when off-screen.
- Seaweed — Static (but gently swaying) background decoration drawn as a chain of quadratic Bézier curves.
Autonomous Behaviours
- Wandering — Every 120 ± frames a new target angle is chosen within a small angular window of the current heading. The fish steers toward it at a fixed turn rate, producing organic curves.
- Boundary Repulsion — Within a 60 px margin of each edge, a push proportional to distance is added to the fish's angle, so it turns away before hitting the wall. Combined with a gentle centre-bias force, fish naturally explore the whole tank without clustering.
- Speed Modulation — Each fish's base speed is modulated by a low-frequency sine wave, giving the impression of accelerating and slowing down.
Rendering
- Delta-time — All physics are multiplied by a normalised
dt (frame-time / 16.667 ms, clamped to ≤ 3×) so movement is identical at 30 fps, 60 fps, or higher.
- Depth sorting — Fish are re-sorted by Y each frame so those "lower" on screen render on top, adding a subtle 3-D feel.
- Fish shape — A streamlined body drawn with cubic Bézier curves, a gradient fill for volume, animated tail flaps, dorsal & pectoral fins, a glossy eye, and faint scale arcs.
- Caustics — Semi-transparent light rays sway gently across the water for ambiance.
Design Decisions
- 15 fish was chosen as a sweet spot between visual richness and performance.
- 12 tropical colour palettes are rotated; each fish gets a random size (28–56 px) for variety.
- No external libraries — pure Canvas 2D API + requestAnimationFrame.