Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

WebGL helpers

Pyodide-only WebGL bridge for imgui_bundle.

Available only in Pyodide builds (compile-time gated by IMGUI_BUNDLE_WITH_WEBGL). On desktop, importing this submodule raises ImportError; gate with imgui_bundle.has_submodule("webgl").

Typical usage:

import imgui_bundle
if imgui_bundle.has_submodule("webgl"):
    from imgui_bundle import webgl
    from js import document
    gl = document.getElementById("canvas").getContext("webgl2")

    tex = gl.createTexture()
    # ... populate the texture via WebGL ...
    tex_id = webgl.register_texture(tex)
    # ... in the GUI loop ...
    imgui.image(imgui.ImTextureRef(tex_id), ImVec2(256, 256))
    # ... at shutdown ...
    webgl.unregister_texture(tex_id)
    gl.deleteTexture(tex)

See _plans/pyodide_webgl_bridge__spec.md for the design rationale.

Upstream repository · The book’s page

Modules

In imgui_bundle (from imgui_bundle import webgl):

Demos using it

The demos of the catalog that use it (the pictures link to their code and to the playground).

WebGL shader as background

WebGL shader as background

The Mandelbrot set, drawn by a fragment shader behind the GUI.

More

Pyodide only: this demo calls the browser’s WebGL2 API directly, through Python’s js interop. On desktop, you would use PyOpenGL instead.

Browser only

Uses: browser APIs

Python: ▶︎ Run · Code

WebGL / OpenGL custom background

WebGL / OpenGL custom background

One source file, two platforms: a shader behind the GUI, with OpenGL on desktop and WebGL in the browser.

More

The shader is the famous “Seascape” from Shadertoy, by Alexander Alekseev. In the browser, the app runs with Pyodide.

Uses: OpenGL, browser APIs

Python: ▶︎ Run · Code

WebGL 3D cube rendered to a texture

WebGL 3D cube rendered to a texture

A 3D cube drawn with WebGL2 into a texture, and shown in the GUI by imgui.image().

More

No copy between the GPU and the CPU at each frame.

Browser only

Uses: browser APIs

Python: ▶︎ Run · Code

WebAudio synthesizer

WebAudio synthesizer

A small synthesizer written in Python: play the piano with the mouse or the computer keyboard, or let it play a tune.

More

Pick a waveform, add some echo, and watch the scope. Pyodide only: it plays through the browser’s WebAudio API, via Pyodide’s js interop.

Browser only

Uses: knobs, browser APIs

Python: ▶︎ Run · Code