#emscripten #wasm32 #main-loop


Main loop abstraction that works for both native and Emscripten builds. Gets around the issue of freezing up browser tabs when a naive main loop is used.

2 releases

0.1.1 Oct 2, 2020
0.1.0 Oct 2, 2020

#4 in #wasm32

Download history 7/week @ 2023-12-15 6/week @ 2023-12-22 1/week @ 2023-12-29 3/week @ 2024-01-05 9/week @ 2024-01-12 4/week @ 2024-01-19 1/week @ 2024-01-26 1/week @ 2024-02-02 9/week @ 2024-02-09 26/week @ 2024-02-16 32/week @ 2024-02-23 23/week @ 2024-03-01 33/week @ 2024-03-08 21/week @ 2024-03-15 24/week @ 2024-03-22 49/week @ 2024-03-29

129 downloads per month

Custom license

96 lines

Emscripten Main Loop Helper Library



Many interactive applications such as games will utilise the technique of having a main loop. This loop will represent one "tick" of the simulation and often also capture input and render output. However, the typical implementation of this technique (simple loop/while statement) breaks down when targeting Emscripten since naive looping will cause the browser tab to freeze up. This library provides a simple trait to use as a replacement for that loop statement and will make sure that the looping is happening in the way that Emscripten needs it to.


Add emscripten_main_loop as a dependency to your Cargo.toml.

Implement the emscripten_main_loop::MainLoop trait for your application object that contains all the data that should be accessible to the main loop. The trait will require you to implement the main_loop function which is where you are meant to put your looping logic. main_loop will be called once per loop iteration and must return either MainLoopEvent::Continue or MainLoopEvent::Terminate as appropriate.

When the trait is implemented, you can invoke the looping by passing your data object to emscripten_main_loop::run(). See the documentation for further information, or check out this example project as a reference, which uses this library.

I don't know how to use Emscripten yet

For a comprehensive guide on how to use Emscripten to build a project using Rust+SDL2+OpenGL for the web, check out my blog post.

I think this library can be improved

Feel free to raise issues or pull requests as desired, contributions would be appreciated.

No runtime deps