Jung Ervin
Jung Ervin

Reputation: 512

Wasm-bindgen overhead for a canvas game?

I learned about how can I use the canvas from rust and wasm-bindgen (example like this: https://rustwasm.github.io/wasm-bindgen/examples/2d-canvas.html). It is pretty fast, but I am guessing still there must be an overhead for every canvas call, right? So the proper solution for a canvas-based game would be: canvas drawing calls from a javascript function, game logic running on rust/wasm, and call the js drawing function with game state data?

So I should avoid thousands of canvas draw calls from wasm-bindgen for optimal performance, right?

Upvotes: 0

Views: 750

Answers (1)

Yilmaz
Yilmaz

Reputation: 49180

It is really hard to say which approach would be the best. If you keep the canvas in rust code, then you need to worry about keeping the state in sync with calling functions. In this case, I think the event loop would had to do hard work.

Browser is our view layer. So keep the viewing job to javascript and just call the backend. It is like creating a restful API and just sending API requests to the endpoint, we get the data and javascript updates the UI. In this approach, you have to use raw pointers to access memory in rust code and this is not a safe approach.

One benefit of keeping canvas in rust code, then you can ship this code to different platforms, so your rust code will be compact and reusable.

Upvotes: 0

Related Questions