Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

The same flow field ran 2.4x faster on a 144 Hz monitor

I have a particle sketch: 8,000 points drifting through a flow field, Three.js, about 160 lines of plain JavaScript. It looked right on my laptop. I opened it on the desktop with the 144 Hz panel and the whole field tore

I have a particle sketch: 8,000 points drifting through a flow field, Three.js, about 160 lines of plain JavaScript. It looked right on my laptop. I opened it on the desktop with the 144 Hz panel and the whole field tore across the screen like it had somewhere to be.

Nothing was wrong with the desktop. The sketch was wrong everywhere, and I only owned one monitor that could show it.

The bug: the integrator counted frames instead of seconds

The original step was per frame:

velocities[ix] += Math.cos(n * 6) * ACCEL;
pos[ix] += velocities[ix];
velocities[ix] *= DAMPING;

ACCEL and DAMPING were tuned by eye at 60 Hz, so the field moved the way I wanted — on exactly one machine. At 144 Hz every particle took 2.4× as many steps per second. Same code, different physics.

The fix is to rescale constants that were quoted per 1/60 s step by the wall-clock time that actually elapsed:

const dt = last === null ? 0 : Math.min(Math.max((now - last) / 1000, 0), MAX_STEP);
last = now;
const step = dt * 60;                 // frames' worth of 1/60 s steps
const damping = Math.pow(DAMPING, step);

The pow matters. Damping is multiplicative per step, so it does not scale linearly — step it 2.4 times as far and you need 0.96^2.4, not 0.96 * 2.4.

And the clamp is not optional. requestAnimationFrame stops firing in a backgrounded tab, so the first frame after you come back carries a dt of however long you were gone. Without a ceiling the field teleports and damping collapses toward zero:

const MAX_STEP = 1 / 30; // a backgrounded tab must not teleport the field

The second bug: a third of the particles were never on screen

While I was in there I checked the seeding box. Particles spawned in a cube of ±30 world units, camera at z = 30, 60° vertical FOV. I had picked 30 because it looked right at boot.

It isn't, because the cloud rotates around Y. A corner can swing as far as bound * √2 toward the camera, and at that nearer depth the visible half-height is smaller than at z = 30. The box has to fit the frustum at the worst rotation angle, not the friendly one:

const SPIN_REACH = Math.SQRT2;

function seedingBound() {
  const tanHalf = Math.tan((FOV * Math.PI) / 360);
  const vertical = (tanHalf * camera.position.z) / (1 + tanHalf * SPIN_REACH);
  const tanHalfX = tanHalf * camera.aspect;
  const horizontal =
    (tanHalfX * camera.position.z) / (SPIN_REACH * Math.sqrt(1 + tanHalfX * tanHalfX));
  return Math.min(vertical, horizontal);
}

I wrote a test that steps the cloud through a full rotation and projects every seed point. The answer came back ugly: about 43% of the 8,000 particles never rasterized, for the entire run. I was paying for 8,000 points and drawing 4,500.

Two smaller ones, same afternoon

A resize clears the drawing buffer. Users who ask for reduced motion get one static frame, so on resize the canvas went blank and stayed blank — no loop to repaint it. updateViewport() now redraws when the scene exists and the loop is stopped.

Pausing needed a generation counter. requestAnimationFrame callbacks are already queued when you pause; a frame scheduled a moment earlier still fires, draws, and queues a successor. Toggling the preference off and on quickly left two loops re-arming from the same pending callback. Each queued frame now carries the generation it was scheduled for and drops itself if it is stale:

function scheduleFrame() {
  const generation = runGeneration;
  requestAnimationFrame((now) => {
    if (generation !== runGeneration) return; // stale frame from a stopped run
    tick(now);
  });
}

What I actually learned

Frame-coupled motion is the kind of bug that survives every test you run on your own hardware, because your hardware is the constant. The only defense I trust is asserting the invariant directly: the test boots the sketch twice against a seeded RNG, steps one at 16.6 ms and the other at 6.9 ms, and checks both clouds land in the same place after the same simulated second. If a future commit sneaks in a per-frame constant, that test goes red before I do.

Same for the frustum: project the seeds at several rotation angles, assert they are inside normalized device coordinates. Cheap, invisible, and it catches a whole class of "why does this look sparse" that you would otherwise spend an evening squinting at.

The sketch is at github.com/marcus-beauregard/gen-art. Six commits, six tests, no build step.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.