animate/canvas/mod.rs
1use primitives::CanvasContext;
2use std::f64::consts::PI;
3
4#[cfg(target_arch = "wasm32")]
5mod webimpl;
6#[cfg(target_arch = "wasm32")]
7pub use webimpl::*;
8
9#[cfg(not(target_arch = "wasm32"))]
10mod cairoimpl;
11#[cfg(not(target_arch = "wasm32"))]
12pub use cairoimpl::*;
13
14mod rough;
15pub use rough::*;
16
17pub trait AdvancedShapesExt {
18 // /**
19 // * Create a rectangle
20 // * Optional with rounded corners if radius is set
21 // * @param x position on the x axis in pixel
22 // * @param y position on the y axis in pixel
23 // * @param w height of the rectangle in pixel
24 // * @param h width of the rectangle in pixel
25 // * @param r (optional) radius for rounded corners
26 // * @return rectangle item instance
27 // */
28 fn round_rect(&self, x: f64, y: f64, width: f64, height: f64, radius: f64);
29 // /**
30 // * Create a symmetric polygon
31 // * @param x Position on x-axis in pixel
32 // * @param y Position on y-axis in pixel
33 // * @param n Number of edges
34 // * @param r Radius of the polygon
35 // * @return polygon item instance
36 // */
37 fn polygon(&self, x: f64, y: f64, radius: f64, n: usize);
38 // /**
39 // * Create a path
40 // * @param x start of the path at position x in pixel
41 // * @param y start of the path at position y in pixel
42 // * @return path item instance
43 // */
44 // fn path(&self, x: f64, y: f64, width: f64, height: f64, radius: f64);
45
46 // centerX, centerY: the center point of the star
47 // points: the number of points on the exterior of the star
48 // inner: the radius of the inner points of the star
49 // outer: the radius of the outer points of the star
50 // fill, stroke: the fill and stroke colors to apply
51 // line: the linewidth of the stroke
52 fn star(&self, x: f64, y: f64, points: usize, outer: f64, inner: f64);
53}
54
55impl<O> AdvancedShapesExt for O
56where
57 O: CanvasContext,
58{
59 fn round_rect(&self, x: f64, y: f64, width: f64, height: f64, radius: f64) {
60 if radius > 0.0 {
61 // self.begin_path();
62 // self.move_to(x+r, y+0);
63 // self.arc_to(x+w, y+0, x+this.rect.w, y+this.rect.r, this.rect.r);
64 // self.arc_to(x+w, y+height, x+this.rect.r, y+this.rect.h, this.rect.r);
65 // self.arc_to(x+0, y+height, x+0, y+this.rect.r, this.rect.r);
66 // self.arc_to(x+0, y+0, x+this.rect.r, y+0, this.rect.r);
67 // self.close_path();
68
69 // self.save();
70 self.begin_path();
71 self.move_to(x + radius, y);
72 self.line_to(x + width - radius, y);
73 self.quadratic_curve_to(x + width, y, x + width, y + radius);
74 self.line_to(x + width, y + height - radius);
75 self.quadratic_curve_to(x + width, y + height, x + width - radius, y + height);
76 self.line_to(x + radius, y + height);
77 self.quadratic_curve_to(x, y + height, x, y + height - radius);
78 self.line_to(x, y + radius);
79 self.quadratic_curve_to(x, y, x + radius, y);
80 self.close_path();
81 // ctx.restore();
82 } else {
83 self.begin_path(); // FIXME: ??
84 self.rect(x, y, width, height);
85 }
86 }
87
88 fn polygon(&self, x: f64, y: f64, radius: f64, n: usize) {
89 self.move_to(x, y);
90 self.begin_path();
91 let angle = PI * 2.0 / n as f64;
92 for idx in 0..n {
93 let px = x + radius * (idx as f64 * angle).cos();
94 let py = y + radius * (idx as f64 * angle).sin();
95 self.line_to(px, py);
96 }
97 self.close_path();
98 }
99
100 fn star(&self, x: f64, y: f64, points: usize, outer: f64, inner: f64) {
101 // define the star
102 self.begin_path();
103 self.move_to(x, y + outer);
104 for idx in 0..2 * points + 1 {
105 let radius = if idx % 2 == 0 { outer } else { inner };
106 let angle = PI * idx as f64 / points as f64;
107 self.line_to(x + radius * (angle).sin(), y + radius * (angle).cos());
108 }
109 self.close_path();
110 }
111}