symcode_webapp/
debugger.rs1use wasm_bindgen::{Clamped, JsValue};
2use web_sys::ImageData;
3use visioncortex::{BoundingRect, Color, BinaryImage, ColorImage};
4use symcode::interfaces::Debugger as DebuggerInterface;
5use crate::canvas::Canvas;
6
7pub struct Debugger {
8 pub(crate) debug_canvas: Canvas,
9}
10
11impl DebuggerInterface for Debugger {
12 fn render_color_image_to_canvas(&self, image: &ColorImage) -> Result<(), &'static str> {
13 render_color_image_to_canvas(&self.debug_canvas, image)
14 }
15
16 fn render_bounding_rect_to_canvas_with_color(&self, rect: &BoundingRect, color: Color) {
17 let ctx = self.debug_canvas.get_rendering_context_2d();
18 ctx.set_stroke_style(JsValue::from_str(
19 &color.to_color_string()
20 ).as_ref());
22 let x1 = rect.left as f64;
23 let y1 = rect.top as f64;
24 let x2 = rect.right as f64;
25 let y2 = rect.bottom as f64;
26 ctx.begin_path();
27 ctx.move_to(x1, y1);
28 ctx.line_to(x1, y2);
29 ctx.line_to(x2, y2);
30 ctx.line_to(x2, y1);
31 ctx.line_to(x1, y1);
32 ctx.stroke();
33 }
34
35 fn log(&self, msg: &str) {
36 crate::util::console_log_util(msg)
37 }
38}
39
40pub fn render_color_image_to_canvas(canvas: &Canvas, image: &ColorImage) -> Result<(), &'static str> {
41 let data = image.pixels.clone();
42 let data = ImageData::new_with_u8_clamped_array_and_sh(Clamped(&data), image.width as u32, image.height as u32).unwrap();
43 let ctx = canvas.get_rendering_context_2d();
44 canvas.set_width(image.width);
45 canvas.set_height(image.height);
46 ctx.clear_rect(0.0, 0.0, canvas.width() as f64, canvas.height() as f64);
47 if ctx.put_image_data(&data, 0.0, 0.0).is_err() {
48 Err("failed to put_image_data")
49 } else {
50 Ok(())
51 }
52}
53
54pub fn render_binary_image_to_canvas(canvas: &Canvas, image: &BinaryImage) -> Result<(), &'static str> {
55 let image = &image.to_color_image();
56 render_color_image_to_canvas(canvas, image)
57}