mod3d_gl_wasm_example/
lib.rs

1//a Imports
2use std::rc::Rc;
3
4use wasm_bindgen::prelude::*;
5use web_sys::HtmlCanvasElement;
6
7mod inner;
8use inner::Inner;
9mod model;
10mod objects;
11
12#[wasm_bindgen]
13extern "C" {
14    // Use `js_namespace` here to bind `console.log(..)` instead of just
15    // `log(..)`
16    #[wasm_bindgen(js_namespace = console)]
17    pub fn log(s: &str);
18}
19
20#[macro_export]
21macro_rules! console_log {
22    // Note that this is using the `log` function imported above during
23    // `bare_bones`
24    ($($t:tt)*) => (
25        #[allow(unused_unsafe)]
26        unsafe { $crate::log(&format_args!($($t)*).to_string())}
27    )
28}
29
30//a CanvasWebgl - the external interface
31//tp CanvasWebgl
32/// A paint module that is attached to a Canvas element in an HTML
33/// document, which uses mouse events etc to provide a simple paint
34/// program
35#[wasm_bindgen]
36pub struct CanvasWebgl {
37    inner: Rc<Inner>,
38}
39
40//ip CanvasWebgl
41#[wasm_bindgen]
42impl CanvasWebgl {
43    //fp new
44    /// Create a new CanvasWebgl attached to a Canvas HTML element,
45    /// adding events to the canvas that provide the paint program
46    #[wasm_bindgen(constructor)]
47    pub fn new(canvas: HtmlCanvasElement) -> Result<CanvasWebgl, JsValue> {
48        console_error_panic_hook::set_once();
49        let inner = Inner::new(canvas)?;
50        Ok(Self { inner })
51    }
52
53    //mp shutdown
54    /// Shut down the CanvasWebgl, removing any event callbacks for the canvas
55    pub fn shutdown(&self) -> Result<(), JsValue> {
56        self.inner.shutdown()
57    }
58
59    //mp add_file
60    pub fn add_file(&mut self, filename: &str, data: JsValue) -> Result<(), JsValue> {
61        let data = js_sys::Uint8Array::new(&data);
62        let data = data.to_vec();
63        Ok(std::rc::Rc::get_mut(&mut self.inner)
64            .unwrap()
65            .add_file(filename, data))
66    }
67
68    //mp create_f
69    pub fn create_f(&mut self, shader: &str, glb: &str) -> Result<(), JsValue> {
70        Ok(std::rc::Rc::get_mut(&mut self.inner)
71            .unwrap()
72            .create_f(shader, glb, &["0"])?)
73    }
74
75    //mp fill
76    /// Fill
77    pub fn fill(&mut self) -> Result<(), JsValue> {
78        std::rc::Rc::get_mut(&mut self.inner).unwrap().fill();
79        Ok(())
80    }
81
82    //zz All done
83}