Rust canvas
If you're looking to work with graphics in Rust, particularly to create a canvas for rendering, there are several libraries available that can help you with this. Here are a few options to consider:
### 1. **GGEZ**
- **Description**: A lightweight game framework for Rust, GGEZ aims to make it easier to create 2D games.
- **Features**: Simple to use with concepts familiar to game developers, like audio, graphics, and input handling.
- **Usage**: You can draw shapes and images easily on the screen using GGEZ.
```rust
use ggez::{Context, ContextBuilder, GameResult};
use ggez::graphics::{self, Color};
struct MyGame;
impl ggez::event::EventHandler for MyGame {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
graphics::clear(ctx, Color::from_rgb(255, 255, 255));
// Draw your shapes here...
graphics::present(ctx)?;
Ok(())
}
}
fn main() {
let (ctx, event_loop) = ContextBuilder::new("my_game", "author").build().unwrap();
let mut my_game = MyGame;
ggez::event::run(ctx, event_loop, my_game).unwrap();
}
```
### 2. **SDL2**
- **Description**: SDL2 is a cross-platform development library that provides low-level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. There's a Rust binding available.
- **Usage**: Suitable for more robust applications and games requiring more detailed control over rendering and input.
```rust
extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::rect::Rect;
fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem
.window("SDL2 Rust Canvas", 800, 600)
.position_centered()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
canvas.set_draw_color(Color::RGB(255, 255, 255));
canvas.clear();
canvas.set_draw_color(Color::RGB(0, 0, 255));
canvas.fill_rect(Rect::new(10, 10, 100, 100)).unwrap();
canvas.present();
// Wait for a while before closing
std::thread::sleep(std::time::Duration::from_secs(2));
}
```
### 3. **Piston**
- **Description**: A modular game engine in Rust, suitable for 2D graphics.
- **Features**: Simple and flexible design, with support for various backends.
```rust
use piston_window::*;
fn main() {
let mut window: PistonWindow = WindowSettings::new("Piston Window", [800, 600])
.exit_on_esc(true)
.build()
.unwrap();
while let Some(event) = window.next() {
window.draw_2d(&event, |c, g, _| {
clear([1.0, 1.0, 1.0, 1.0], g);
rectangle([0.0, 0.0, 1.0, 1.0], [10.0, 10.0, 100.0, 100.0], c.transform, g);
});
}
}
```
### 4. **WebAssembly with `wasm-bindgen` and `canvas`**
If you're looking at web applications, you can use Rust to generate WebAssembly (Wasm). The `wasm-bindgen` library helps you bind Rust code to JavaScript, including HTML5 Canvas.
- **Setup**: You'll need to set up a Rust project to compile to Wasm and integrate with HTML and JavaScript.
```rust
use wasm_bindgen::prelude::*;
use web_sys::CanvasRenderingContext2d;
#[wasm_bindgen]
pub fn start() {
let document = web_sys::window().unwrap().document().unwrap();
let canvas = document.get_element_by_id("my_canvas").unwrap();
let context = canvas
.dyn_into::<web_sys::HtmlCanvasElement>()
.unwrap()
.get_context("2d")
.unwrap()
.unwrap()
.dyn_into::<CanvasRenderingContext2d>()
.unwrap();
context.set_fill_style(&JsValue::from_str("blue"));
context.fill_rect(10.0, 10.0, 100.0, 100.0);
}
```
### Conclusion
Choose the library that best fits your project’s needs. For 2D games or applications, **GGEZ** or **Piston** could be great choices, while **SDL2** offers more control for lower-level graphics. If you're targeting web applications, consider using Rust with **wasm-bindgen** to work with HTML5 Canvas directly.