Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created April 5, 2019 07:40
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/bef7e325540005fb9f3d3b87dbbecdc7 to your computer and use it in GitHub Desktop.
Save mizchi/bef7e325540005fb9f3d3b87dbbecdc7 to your computer and use it in GitHub Desktop.
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
fn run() -> Result<(), JsValue> {
let document = web_sys::window().unwrap().document().unwrap();
let canvas = document
.create_element("canvas")
.unwrap()
.unchecked_into::<web_sys::HtmlCanvasElement>();
canvas.set_attribute("width", "400px")?;
canvas.set_attribute("height", "320px")?;
canvas.set_attribute("style", "background: #eee")?;
let context = canvas
.get_context("2d")?
.unwrap()
.unchecked_into::<web_sys::CanvasRenderingContext2d>();
document.body().unwrap().append_child(&canvas)?;
context.translate(0.5, 0.5)?;
context.begin_path();
context.arc(10.0, 10.0, 5.0, 0.0, std::f64::consts::PI * 2.0)?;
context.stroke();
Ok(())
}
#[wasm_bindgen(start)]
pub fn start() {
run().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment