web_sys::HtmlCanvasElement::get_context function

Example

Get the drawing context on the canvas.


pub fn htmlcanvaselement_get_context()
{
    let document = web_sys::window().unwrap().document().unwrap();
    let canvas = document.get_element_by_id("canvas").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();

    context.begin_path();

    context
        .rect(75.0, 75.0, 50.0, 50.0);

    context.stroke();

}

Function


	pub fn get_context(&self, context_id: &str) -> Result<Option<Object>, JsValue>

features/dependencies

Source Code