diff --git a/src/circle.rs b/src/circle.rs new file mode 100644 index 0000000..2f9b0f4 --- /dev/null +++ b/src/circle.rs @@ -0,0 +1,18 @@ +use crate::shapes::Shape; + +/// An iterator containing the shapes to display +pub struct CircleShapes; + +impl CircleShapes { + pub fn new() -> CircleShapes { + CircleShapes {} + } +} + +impl Iterator for CircleShapes { + type Item = Shape; + + fn next(&mut self) -> Option { + None + } +} diff --git a/src/drawing.rs b/src/drawing.rs index 83305cc..6533f7e 100644 --- a/src/drawing.rs +++ b/src/drawing.rs @@ -2,14 +2,22 @@ use std::f64; use crate::shapes::*; pub fn draw_circle(circle: Circle, context: &web_sys::CanvasRenderingContext2d) { - context.move_to(circle.center.x+circle.radius, circle.center.y); - context - .arc(circle.center.x, circle.center.y, circle.radius, 0.0, f64::consts::PI * 2.0) - .unwrap(); + context.move_to(circle.center.x+circle.radius, circle.center.y); + context + .arc(circle.center.x, circle.center.y, circle.radius, 0.0, f64::consts::PI * 2.0) + .unwrap(); } pub fn draw_line(line: Line, context: &web_sys::CanvasRenderingContext2d) { - context.move_to(line.p1.x, line.p1.y); - context - .line_to(line.p2.x, line.p2.y); + context.move_to(line.p1.x, line.p1.y); + context + .line_to(line.p2.x, line.p2.y); +} + +pub fn draw_shape(shape: Shape, context: &web_sys::CanvasRenderingContext2d) { + match shape { + Shape::Circle(circle) => draw_circle(circle, context), + Shape::Line(line) => draw_line(line, context), + Shape::Point(_) => (), + } } diff --git a/src/lib.rs b/src/lib.rs index d9c7fa3..699bf03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,33 +3,40 @@ use wasm_bindgen::JsCast; mod drawing; mod shapes; +mod circle; use shapes::*; +use circle::CircleShapes; #[wasm_bindgen(start)] pub fn start() { - let document = web_sys::window().unwrap().document().unwrap(); - let canvas = document.get_element_by_id("canvas").unwrap(); - let canvas: web_sys::HtmlCanvasElement = canvas - .dyn_into::() - .map_err(|_| ()) - .unwrap(); + let document = web_sys::window().unwrap().document().unwrap(); + let canvas = document.get_element_by_id("canvas").unwrap(); + let canvas: web_sys::HtmlCanvasElement = canvas + .dyn_into::() + .map_err(|_| ()) + .unwrap(); - let context = canvas - .get_context("2d") - .unwrap() - .unwrap() - .dyn_into::() - .unwrap(); + let context = canvas + .get_context("2d") + .unwrap() + .unwrap() + .dyn_into::() + .unwrap(); - context.begin_path(); - - drawing::draw_circle(Circle {center: Point {x: 75.0, y: 75.0}, radius: 70.0}, &context); - drawing::draw_circle(Circle {center: Point {x: 75.0, y: 75.0}, radius: 35.0}, &context); - drawing::draw_circle(Circle {center: Point {x: 60.0, y: 65.0}, radius: 5.0}, &context); - - drawing::draw_line(Line {p1: Point {x: 0.0, y: 0.0}, p2: Point {x: 150.0, y: 150.0}}, &context); - drawing::draw_line(Line {p1: Point {x: 0.0, y: 150.0}, p2: Point {x: 150.0, y: 0.0}}, &context); - - context.stroke(); + context.begin_path(); + + let iter = CircleShapes::new(); + for shape in iter { + drawing::draw_shape(shape, &context); + } + + drawing::draw_circle(Circle {center: Point {x: 75.0, y: 75.0}, radius: 70.0}, &context); + drawing::draw_circle(Circle {center: Point {x: 75.0, y: 75.0}, radius: 35.0}, &context); + drawing::draw_circle(Circle {center: Point {x: 60.0, y: 65.0}, radius: 5.0}, &context); + + drawing::draw_line(Line {p1: Point {x: 0.0, y: 0.0}, p2: Point {x: 150.0, y: 150.0}}, &context); + drawing::draw_line(Line {p1: Point {x: 0.0, y: 150.0}, p2: Point {x: 150.0, y: 0.0}}, &context); + + context.stroke(); } diff --git a/src/shapes.rs b/src/shapes.rs index 7de20fd..b62de6a 100644 --- a/src/shapes.rs +++ b/src/shapes.rs @@ -12,3 +12,9 @@ pub struct Line { pub p1: Point, pub p2: Point, } + +pub enum Shape { + Point(Point), + Circle(Circle), + Line(Line), +}