Support multiple WebGL contexts
- Dominant language
- Rust
- Stars
- 1.8k
- Forks
- 194
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 2
Description
Right now, kiss3d only supports rendering to a single `` element. I have a use case where I'd like to embed multiple instances of kiss3d on the same page (a blog post that goes through implementing a 3d simulation).
I've identified a few blockers:
* [WebGLCanvas always looks for a single canvas on the page](https://github.com/sebcrozet/kiss3d/blob/48985e838f4946aacf2db269909f8c5a95455f9c/src/window/webgl_canvas.rs#L46) (`#canvas`). This can be worked around on the JavaScript side by changing the id of the target canvas before creating the window, and then changing it back afterwards.
* [WebGLContext does the same thing](https://github.com/sebcrozet/kiss3d/blob/48985e838f4946aacf2db269909f8c5a95455f9c/src/context/webgl_context.rs#L27). Now this one is more complicated to work around because [kiss3d only ever keeps a single context in memory](https://github.com/sebcrozet/kiss3d/blob/48985e838f4946aacf2db269909f8c5a95455f9c/src/context/context.rs#L116). One solution to this is to instantiate one WASM module per canvas (+ the trick for supporting multiple canvases).
* Mouse and keyboard events are relayed to all canvases because [they all listen on window](https://github.com/sebcrozet/kiss3d/blob/48985e838f4946aacf2db269909f8c5a95455f9c/src/window/webgl_canvas.rs#L78). As a temporary fix on my fork, I've added the following check inside the event handlers:
```rust
if e.target()
.and_then(|target| ::try_from(target).ok())
.map_or(true, |canvas| canvas != edata.canvas)
{
return;
}
```
**EDIT:** For the last point, a better solution is to add event listeners to the canvas element directly (except for window resize / mouse up / mouse move (which needs to be modified to support the pointer leaving the canvas).
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.