eloigil6 commited on
Commit
dda4750
·
1 Parent(s): 6c59a97

Throttle render loop to 30fps for improved performance in garden animation. Updated frame handling in createGarden function to reduce CPU/GPU load while maintaining smooth visuals.

Browse files
Files changed (1) hide show
  1. frontend/garden.js +9 -7
frontend/garden.js CHANGED
@@ -31,6 +31,10 @@ const WET_RATE = 0.5; // growth-stages per second while damp
31
  const DRY_RATE = 0.1; // growth-stages per second while dry
32
  const WATER_BUMP = 0.18; // instant nudge each time you water
33
 
 
 
 
 
34
  // A cohesive, sunny GBC-ish palette.
35
  const P = {
36
  sky0: "#7ec8ff",
@@ -666,11 +670,12 @@ export function createGarden(root) {
666
  // --- loop ------------------------------------------------------------------
667
  function frame(now) {
668
  if (!running) return;
669
- const dt = Math.min(0.05, (now - last) / 1000 || 0);
 
 
670
  last = now;
671
  update(dt);
672
  render();
673
- raf = requestAnimationFrame(frame);
674
  }
675
 
676
  // --- input wiring ----------------------------------------------------------
@@ -771,13 +776,10 @@ export function createGarden(root) {
771
  start() {
772
  if (running) return;
773
  running = true;
774
- last = 0;
775
  updateCaption();
776
  window.addEventListener("keydown", onKey, true);
777
- raf = requestAnimationFrame((now) => {
778
- last = now;
779
- frame(now);
780
- });
781
  },
782
  stop() {
783
  running = false;
 
31
  const DRY_RATE = 0.1; // growth-stages per second while dry
32
  const WATER_BUMP = 0.18; // instant nudge each time you water
33
 
34
+ // cap the render loop at 30fps — the garden animates gently, so there's no need
35
+ // to redraw at the display's full 60/120Hz (keeps it light on the CPU/GPU).
36
+ const FRAME_MS = 1000 / 30;
37
+
38
  // A cohesive, sunny GBC-ish palette.
39
  const P = {
40
  sky0: "#7ec8ff",
 
670
  // --- loop ------------------------------------------------------------------
671
  function frame(now) {
672
  if (!running) return;
673
+ raf = requestAnimationFrame(frame);
674
+ if (now - last < FRAME_MS - 1) return; // throttle to ~30fps
675
+ const dt = Math.min(0.05, last ? (now - last) / 1000 : 0);
676
  last = now;
677
  update(dt);
678
  render();
 
679
  }
680
 
681
  // --- input wiring ----------------------------------------------------------
 
776
  start() {
777
  if (running) return;
778
  running = true;
779
+ last = 0; // 0 → the first frame renders immediately, then the gate kicks in
780
  updateCaption();
781
  window.addEventListener("keydown", onKey, true);
782
+ raf = requestAnimationFrame(frame);
 
 
 
783
  },
784
  stop() {
785
  running = false;