create iterator structure
This commit is contained in:
parent
9b2845ca51
commit
58e5efa58d
4 changed files with 68 additions and 29 deletions
18
src/circle.rs
Normal file
18
src/circle.rs
Normal file
|
@ -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<Self::Item> {
|
||||
None
|
||||
}
|
||||
}
|
|
@ -13,3 +13,11 @@ pub fn draw_line(line: Line, context: &web_sys::CanvasRenderingContext2d) {
|
|||
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(_) => (),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,10 @@ use wasm_bindgen::JsCast;
|
|||
|
||||
mod drawing;
|
||||
mod shapes;
|
||||
mod circle;
|
||||
|
||||
use shapes::*;
|
||||
use circle::CircleShapes;
|
||||
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn start() {
|
||||
|
@ -24,6 +26,11 @@ pub fn start() {
|
|||
|
||||
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);
|
||||
|
|
|
@ -12,3 +12,9 @@ pub struct Line {
|
|||
pub p1: Point,
|
||||
pub p2: Point,
|
||||
}
|
||||
|
||||
pub enum Shape {
|
||||
Point(Point),
|
||||
Circle(Circle),
|
||||
Line(Line),
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue