{"id":20906135,"url":"https://github.com/distributedlife/game-loops","last_synced_at":"2025-07-31T18:32:44.539Z","repository":{"id":57244124,"uuid":"69458805","full_name":"distributedlife/game-loops","owner":"distributedlife","description":"Several kinds of game loop","archived":false,"fork":false,"pushed_at":"2016-10-14T08:19:33.000Z","size":10,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-25T23:44:06.315Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/distributedlife.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-28T12:00:23.000Z","updated_at":"2022-07-28T16:22:08.000Z","dependencies_parsed_at":"2022-09-01T06:11:38.573Z","dependency_job_id":null,"html_url":"https://github.com/distributedlife/game-loops","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fgame-loops","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fgame-loops/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fgame-loops/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/distributedlife%2Fgame-loops/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/distributedlife","download_url":"https://codeload.github.com/distributedlife/game-loops/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243297464,"owners_count":20268778,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T13:29:11.024Z","updated_at":"2025-03-12T21:29:05.957Z","avatar_url":"https://github.com/distributedlife.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Game Loops\n\nVarious implementations of game loops. Most of which I have implemented at one time or another. I grow weary of rewriting this code. Here it is for viewing or usage. There are tests too. If you want detail explanation on why you may want one over another read [Fix your timestep by Glenn Fielder](http://gafferongames.com/game-physics/fix-your-timestep/).\n\n- fixed delta time\n- variable delta time\n- semi fixed timestep\n- fixed timestep\n- fixed timestep with remainder\n\n# Creating a loop\n\n```javascript\nimport {\n  createFixedDeltaTime,\n  createVariableDeltaTime,\n  createSemiFixedTimestep,\n  createFixedTimeStep,\n  createFixedTimeStepWithRemainder\n} from 'game-loops';\n\nconst ms = 1000 / 60;\nconst onFrame = (Δ, t) =\u003e { ... };\nconst onRemainder = (Δ, t) =\u003e { ... };\nconst isPaused = () =\u003e false;\n\nconst fixedDeltaTime = createFixedDeltaTime(ms, isPaused, onFrame);\nconst variableDeltaTime = createVariableDeltaTime(isPaused, onFrame);\nconst semiFixedTimeStep = createSemiFixedTimestep(ms, isPaused, onFrame);\nconst fixedTimeStep = createFixedTimeStep(ms, isPaused, onFrame);\nconst withRemainder = createFixedTimeStepWithRemainder(ms, isPaused, onFrame, onRemainder);\n```\n\n# Hooking it into a scheduler\n\n```javascript\nsetInterval(fixedDeltaTime, 1000 / 60);\nsetInterval(variableDeltaTime, 1000 / 60);\nsetInterval(semiFixedTimeStep, 1000 / 60);\nsetInterval(fixedTimeStep, 1000 / 60);\nsetInterval(withRemainder, 1000 / 60);\n```\n\nor you can call it on demand\n\n```javascript\nfixedDeltaTime();\nvariableDeltaTime();\nsemiFixedTimeStep();\nfixedTimeStep();\nwithRemainder();\n```\n\n# Common behaviour\n- They all accept a function that should return true if paused and false if not paused. No callbacks are executed when paused.\n- All callbacks receive the delta as the first parameter\n- All callbacks receive the elapsed game time as the second parameter.\n\n# fixed delta time\nCalls your function with a fixed time delta everytime you invoke it. You could probably just do this yourself.\n\n# variable delta time\nCalculates time between invocations and uses that as the delta.\n\n# semi fixed timestep\nAs variable but caps it at a delta you define.\n\n# fixed timestep\nSet the timestep to some number of milliseconds. Say 1000/60 for 60 frames per second. This means that the callback will _always_ receive the timestep specified on frame. Will call your function `frameDelta / ms` times\n\nAccepts an _optional_ final parameter to limit the frameDelta to some number of ms.\n\n```javascript\nconst fixedTimeStep = createFixedTimeStep(ms, isPaused, onFrame, maxMS);\n```\n\n\n# fixed timestep with remainder\nLike fixed timestep but your `onRemainder` function is called with the remainder of ms. You can determine how far through a frame you are to render the world. It's up to you. Will call your function `frameDelta / ms` times.\n\nAccepts an _optional_ final parameter to limit the frameDelta to some number of ms.\n\n```javascript\nconst withRemainder = createFixedTimeStepWithRemainder(ms, isPaused, onFrame, onRemainder, maxMS);\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistributedlife%2Fgame-loops","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdistributedlife%2Fgame-loops","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdistributedlife%2Fgame-loops/lists"}