1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use webcore::value::{Reference, ConversionError};
use webcore::try_from::TryInto;
use webapi::html_elements::CanvasElement;
/// Trait implemented by rendering contexts which can be obtained from a canvas.
pub trait RenderingContext {
/// Type of error which can occur whilst creating this context
type Error;
/// Name which identifies this kind of rendering context.
fn from_canvas(canvas: &CanvasElement) -> Result<Self, Self::Error> where Self: Sized;
}
/// Used for drawing rectangles, text, images and other objects onto the canvas element.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D)
// https://html.spec.whatwg.org/#canvasrenderingcontext2d
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "CanvasRenderingContext2D")]
pub struct CanvasRenderingContext2d(Reference);
impl RenderingContext for CanvasRenderingContext2d {
type Error = ConversionError;
fn from_canvas(canvas: &CanvasElement) -> Result<Self, ConversionError> {
js!(
return @{canvas}.getContext("2d");
).try_into()
}
}
impl CanvasRenderingContext2d {
/// Draws a filled rectangle whose starting point is at the coordinates (x, y) with the
/// specified width and height and whose style is determined by the fillStyle attribute.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillRect)
// https://html.spec.whatwg.org/#2dcontext:dom-context-2d-fillrect
pub fn fill_rect(&self, x: f64, y: f64, width: f64, height: f64) {
js! { @(no_return)
@{&self.0}.fillRect(@{x}, @{y}, @{width}, @{height});
}
}
/// Draws a text string at the specified coordinates, filling the string's characters
/// with the current foreground color. An optional parameter allows specifying a maximum
/// width for the rendered text, which the user agent will achieve by condensing the
/// text or by using a lower font size.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillText)
// https://html.spec.whatwg.org/#2dcontext:dom-context-2d-filltext
pub fn fill_text(&self, text: &str, x: f64, y: f64, max_width: Option<f64>) {
if let Some(max_width) = max_width {
js! { @(no_return)
@{&self.0}.fillText(@{text}, @{x}, @{y}, @{max_width});
}
}
else {
js! { @(no_return)
@{&self.0}.fillText(@{text}, @{x}, @{y});
}
}
}
}