Draw shapes and text using Canvas Web API or wgpu
Questions ▷ [email protected]
Fast2D is a Rust library for efficient 2D graphics rendering in browsers and webviews via WebAssembly. It provides a simple API for drawing shapes and text.
- Draw rectangles, circles, lines, and text
- More shapes and tools will likely be supported in the future as needed
- Multiple rendering backends: WebGL, WebGPU, Canvas API
- Easy font loading and registration
- Tested with MoonZoon and Tauri in examples and in the NovyWave app
- Lyon is used for tessellation (dividing shapes into triangles), Glyphon for text rendering
| OS | Browser | WebGL | WebGPU | Canvas API |
|---|---|---|---|---|
| Windows | Edge | ✅ | ✅ | ✅ |
| Chrome | ✅ | ✅ | ✅ | |
| Firefox | ✅ | ❌ | ✅ | |
| Tauri | ✅ | ✅ | ✅ | |
| macOS | Safari | ✅ | ❌ | ✅ |
| Chrome | ✅ | ✅ | ✅ | |
| Tauri | ✅ | ❌ | ✅ | |
| Linux | Chrome | ✅ | ❌ | ✅ |
| Firefox | ✅ | ❌ | ✅ | |
| Tauri | ✅ | ❌ | ✅ |
Add Fast2D to your Cargo.toml:
fast2d = { git = "https://github.com/FastWaveViewer/Fast2D", features = ["webgl"] }- Choose one of these features:
"webgl","webgpu","canvas". - Add the
revattribute to select a specific commit. - The library will be published to crates.io later.
Note: Code snippets inspired by browser_example/frontend/src/main.rs
-
Fetch and register fonts
let fonts = try_join_all([ fast2d::fetch_file("/_api/public/fonts/Inter-Regular.ttf"), fast2d::fetch_file("/_api/public/fonts/Inter-Bold.ttf"), ]).await.unwrap_throw(); fast2d::register_fonts(fonts).unwrap_throw();
-
Define objects to render
let mut example_objects: Vec<fast2d::Object2d> = vec![ fast2d::Rectangle::new() .position(50., 50.) .size(200., 150.) .color(50, 0, 100, 1.0) .into(), fast2d::Text::new() .text("Simple Rectangle") .position(10., 50.) .size(350., 120.) .color(255, 255, 255, 0.2) .font_size(60.) .family(fast2d::Family::name("Inter")) .into(), ];
-
Wrap your HtmlCanvasElement
let mut canvas_wrapper = fast2d::CanvasWrapper::new_with_canvas(canvas).await;
-
Draw defined objects
canvas_wrapper.update_objects(move |objects| { mem::swap(objects, &mut example_objects) });
-
Notify
CanvasWrapperon canvas size changecanvas_wrapper.resized(width, height);
Note: Size change listener hasn't been implemented directly in this library (yet?), look at MoonZoon's resize_observer.rs for inspiration.
See the browser_example and tauri_example for instructions.
| Compression | WebGL | WebGPU | Canvas API |
|---|---|---|---|
| - | 2903 KB | 1432 KB | 237 KB |
| Gzip | 1100 KB | 576 KB | 106 KB |
| Brotli | 847 KB | 445 KB | 88 KB |
This project is licensed under the MIT License.
This project is funded through NGI Zero Core, a fund established by NLnet with financial support from the European Commission's Next Generation Internet program. Learn more at the NLnet project page.

