diff --git a/src/lib.rs b/src/lib.rs index f1b9676..5503e67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,14 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; pub type DrawResult = Result>; +#[wasm_bindgen] +extern "C" { + // Use `js_namespace` here to bind `console.log(..)` instead of just + // `log(..)` + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + /// Result of screen to chart coordinates conversion. #[wasm_bindgen] pub struct Point { @@ -62,6 +70,7 @@ impl ChartManager { pub fn init_panic_hook() { panic::set_hook(Box::new(console_error_panic_hook::hook)); } fn get_back_cache(&self) -> Vec<(f32, f32)> { + log("Using back_cache"); match &self.back_cache { Some(x) => x.clone(), None => panic!("use_back_cache is true, but back_cache is None!"), @@ -69,6 +78,7 @@ impl ChartManager { } fn get_front_cache(&self) -> (Vec<(f32, f32, f32)>, f32) { + log("Using front_cache"); match &self.front_cache { Some(x) => x.clone(), None => panic!("use_front_cache is true, but front_cache is None!"), @@ -82,7 +92,6 @@ impl ChartManager { let func = expr.bind("x").unwrap(); let backend = CanvasBackend::with_canvas_object(element).unwrap(); - let root = backend.into_drawing_area(); let font: FontDesc = ("sans-serif", 20.0).into(); @@ -101,6 +110,7 @@ impl ChartManager { let data: Vec<(f32, f32)> = match self.use_back_cache { true => self.get_back_cache(), false => { + log("Updating back_cache"); let output: Vec<(f32, f32)> = (1..=self.resolution) .map(|x| ((x as f32 / self.resolution as f32) * absrange) + self.min_x) .map(|x| (x, func(x as f64) as f32)) @@ -113,9 +123,10 @@ impl ChartManager { chart.draw_series(LineSeries::new(data, &RED))?; - let (data2, area): (Vec<(f32, f32, f32)>, f32) = match self.use_front_cache { + let (rect_data, area): (Vec<(f32, f32, f32)>, f32) = match self.use_front_cache { true => self.get_front_cache(), false => { + log("Updating front_cache"); let step = absrange / (self.num_interval as f32); let output: (Vec<(f32, f32, f32)>, f32) = self.integral_rectangles(step, &func); self.front_cache = Some(output.clone()); @@ -125,7 +136,7 @@ impl ChartManager { // Draw rectangles chart.draw_series( - data2 + rect_data .iter() .map(|(x1, x2, y)| Rectangle::new([(*x2, *y), (*x1, 0.0)], &BLUE)), )?;