draw/
canvas.rs

1//! Top level container for a drawing.
2use crate::shape::Shape;
3use crate::{DisplayList, Drawing};
4
5/// Container for a drawing
6pub struct Canvas {
7    /// Width of the canvas in pixels
8    pub width: u32,
9    /// Height of the canvas in pixels
10    pub height: u32,
11    /// Optional background shape
12    pub background: Option<Shape>,
13    /// Display list; contains drawings ordered from bottom to top
14    pub display_list: DisplayList,
15}
16
17impl Canvas {
18    /// New empty Canvas with no background
19    pub fn new(width: u32, height: u32) -> Canvas {
20        Canvas {
21            width,
22            height,
23            background: None,
24            display_list: DisplayList::new(),
25        }
26    }
27
28    /// New canvas with a default background shape
29    pub fn with_background(width: u32, height: u32, background: Shape) -> Canvas {
30        Canvas {
31            width,
32            height,
33            background: Some(background),
34            display_list: DisplayList::new(),
35        }
36    }
37
38    /// All top level drawings contained in the Canvas
39    pub fn drawings(&self) -> &Vec<Drawing> {
40        &self.display_list.drawings
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn with_background() {
50        let canvas = Canvas::with_background(
51            50,
52            50,
53            Shape::Rectangle {
54                width: 50,
55                height: 50,
56            },
57        );
58
59        assert!(canvas.background.is_some());
60    }
61}