Giter Club home page Giter Club logo

Comments (4)

rezoner avatar rezoner commented on September 4, 2024

onStep is based on interval which is set to 25
onRender is using requestAnimationFrame which will run as fast as possible

You should use delta in your formulas like

x += speed * delta / 1000;

Speed is in pixels per second. Try to put that in both render and step and you will notice that they go at the same speed.

from canvasquery.

florian-s-code avatar florian-s-code commented on September 4, 2024

Thank you for your reply, I should have used delta before but I was missing a formula so double thanks.

But why don't the 2 methods use requestAnimationFrame ?

from canvasquery.

rezoner avatar rezoner commented on September 4, 2024

Well this is really simple.

When you change the tab in the browser.
requestAnimationFrame stops - which is something you really want because it also stops to drain users CPU
but the setInterval is still going - that's why we use it for logic update - because usually you want the game to proceed - same as you are alt-tabbing in windows.

Why not put logic in animationFrame? Because logic can be really heavy - for example when using physics - and you don't need 60 updates per second for physics - it could just kill the performance.

ps: well you can decrease FPS rate manually in requestAnimationFrame, so this is not 100% accurate answer :)

from canvasquery.

florian-s-code avatar florian-s-code commented on September 4, 2024

I was using requestAnimationFrame for everything, thinking it would boost my games' performance but it was not the right way of using it.
Thanks for the explanation.

from canvasquery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.