Search code examples
htmlrustwebglwebassemblybevy

Render WASM Graphics to Custom HTML Canvas


I have setup a WASM project using Rust and a game engine called Bevy to create graphics within a Svelte app. However, when I run the init() function generated by wasm-pack, it creates a canvas element for the graphics to be rendered into. Is there any way to make it render to a canvas I have created or to style the canvas it generates?


Solution

  • You should be able to specify the canvas bevy should render to by setting the WindowDescriptor's canvas field

    The docs say "If set, this selector will be used to find a matching html canvas element, rather than creating a new one. Uses the CSS selector format."

    When you create the WindowDescriptor add the canvas selector as a field.

    let window_descriptor = WindowDescriptor {
        canvas: "#mycanvas",
        ..default()
    };