diff --git a/crates/test-web/src/main.rs b/crates/test-web/src/main.rs index 81c4420..87d64ed 100644 --- a/crates/test-web/src/main.rs +++ b/crates/test-web/src/main.rs @@ -3,7 +3,7 @@ use wasm_bindgen::JsCast; use wasm_draw::init_app; // mod lib; -use web_sys::{window, Element, HtmlCanvasElement, HtmlElement}; +use web_sys::{window, HtmlCanvasElement}; fn main() { console_error_panic_hook::set_once(); diff --git a/crates/test-web/tests/lib.rs b/crates/test-web/tests/lib.rs index e69de29..8b13789 100644 --- a/crates/test-web/tests/lib.rs +++ b/crates/test-web/tests/lib.rs @@ -0,0 +1 @@ + diff --git a/crates/wasm-draw/src/app.rs b/crates/wasm-draw/src/app.rs index 04bded5..c9da39b 100644 --- a/crates/wasm-draw/src/app.rs +++ b/crates/wasm-draw/src/app.rs @@ -4,15 +4,13 @@ use crate::state::get_element_dimensions; use crate::state::Dimensions; use crate::state::State; use crate::tool::ToolType; -use serde::{Deserialize, Serialize}; use std::cell::RefCell; -use std::cmp::{max, min}; use std::rc::Rc; use utilities::console_log; use wasm_bindgen::prelude::*; use web_sys::{ - console, window, CanvasRenderingContext2d, Element, HtmlCanvasElement, HtmlElement, MouseEvent, + window, CanvasRenderingContext2d, HtmlCanvasElement, MouseEvent, }; use crate::tool::Measurement; diff --git a/crates/wasm-draw/src/canvas.rs b/crates/wasm-draw/src/canvas.rs index 77f20e7..016ad2f 100644 --- a/crates/wasm-draw/src/canvas.rs +++ b/crates/wasm-draw/src/canvas.rs @@ -1,15 +1,12 @@ extern crate wasm_bindgen; -use crate::log; use crate::state::State; // use crate::tool::Line; // use crate::tool::ToolBelt; use std::cell::RefCell; use std::rc::Rc; -use utilities::console_log; use wasm_bindgen::prelude::*; -use wasm_bindgen::JsCast; -use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement, MouseEvent}; +use web_sys::{HtmlCanvasElement}; pub fn init_canvas(canvas: &HtmlCanvasElement, state: &Rc>) -> Result<(), JsValue> { canvas.set_width(state.borrow().get_width()); canvas.set_height(state.borrow().get_height()); diff --git a/crates/wasm-draw/src/lib.rs b/crates/wasm-draw/src/lib.rs index 33e6a70..afe122e 100644 --- a/crates/wasm-draw/src/lib.rs +++ b/crates/wasm-draw/src/lib.rs @@ -1,12 +1,7 @@ extern crate wasm_bindgen; -use std::cell::RefCell; -use std::cmp::{max, min}; -use std::rc::Rc; -use utilities::console_log; use wasm_bindgen::prelude::*; -use wasm_bindgen::JsCast; -use web_sys::{window, Element, HtmlCanvasElement, HtmlElement}; +use web_sys::{window, HtmlCanvasElement}; mod app; mod canvas; mod draw_engine; diff --git a/crates/wasm-draw/src/settings.rs b/crates/wasm-draw/src/settings.rs index b9ebd99..974be3e 100644 --- a/crates/wasm-draw/src/settings.rs +++ b/crates/wasm-draw/src/settings.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use wasm_bindgen::prelude::*; pub static COLORS: [(&str, &str); 6] = [ ("Black", "#000000"), @@ -10,12 +8,10 @@ pub static COLORS: [(&str, &str); 6] = [ ("White", "#FFFFFF"), ]; -static DEFAULT_COLOR: &str = COLORS[0].1; -use crate::log; -use js_sys::Array; +pub static DEFAULT_COLOR: &str = COLORS[0].1; + use serde::{Deserialize, Serialize}; -use utilities::console_log; -use web_sys::{console, js_sys, window, Element, HtmlCanvasElement, HtmlElement, MouseEvent}; + pub static PEN_SIZES: [f64; 4] = [1.0, 2.0, 4.0, 8.0]; static DEFAULT_PEN_SIZE: f64 = PEN_SIZES[0]; @@ -47,10 +43,16 @@ pub struct Settings { pub tools: Vec, } -#[wasm_bindgen] +// #[wasm_bindgen] +impl Default for Settings { + fn default() -> Self { + Self::new() + } +} + impl Settings { - #[wasm_bindgen(constructor)] - pub fn new() -> Self { + // #[wasm_bindgen(constructor)] + pub fn new() -> Settings { let colors = COLORS .iter() .map(|(name, hex)| Color { @@ -59,7 +61,7 @@ impl Settings { }) .collect(); - Self { + Settings { colors, pen_sizes: PEN_SIZES.to_vec(), tools: vec!["pen".to_string(), "line".to_string(), "circle".to_string()], diff --git a/crates/wasm-draw/src/state.rs b/crates/wasm-draw/src/state.rs index c3f88c4..c8257ef 100644 --- a/crates/wasm-draw/src/state.rs +++ b/crates/wasm-draw/src/state.rs @@ -1,18 +1,11 @@ use wasm_bindgen::prelude::*; -pub static COLORS: [(&str, &str); 6] = [ - ("Black", "#000000"), - ("Green", "#3DC06C"), - ("Red", "#FF0000"), - ("Blue", "#4F8DE4"), - ("Yellow", "#FAE589"), - ("White", "#FFFFFF"), -]; + use serde::{Deserialize, Serialize}; -static DEFAULT_COLOR: &str = COLORS[0].1; -use crate::settings::Settings; + +use crate::settings::{Settings, DEFAULT_COLOR}; use crate::{log, tool::ToolType}; use utilities::console_log; -use web_sys::{console, window, Element, HtmlCanvasElement, HtmlElement, MouseEvent}; +use web_sys::{HtmlCanvasElement, HtmlElement, MouseEvent}; pub static PEN_SIZES: [f64; 4] = [1.0, 2.0, 4.0, 8.0]; static DEFAULT_PEN_SIZE: f64 = PEN_SIZES[0]; @@ -56,7 +49,7 @@ pub struct State { #[wasm_bindgen] impl State { pub fn new(canvas_el: &HtmlCanvasElement, settings: Settings) -> State { - let dimensions = get_element_dimensions(&canvas_el); + let dimensions = get_element_dimensions(canvas_el); console_log!("dimensions: {:?}", dimensions); State { dimensions: dimensions.clone(), diff --git a/crates/wasm-draw/src/tool.rs b/crates/wasm-draw/src/tool.rs index 3f90057..32436c5 100644 --- a/crates/wasm-draw/src/tool.rs +++ b/crates/wasm-draw/src/tool.rs @@ -1,14 +1,6 @@ -use crate::log; -use crate::settings; -use crate::state::Dimensions; -use crate::state::State; use serde::{Deserialize, Serialize}; -use std::cell::RefCell; -use std::cmp::{max, min}; -use std::rc::Rc; -use utilities::console_log; use wasm_bindgen::prelude::*; -use web_sys::{console, window, CanvasRenderingContext2d, Element, HtmlCanvasElement, HtmlElement}; +use web_sys::{CanvasRenderingContext2d}; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] #[wasm_bindgen] @@ -56,12 +48,10 @@ impl Measurement { } pub fn finish(&mut self) { - console_log!("finishing measurement"); if self.tool != ToolType::Pen && self.points.len() > 2 { let last_point = self.points.last().unwrap(); let first_point = self.points.first().unwrap(); self.points = vec![first_point.clone(), last_point.clone()]; - console_log!("finished measurement: {:?}", self); } } @@ -100,3 +90,23 @@ impl Measurement { context.stroke(); } } + +#[cfg(test)] +mod tests { + use super::*; + use rand::Rng; + + #[test] + fn it_works() { + let mut measurement = Measurement::new(&"red".to_string(), 2.0, ToolType::Rectangle); + + for _ in 0..100 { + let x = rand::thread_rng().gen_range(0.0..100.0); + let y = rand::thread_rng().gen_range(0.0..100.0); + measurement.add_point(x, y); + } + assert_eq!(measurement.points.len(), 100); + measurement.finish(); + assert_eq!(measurement.points.len(), 2); + } +} diff --git a/crates/wasm-draw/src/toolbar.rs b/crates/wasm-draw/src/toolbar.rs index 4811073..ef931ba 100644 --- a/crates/wasm-draw/src/toolbar.rs +++ b/crates/wasm-draw/src/toolbar.rs @@ -8,7 +8,7 @@ use web_sys::{ HtmlImageElement, }; -use crate::state::{State, COLORS, PEN_SIZES}; +use crate::state::{State}; const generic_box_styles: &str = "height: 50px; width: 50px; border-bottom: 1px solid #efefef; display: flex; align-items: center; justify-content: center;"; @@ -92,7 +92,7 @@ fn get_pen_size_element( let state_copy = state.clone(); let handle_click = Closure::wrap(Box::new(move || { - state_copy.borrow_mut().update_pen_size(size as f64); + state_copy.borrow_mut().update_pen_size(size); }) as Box); el.add_event_listener_with_callback("click", handle_click.as_ref().unchecked_ref())?; diff --git a/crates/wasm-draw/tests/lib.rs b/crates/wasm-draw/tests/lib.rs index e69de29..8b13789 100644 --- a/crates/wasm-draw/tests/lib.rs +++ b/crates/wasm-draw/tests/lib.rs @@ -0,0 +1 @@ +