stdweb/webapi/html_elements/canvas.rs
1use webcore::value::Reference;
2use webcore::try_from::TryInto;
3use webcore::once::Once;
4use webapi::event_target::{IEventTarget, EventTarget};
5use webapi::node::{INode, Node};
6use webapi::element::{IElement, Element};
7use webapi::html_element::{IHtmlElement, HtmlElement};
8use webapi::blob::Blob;
9use webapi::rendering_context::RenderingContext;
10use private::TODO;
11
12/// The HTML `<canvas>` element provides an empty graphic zone on which specific JavaScript APIs
13/// can draw (such as Canvas 2D or WebGL).
14///
15/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement)
16// https://html.spec.whatwg.org/#htmlcanvaselement
17#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
18#[reference(instance_of = "HTMLCanvasElement")]
19#[reference(subclass_of(EventTarget, Node, Element, HtmlElement))]
20pub struct CanvasElement( Reference );
21
22impl IEventTarget for CanvasElement {}
23impl INode for CanvasElement {}
24impl IElement for CanvasElement {}
25impl IHtmlElement for CanvasElement {}
26
27impl CanvasElement {
28 /// Returns a positive integer reflecting the height HTML attribute of the <canvas> element
29 /// interpreted in CSS pixels. When the attribute is not specified, or if it is set to an
30 /// invalid value, like a negative, the default value of 150 is used.
31 ///
32 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/height)
33 // https://html.spec.whatwg.org/#the-canvas-element:dom-canvas-height
34 pub fn height( &self ) -> u32 {
35 js! (
36 return @{self}.height;
37 ).try_into().unwrap()
38 }
39
40 /// Sets a positive integer reflecting the height HTML attribute of the <canvas> element
41 /// interpreted in CSS pixels. When the attribute is not specified, or if it is set to an
42 /// invalid value, like a negative, the default value of 150 is used.
43 ///
44 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/height)
45 // https://html.spec.whatwg.org/#the-canvas-element:dom-canvas-height
46 pub fn set_height( &self, value: u32 ) {
47 js! { @(no_return)
48 @{self}.height = @{value};
49 }
50 }
51
52 /// Returns a positive integer reflecting the width HTML attribute of the <canvas> element
53 /// interpreted in CSS pixels. When the attribute is not specified, or if it is set to an
54 /// invalid value, like a negative, the default value of 300 is used.
55 ///
56 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/width)
57 // https://html.spec.whatwg.org/#the-canvas-element:dom-canvas-width
58 pub fn width( &self ) -> u32 {
59 js! (
60 return @{self}.width;
61 ).try_into().unwrap()
62 }
63
64 /// Sets a positive integer reflecting the width HTML attribute of the <canvas> element
65 /// interpreted in CSS pixels. When the attribute is not specified, or if it is set to an
66 /// invalid value, like a negative, the default value of 300 is used.
67 ///
68 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/width)
69 // https://html.spec.whatwg.org/#the-canvas-element:dom-canvas-width
70 pub fn set_width( &self, value: u32 ) {
71 js! { @(no_return)
72 @{self}.width = @{value};
73 }
74 }
75
76 /// Returns a drawing context on the canvas, or None if the context identifier is not supported.
77 ///
78 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext)
79 // https://html.spec.whatwg.org/#the-canvas-element:dom-canvas-getcontext
80 pub fn get_context<T: RenderingContext>( &self ) -> Result<T, T::Error> {
81 T::from_canvas(self)
82 }
83
84 /// Returns a data URI containing a representation of the image in the format specified by the
85 /// type parameter (defaults to PNG). The returned image is in a resolution of 96 dpi.
86 ///
87 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataUrl)
88 // https://html.spec.whatwg.org/#the-canvas-element:dom-canvas-todataurl
89 pub fn to_data_url( &self, mime_type: Option<&str>, quality: Option<f64> ) -> Result< String, TODO > {
90 Ok( js! (
91 return @{self}.toDataURL(@{mime_type}, @{quality});
92 ).try_into().unwrap() )
93 }
94
95 /// Creates a Blob object representing the image contained in the canvas; this file may be
96 /// cached on the disk or stored in memory at the discretion of the user agent.
97 ///
98 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob)
99 // https://html.spec.whatwg.org/#the-canvas-element:dom-canvas-toblob
100 pub fn to_blob<F: FnOnce(Blob) + 'static>( &self, f: F, mime_type: Option<&str>, quality: Option<f64> ) -> Result< (), TODO > {
101 js! { @(no_return)
102 @{self}.toBlob(@{Once(f)}, @{mime_type}, @{quality});
103 }
104
105 Ok(())
106 }
107}