Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch to requestAnimationFrame() for drawing #948

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

pancelor
Copy link
Contributor

This is essentially the same code as #924, with this additional code to make sure deltatime is accurate

var prevTimestamp;
function loop(timestamp) {
    var deltatime = 0
    if (prevTimestamp !== undefined) {
        deltatime = timestamp - prevTimestamp;
    }
    prevTimestamp = timestamp

This was the solution suggested by the docs; I should have seen it and included it last time!

Warning: Be sure to always use the first argument (or some other method for getting the current time) to calculate how much the animation will progress in a frame, otherwise the animation will run faster on high refresh rate screens. Check the example below for a way to do this.

previously we assumed deltatime=17 always,
but this may not be true. now, use the value
that requestAnimationFrame() gives us.

this follows the recommendation from MDN:
https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
ClementSparrow added a commit to ClementSparrow/Pattern-Script that referenced this pull request Dec 28, 2022
Copy link

@cwboden cwboden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet 👍

@@ -35,7 +35,7 @@ var throttle_movement=false;
var cache_console_messages=false;
var quittingTitleScreen=false;
var quittingMessageScreen=false;
var deltatime=17;
var deltatime=17; // this gets updated every frame; see loop()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love comments like this 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants