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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use super::drawable::Drawable;
use super::color::Color;
use super::canvas::*;
use wasm_bindgen::JsValue;
pub struct Rectangle {
pub line_style: LineStyle,
pub top_left: (f64, f64),
pub dimensions: (f64, f64),
pub fill_color: Option<Color>
}
impl Rectangle {
pub fn new_with_two_points(point_a: (f64, f64), point_b: (f64, f64)) -> Rectangle {
Rectangle {
line_style: LineStyle::default(),
top_left: point_a,
dimensions: (point_b.0 - point_a.0, point_b.1 - point_a.1),
fill_color: None
}
}
pub fn new_with_dimension(point: (f64, f64), dimensions: (f64, f64)) -> Rectangle {
Rectangle {
line_style: LineStyle::default(),
top_left: point,
dimensions,
fill_color: None
}
}
}
impl Drawable for Rectangle {
fn draw_on_canvas(&self, mut canvas: &mut Canvas) {
canvas.context.begin_path();
canvas.context.rect(self.top_left.0, self.top_left.1, self.dimensions.0, self.dimensions.1);
if let Some(color) = &self.fill_color {
canvas.context.set_fill_style(&JsValue::from_str(&color.to_string()));
canvas.context.fill();
}
self.line_style.apply_on_canvas(&mut canvas);
canvas.context.stroke();
}
}
pub struct Line {
pub line_style: LineStyle,
#[allow(missing_docs)]
pub point_a: (f64, f64),
#[allow(missing_docs)]
pub point_b: (f64, f64)
}
impl Line {
pub fn new(point_a: (f64, f64), point_b: (f64, f64)) -> Line {
Line {
line_style: LineStyle::default(),
point_a,
point_b
}
}
}
impl Drawable for Line {
fn draw_on_canvas(&self, mut canvas: &mut Canvas) {
canvas.context.begin_path();
canvas.context.move_to(self.point_a.0, self.point_a.1);
canvas.context.line_to(self.point_b.0, self.point_b.1);
self.line_style.apply_on_canvas(&mut canvas);
canvas.context.stroke();
}
}