diff --git a/src/drawing.rs b/src/drawing.rs index b2a5811..83305cc 100644 --- a/src/drawing.rs +++ b/src/drawing.rs @@ -1,15 +1,15 @@ use std::f64; -use crate::shapes::Point; +use crate::shapes::*; -pub fn draw_circle(center: Point, radius: f64, context: &web_sys::CanvasRenderingContext2d) { - context.move_to(center.x+radius, center.y); +pub fn draw_circle(circle: Circle, context: &web_sys::CanvasRenderingContext2d) { + context.move_to(circle.center.x+circle.radius, circle.center.y); context - .arc(center.x, center.y, radius, 0.0, f64::consts::PI * 2.0) + .arc(circle.center.x, circle.center.y, circle.radius, 0.0, f64::consts::PI * 2.0) .unwrap(); } -pub fn draw_line(p1: Point, p2: Point, context: &web_sys::CanvasRenderingContext2d) { - context.move_to(p1.x, p1.y); +pub fn draw_line(line: Line, context: &web_sys::CanvasRenderingContext2d) { + context.move_to(line.p1.x, line.p1.y); context - .line_to(p2.x, p2.y); + .line_to(line.p2.x, line.p2.y); } diff --git a/src/lib.rs b/src/lib.rs index b971834..d9c7fa3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ use wasm_bindgen::JsCast; mod drawing; mod shapes; -use shapes::Point; +use shapes::*; #[wasm_bindgen(start)] pub fn start() { @@ -24,12 +24,12 @@ pub fn start() { context.begin_path(); - drawing::draw_circle(Point {x: 75.0, y: 75.0}, 70.0, &context); - drawing::draw_circle(Point {x: 75.0, y: 75.0}, 35.0, &context); - drawing::draw_circle(Point {x: 60.0, y: 65.0}, 5.0, &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(Point {x: 0.0, y: 0.0}, Point {x: 150.0, y: 150.0}, &context); - drawing::draw_line(Point {x: 0.0, y: 150.0}, Point {x: 150.0, y: 0.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 e22a300..7de20fd 100644 --- a/src/shapes.rs +++ b/src/shapes.rs @@ -7,3 +7,8 @@ pub struct Circle { pub center: Point, pub radius: f64, } + +pub struct Line { + pub p1: Point, + pub p2: Point, +}