arwa/html/
html_canvas_element.rs

1use std::convert::TryFrom;
2
3use delegate::delegate;
4use wasm_bindgen::JsCast;
5
6use crate::error::SecurityError;
7use crate::html::{GenericHtmlElement, HtmlElement};
8use crate::{
9    Element, GenericElement, GenericNode, GlobalEventHandlers, ImageQuality, InvalidCast, Node,
10};
11
12#[derive(Clone)]
13pub struct HtmlCanvasElement {
14    inner: web_sys::HtmlCanvasElement,
15}
16
17impl HtmlCanvasElement {
18    delegate! {
19        target self.inner {
20            pub fn width(&self) -> u32;
21
22            pub fn set_width(&self, width: u32);
23
24            pub fn height(&self) -> u32;
25
26            pub fn set_height(&self, height: u32);
27        }
28    }
29
30    // TODO: it's unclear from the WHATWG spec if there can also be an encoding error, or if that
31    // would only result in an empty string, needs to be experimented with.
32    pub fn to_data_url(
33        &self,
34        mime_type: &str,
35        quality: ImageQuality,
36    ) -> Result<String, SecurityError> {
37        let quality: f64 = quality.into();
38
39        self.inner
40            .to_data_url_with_type_and_encoder_options(mime_type, &quality.into())
41            .map_err(|err| {
42                let err: web_sys::DomException = err.unchecked_into();
43
44                SecurityError::new(err)
45            })
46    }
47
48    // TODO: figure our what to do about obtaining contexts. Leave that to external crates?
49
50    // TODO: to_blob. The javascript version takes a callback that receives the blob, rather than
51    // returning a blob. There is no indication that the blob can't outlive this callback though,
52    // this needs to be experimented with.
53}
54
55impl_html_common_traits!(HtmlCanvasElement);