Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created April 4, 2019 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/fa38710dedcd2566dbbfb50ee23f146a to your computer and use it in GitHub Desktop.
Save mizchi/fa38710dedcd2566dbbfb50ee23f146a to your computer and use it in GitHub Desktop.
// https://rustwasm.github.io/wasm-bindgen/examples/2d-canvas.html
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
#[wasm_bindgen(start)]
pub fn start() {
web_sys::console::log_1(&"start".into());
let document = web_sys::window().unwrap().document().unwrap();
let canvas = document.create_element("canvas").unwrap();
canvas.set_attribute("width", "400px").unwrap();
canvas.set_attribute("height", "320px").unwrap();
canvas.set_attribute("style", "background: #eee").unwrap();
let canvas: web_sys::HtmlCanvasElement = canvas
.dyn_into::<web_sys::HtmlCanvasElement>()
.map_err(|_| ())
.unwrap();
let context = canvas
.get_context("2d")
.unwrap()
.unwrap()
.dyn_into::<web_sys::CanvasRenderingContext2d>()
.unwrap();
document.body().unwrap().append_child(&canvas).unwrap();
context.begin_path();
context
.arc(10.0, 10.0, 5.0, 0.0, std::f64::consts::PI * 2.0)
.unwrap();
context.stroke();
}
#[cfg(test)]
mod tests {
// use super::*;
#[test]
fn it_works() {
assert_eq!(true, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment