Base movement speed entirely on time #8

Closed
opened 2025-08-08 14:48:08 +03:00 by gytisrepecka · 1 comment
Owner

Currently logo movement is determined on every screen redraw with window.requestAnimationFrame:

Line 111 in 6a2a3de
window.requestAnimationFrame(playAnimation);

This method redraws screen based on refresh rate, which, as it turns out, dinamically varies on mobile devices - e.g.: when inactive, screen will drop refresh rate and logo will noticeably slow-down.

In order to compensate this, all logo movement - regardless of slower or faster - should be calculated based on time and not rely on refresh rate.

At the moment delay is implemented for situations when we want to skip movement by 1 px during one requestAnimationFrame cycle - that enables introduction of delay by miliseconds configured in movementVelocity.

Line 50 in 6a2a3de
Delay moving logo by 1 pixel for movementVelocity miliseconds.

Currently logo movement is determined on every screen redraw with `window.requestAnimationFrame`: https://source.gyt.is/inretio/bouncing-logo/src/commit/6a2a3de69edbc1fd74a931b9fa780e4f1b00c734/bounce.js#L111 [This method](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame) redraws screen based on refresh rate, which, as it turns out, dinamically varies on mobile devices - e.g.: when inactive, screen will drop refresh rate and logo will noticeably slow-down. In order to compensate this, all logo movement - regardless of slower or faster - should be calculated based on time and not rely on refresh rate. At the moment delay is implemented for situations when we want to skip movement by 1 px during one `requestAnimationFrame` cycle - that enables introduction of delay by miliseconds configured in `movementVelocity`. https://source.gyt.is/inretio/bouncing-logo/src/commit/6a2a3de69edbc1fd74a931b9fa780e4f1b00c734/bounce.js#L50
Author
Owner

Screen is redrawed with requestAnimationFrame based on screen refresh rate. Since it is dynamic on mobile devices, need to introduce time-based pause in every cycle of requestAnimationFrame redraw:

Line 25 in fd1d2af
let movementVelocity = 10;

That way no matter how fast or slow screen redraws, there is always a movementVelocity (ms) pause between logo is moved a pixel:

Line 54 in fd1d2af
if ((lastMovementTime == null) || ((Math.abs(lastMovementTime - Date.now())) > movementVelocity) ) {

Screen is redrawed with `requestAnimationFrame` based on screen refresh rate. Since it is dynamic on mobile devices, need to introduce time-based pause in every cycle of `requestAnimationFrame` redraw: https://source.gyt.is/inretio/bouncing-logo/src/commit/fd1d2afda6c6ba1d966262e41c150cabd05626ee/bounce.js#L25 That way no matter how fast or slow screen redraws, there is always a `movementVelocity` (ms) pause between logo is moved a pixel: https://source.gyt.is/inretio/bouncing-logo/src/commit/fd1d2afda6c6ba1d966262e41c150cabd05626ee/bounce.js#L54
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
inretio/bouncing-logo#8
No description provided.