fluffl 0.0.5

A cross-platform multimedia layer that exposes opengl,sockets,and audio utilities for desktop and browser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
use super::{event_util::*, FlufflError, HasFlufflWindow, TouchTracker};
use crate::{
    audio::{init_audio_threads, FlufflAudioContext},
    console::*,
    window::FlufflRunning,
    *,
};

pub use js_sys;
pub use wasm_bindgen;
pub use wasm_bindgen::{prelude::*, JsCast};
pub use web_sys::*;

use std::{cell::RefCell, rc::Rc, sync::Arc};

use glow;
use glow::*;
use wasm_bindgen_futures::*;
use web_sys;

use super::{FlufflWindowConfigs, FlufflWindowPtr};

///Global for touch tracker
static mut GLOBAL_TOUCH_TRACKER: Option<TouchTracker<i32>> = None;

impl TouchTracker<i32> {
    /// # Description
    /// Initalizes tracker. Tracker routines will panic if this function isn't called.
    pub fn init() {
        unsafe {
            GLOBAL_TOUCH_TRACKER = Some(TouchTracker::new());
        }
    }
    pub fn get_mut() -> &'static mut Self {
        unsafe {
            GLOBAL_TOUCH_TRACKER
                .as_mut()
                .expect("tracker not initalized")
        }
    }
}

// Global variables that are only visible inside of this module.
// The use of global variables should be fine if there is no multithreading going on.
static mut GLOBAL_EVENT_QUEUE: Option<FlufflEvent> = None;
static mut GLOBAL_CANVAS_REF: Option<Rc<HtmlCanvasElement>> = None;
static mut IS_MOBILE: bool = false;

/// determines if desktop is mobile or not
fn determine_desktop_or_mobile() {
    let navigator = web_sys::window().unwrap().navigator();

    let mobile_platforms = [
        "Android",
        "webOS",
        "iPad",
        "iPhone",
        "iPod",
        "BlackBerry",
        "Windows Phone",
        "IEMobile",
    ];

    match navigator.user_agent() {
        Ok(agent_string) => {
            for platform in mobile_platforms.iter() {
                let has_match = agent_string
                    .to_lowercase()
                    .matches(platform.to_lowercase().as_str())
                    .next()
                    .is_some();

                if has_match {
                    unsafe {
                        IS_MOBILE = true;
                        break;
                    }
                }
            }
        }
        Err(_) => panic!("Failed to determine browser platform!"),
    }
}

fn is_mobile() -> bool {
    unsafe { IS_MOBILE }
}

fn init_global_event_queue() {
    unsafe { GLOBAL_EVENT_QUEUE = Some(FlufflEvent::new()) };
}

fn init_global_canvas(canvas_ptr: Rc<HtmlCanvasElement>) {
    unsafe {
        GLOBAL_CANVAS_REF = Some(canvas_ptr);
    }
}

fn get_canvas() -> Rc<HtmlCanvasElement> {
    unsafe {
        GLOBAL_CANVAS_REF
            .as_ref()
            .expect("global canvas pointer not initalized!")
            .clone()
    }
}

#[allow(dead_code)]
pub struct FlufflWindow {
    glue_event: Option<FlufflEvent>,
    gl: GlowGL,
    render_loop: Option<glow::RenderLoop>,
    window_width: u32,
    window_height: u32,
    audio_ctx: FlufflAudioContext,
    canvas: Rc<HtmlCanvasElement>,
}

impl FlufflWindow {
    //moves renderloop out of glue_window and calls it
    fn get_render_loop(&mut self) -> Option<impl glow::HasRenderLoop> {
        self.render_loop.take()
    }

    /// Loops infinitely unless caller mutates `running` to false. Just executes the user defined taske over and over while also \
    /// doing basic matenence for events and the window
    /// # Arguments
    /// * `closure` is the user defined task. it exposes platform specific internals with `&mut Self`. \
    /// ```
    pub fn run<Loop, LoopRet, State>(self, app_state: State, core_loop: Loop)
    where
        State: 'static,
        Loop: Fn(FlufflWindowPtr, FlufflRunning, FlufflState<State>) -> LoopRet + Copy + 'static,
        LoopRet: std::future::Future<Output = ()> + 'static,
    {
        let mut fluffl_window = self;
        let render_loop = fluffl_window.get_render_loop().unwrap();

        let window_ptr = FlufflWindowPtr {
            ptr: Arc::new(RefCell::new(fluffl_window)),
        };

        let app_state_ptr = FlufflState::new(app_state);
        render_loop.run(move |running| {
            if window_ptr.window_mut_cb(transfer_events) {
                let win_ptr_clone = window_ptr.clone();
                let app_state_clone = app_state_ptr.clone();
                spawn_local(core_loop(
                    win_ptr_clone,
                    FlufflRunning::new(running),
                    app_state_clone,
                ));
            }
        });
    }
}

impl HasFlufflWindow for FlufflWindow {
    #[allow(unused_variables)]
    /// spins up a window
    /// `config_xml` - an xml text containing window config data
    fn init(config_xml: &str) -> Result<Self, FlufflError> {
        determine_desktop_or_mobile();

        console_log!("is_mobile = {}\n", is_mobile());

        //get config settings from javascript (if possible)

        #[allow(unused_unsafe)]
        // let config_js = unsafe { get_xml_config() };

        // let xml_text = if config_js.is_empty() == false {
        //     config_js.as_str()
        // } else {
        //     config_xml
        // };
        let xml_text = config_xml;

        let settings = FlufflWindowConfigs::new().parser_config_file(config_xml);

        let web_window = web_sys::window().unwrap();
        let canvas = web_window
            .document()
            .unwrap()
            .get_element_by_id(settings.canvas_id.as_str())
            .unwrap()
            .dyn_into::<HtmlCanvasElement>()
            .unwrap();

        let webgl2_context = canvas
            .get_context(settings.webgl_version.as_str())
            .unwrap()
            .unwrap()
            .dyn_into::<WebGl2RenderingContext>()
            .unwrap();

        let gl = glow::Context::from_webgl2_context(webgl2_context);
        let render_loop = glow::RenderLoop::from_request_animation_frame();

        if attach_event_handlers(&web_window, &canvas).is_err() {
            // console_write("Event handler instantiation failed!");
            return Err(FlufflError::WindowInitError(String::from(
                "javascript event listeners failed",
            )));
        }

        let canvas = Rc::new(canvas);

        //I need a global reference to the canvas in this module
        init_global_canvas(canvas.clone());

        let window = Self {
            window_width: settings.width,
            window_height: settings.height,
            glue_event: Some(FlufflEvent::new()),
            render_loop: Some(render_loop),
            gl: Arc::new(Box::new(gl)),
            audio_ctx: FlufflAudioContext::new(),
            canvas,
        };

        //I use this table to track touch displacements
        TouchTracker::init();

        //web implementation keeps a list of 'audio threads' in a static list
        init_audio_threads();

        //web implementation uses a temporary event queue from which the Fluffl window will read from
        init_global_event_queue();

        Ok(window)
    }

    fn get_events(&mut self) -> &mut FlufflEvent {
        self.glue_event.as_mut().unwrap()
    }
    fn width(&self) -> u32 {
        self.canvas.width()
    }
    fn height(&self) -> u32 {
        self.canvas.height()
    }

    fn gl(&self) -> Arc<Box<Context>> {
        self.gl.clone()
    }

    fn audio_context(&self) -> FlufflAudioContext {
        self.audio_ctx.clone()
    }

    fn set_fullscreen(&mut self, go_fullscren: bool) {
        let document: Document = web_sys::window().unwrap().document().unwrap();

        if go_fullscren && !document.fullscreen() {
            let canvas_ref = self.canvas.as_ref();
            let canvas_element: &HtmlElement = canvas_ref.dyn_ref::<HtmlElement>().unwrap();
            canvas_element
                .request_fullscreen()
                .expect("Fullscreen Failed");
        } else if !go_fullscren && document.fullscreen() {
            document.exit_fullscreen();
        }
    }
}

fn attach_event_handlers(window: &Window, canvas: &HtmlCanvasElement) -> Result<(), JsValue> {
    canvas.style().set_property("border", "solid")?;
    /*canvas resize handler*/
    {
        let closure = Closure::wrap(Box::new(move |_event: web_sys::UiEvent| {
            let event_queue = get_global_event_queue_mut();

            let (new_width, new_height) = {
                let canvas = get_canvas();
                (canvas.width() as i32, canvas.height() as i32)
            };

            event_queue.push_event(EventKind::Resize {
                width: new_width,
                height: new_height,
            });
        }) as Box<dyn FnMut(_)>);

        canvas.add_event_listener_with_callback("resize", closure.as_ref().unchecked_ref())?;
        window.add_event_listener_with_callback("resize", closure.as_ref().unchecked_ref())?;
        closure.forget();
    }

    /*touch move handler*/
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
            let touch_list: web_sys::TouchList = event.changed_touches();

            let event_queue = get_global_event_queue_mut();

            for k in 0..touch_list.length() {
                if let Some(touch) = touch_list.item(k) {
                    let id = touch.identifier();

                    let x = touch.client_x() as f64;
                    let y = touch.client_y() as f64;
                    let (x, y, _, _) = convert_from_viewport_to_window(x, y, 0., 0.);
                    let (x, y) = (x as f32, y as f32);

                    let [dx, dy] = TouchTracker::get_mut().get_touch_displacement(id, [x, y]);
                    event_queue.push_event(EventKind::TouchDown {
                        finger_id: id,
                        x,
                        y,
                        dx,
                        dy,
                    });
                }
            }
        }) as Box<dyn FnMut(_)>);

        canvas.add_event_listener_with_callback("touchmove", closure.as_ref().unchecked_ref())?;

        closure.forget();
    }

    /*touch end handler*/
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
            let touch_list: web_sys::TouchList = event.touches();

            let event_queue = get_global_event_queue_mut();

            for k in 0..touch_list.length() {
                if let Some(touch) = touch_list.item(k) {
                    let id = touch.identifier();
                    let x = touch.client_x() as f64;
                    let y = touch.client_y() as f64;

                    let (x, y, _, _) = convert_from_viewport_to_window(x, y, 0., 0.);
                    let (x, y) = (x as f32, y as f32);

                    TouchTracker::get_mut().get_touch_displacement(id, [x, y]);

                    event_queue.push_event(EventKind::TouchUp {
                        finger_id: id,
                        x,
                        y,
                        dx: 0.,
                        dy: 0.,
                    });

                    // Its important to remove info associated with id when finger is released.
                    // We only want to track unique fingers detected by the touchscreen
                    TouchTracker::get_mut().remove(&id);
                }
            }
        }) as Box<dyn FnMut(_)>);

        canvas.add_event_listener_with_callback("touchend", closure.as_ref().unchecked_ref())?;

        closure.forget();
    }

    /*touch start handler*/
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
            let touch_list: web_sys::TouchList = event.touches();

            let event_queue = get_global_event_queue_mut();

            for k in 0..touch_list.length() {
                if let Some(touch) = touch_list.item(k) {
                    let id = touch.identifier();
                    let x = touch.client_x() as f64;
                    let y = touch.client_y() as f64;

                    let (x, y, _, _) = convert_from_viewport_to_window(x, y, 0., 0.);
                    let (x, y) = (x as f32, y as f32);

                    TouchTracker::get_mut().get_touch_displacement(id, [x, y]);

                    event_queue.push_event(EventKind::TouchDown {
                        finger_id: id,
                        x,
                        y,
                        dx: 0.0,
                        dy: 0.0,
                    });
                }
            }
        }) as Box<dyn FnMut(_)>);

        canvas.add_event_listener_with_callback("touchstart", closure.as_ref().unchecked_ref())?;

        closure.forget();
    }

    /*mouse move handler*/
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
            let (x, y, dx, dy) = (
                event.client_x() as f64,
                event.client_y() as f64,
                event.movement_x() as f64,
                event.movement_y() as f64,
            );
            let (x, y, dx, dy) = convert_from_viewport_to_window(x, y, dx, dy);

            let event_queue = get_global_event_queue_mut();
            event_queue.push_event(EventKind::MouseMove { x, y, dx, dy });
        }) as Box<dyn FnMut(_)>);

        canvas.add_event_listener_with_callback("mousemove", closure.as_ref().unchecked_ref())?;
        closure.forget();
    }

    /*mouse down handler*/
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
            let (x, y) = (event.client_x() as f64, event.client_y() as f64);

            let (x, y, _, _) = convert_from_viewport_to_window(x, y, 0., 0.);

            let button = event.button();

            let event_queue = get_global_event_queue_mut();
            event_queue.push_event(EventKind::MouseDown {
                button_code: get_button_code(button),
                x,
                y,
            });
        }) as Box<dyn FnMut(_)>);
        canvas.add_event_listener_with_callback("mousedown", closure.as_ref().unchecked_ref())?;
        closure.forget();
    }

    /*mouse up handler*/
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
            let (x, y) = (event.client_x() as f64, event.offset_y() as f64);
            let button = event.button();

            let (x, y, _, _) = convert_from_viewport_to_window(x, y, 0., 0.);

            get_global_event_queue_mut().push_event(EventKind::MouseUp {
                button_code: get_button_code(button),
                x,
                y,
            });
        }) as Box<dyn FnMut(_)>);
        canvas.add_event_listener_with_callback("mouseup", closure.as_ref().unchecked_ref())?;
        closure.forget();
    }

    /*context menu handler*/
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::Event| {
            //this is dont to prevent context menu from showing up
            event.prevent_default();
        }) as Box<dyn FnMut(_)>);
        canvas.add_event_listener_with_callback("contextmenu", closure.as_ref().unchecked_ref())?;
        closure.forget();
    }

    //wheel event listener
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::WheelEvent| {
            let delta_y = (-event.delta_y().signum()) as i32;
            get_global_event_queue_mut().push_event(EventKind::MouseWheel {
                button_code: MouseCode::WHEEL {
                    direction: delta_y as i32,
                },
            });
        }) as Box<dyn FnMut(_)>);
        canvas.add_event_listener_with_callback("wheel", closure.as_ref().unchecked_ref())?;
        closure.forget();
    }

    //add keydown to window
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
            // console_log!("[down] key() = {} code = {}", event.key() , event.code() );
            let eq = get_global_event_queue_mut();
            let code = map_keycode(event.code().as_str());
            eq.push_event(EventKind::KeyDown { code })
        }) as Box<dyn FnMut(_)>);
        window.add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref())?;
        closure.forget();
    }

    //add keyup to window
    {
        let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
            // console_log!("[up] key() = {} code = {}", event.key() , event.code() );
            let eq = get_global_event_queue_mut();
            let code = map_keycode(event.code().as_str());
            eq.push_event(EventKind::KeyUp { code });
        }) as Box<dyn FnMut(_)>);
        window.add_event_listener_with_callback("keyup", closure.as_ref().unchecked_ref())?;
        closure.forget();
    }

    Ok(())
}

//event listeners fill GLOBAL and this function just transfers events
//from global to window
fn transfer_events(window: &mut FlufflWindow) {
    unsafe { GLOBAL_EVENT_QUEUE.as_mut() }
        .unwrap()
        .flush_iter_mut()
        .for_each(|e| {
            window.get_events().push_event(e);
        });
}

fn get_button_code(button: i16) -> MouseCode {
    //maps javascript button codes to 'Glue' Codes
    match button {
        0 => MouseCode::LEFT_BUTTON,
        1 => MouseCode::WHEEL { direction: 0 },
        2 => MouseCode::RIGHT_BUTTON,
        _ => MouseCode::WHEEL { direction: 0 },
    }
}

fn get_global_event_queue_mut<'a>() -> &'a mut FlufflEvent {
    unsafe { GLOBAL_EVENT_QUEUE.as_mut().unwrap() }
}

// #[wasm_bindgen(module = "/js/aux_util.js")]
// extern "C" {
//     fn get_xml_config() -> String;
//     /// This kind of acts as "command line arguments" for the wasm module.
//     /// The idea is that you call `set_xml_config` right before you *init* the wasm module and
//     /// whatever config options get passed through this function will take absolute precedence.
//     pub fn set_xml_config(xml_text: String);
// }

/// # Description
/// converts javascript viewport coordinates to expected window coordinates
/// # Retuns
/// A 4-tuple of the format: `( x,y,dx,dy )`
/// # Comments
/// I hate that I had to write this routine. Either the Javascript coordinate system sucks
/// Or the documentation is lacking something.
fn convert_from_viewport_to_window(x: f64, y: f64, dx: f64, dy: f64) -> (f32, f32, f32, f32) {
    if is_mobile() {
        viewport_to_window_mobile_browser(x, y, dx, dy)
    } else {
        viewport_to_window_desktop_browser(x, y, dx, dy)
    }
}

fn viewport_to_window_mobile_browser(x: f64, y: f64, dx: f64, dy: f64) -> (f32, f32, f32, f32) {
    if is_in_portrait_mode() {
        //desktop way works in portrait mode both fullscreen and
        viewport_to_window_desktop_browser(x, y, dx, dy)
    } else {
        let window = web_sys::window().unwrap();
        let document = window.document().unwrap();
        let _screen: Screen = window.screen().ok().unwrap();
        let canvas = get_canvas();
        let rect = canvas.get_bounding_client_rect();

        let canvas_width = canvas.width() as f64;
        let canvas_height = canvas.height() as f64;

        if document.fullscreen() {
            let aspect_ratio = canvas_width / canvas_height;
            let vp_height = rect.height();
            let vp_width = aspect_ratio * vp_height;
            let vp_x = rect.width() / 2.0 - vp_width / 2.0;
            let vp_y = 0.0;

            let x = (x - vp_x) * canvas_width / vp_width;
            let y = (y - vp_y) * (canvas_height) / vp_height;
            let dx = (dx - vp_x) * canvas_width / vp_width;
            let dy = (dy - vp_y) * (canvas_height) / vp_height;

            (x as f32, y as f32, dx as f32, dy as f32)
        } else {
            let sx = canvas_width / rect.width() as f64;
            let sy = canvas_height / rect.height() as f64;
            let x = (x * sx - rect.x() * sx) as f32;
            let y = (y * sy - rect.y() * sy) as f32;
            let dx = (dx * sx - rect.x() * sx) as f32;
            let dy = (dy * sx - rect.y() * sx) as f32;

            // console_log!(
            //     "pos:[{},{}],vp_dims: [{},{}], rect dims: [x:{},y:{},w:{},h:{}], screen:[aw:{},ah:{},w:{},h:{}]\n",
            //     x,
            //     y,
            //     -1,
            //     -1,
            //     rect.x(),
            //     rect.y(),
            //     rect.width(),
            //     rect.height(),
            //     screen.avail_width().ok().unwrap(),
            //     screen.avail_height().ok().unwrap(),
            //     screen.width().ok().unwrap(),
            //     screen.height().ok().unwrap(),
            //     );

            (x, y, dx, dy)
        }
    }
}

fn viewport_to_window_desktop_browser(x: f64, y: f64, dx: f64, dy: f64) -> (f32, f32, f32, f32) {
    let canvas = get_canvas();
    let rect = canvas.get_bounding_client_rect();
    let document: Document = web_sys::window().unwrap().document().unwrap();

    let canvas_width = canvas.width() as f64;
    let canvas_height = canvas.height() as f64;

    if !document.fullscreen() {
        let sx = canvas_width / rect.width() as f64;
        let sy = canvas_height / rect.height() as f64;
        let x = (x * sx - rect.x() * sx) as f32;
        let y = (y * sy - rect.y() * sy) as f32;
        let dx = (dx * sx - rect.x() * sx) as f32;
        let dy = (dy * sx - rect.y() * sx) as f32;
        (x, y, dx, dy)
    } else {
        // Explanation for the Else check:
        // The else case has to be checked because I noticed that,when in fullscreen,
        // the get_bounding_client_rect() AABB doesn't actually cover the desired region in fullscreen.
        // So I had to compute the AABB manually knowing that firefox preserves aspect ratio and that AABB in fullscreen is just the
        // clients screen dimensions
        // console_log!("bounding_client:[{},{}]\n", rect.width(), rect.height());

        let aspect_ratio = canvas_width / canvas_height;

        let (vp_width, vp_height, vp_x, vp_y) = if rect.width() >= canvas_width {
            let vp_width = rect.height() * aspect_ratio;
            let vp_height = rect.height();
            let vp_x = rect.width() / 2. - vp_width / 2.0;
            let vp_y = 0.0;
            (vp_width, vp_height, vp_x, vp_y)
        } else {
            //this mapping is broken for modile (fullscreen+landscape)
            let vp_height = rect.width() / aspect_ratio;
            let vp_width = rect.width();
            let vp_x = 0.0;
            let vp_y = rect.height() / 2. - vp_height / 2.0;
            (vp_width, vp_height, vp_x, vp_y)
        };

        // compute transformation matrix
        // [a,b]
        // [c,d]
        // let sx = canvas_width/vp_width
        // let sy = canvas_height/vp_height
        // then this:
        // let x = ((x - vp_x) * (canvas.width() as f64 /vp_width) )  as i32;
        // let y = ((y - vp_y) *(canvas.height() as f64/ vp_height))  as i32;
        // becomes this:
        // let x = x*sx -vp_x*sx
        // let y = y*sy -vp_y*sy
        // becomes(in matrix form):
        // [x'] = [sx][-vp_x*sx]  [x]
        // [y'] = [sy][-yp_y*sy]  [y]

        let sx = canvas.width() as f64 / vp_width;
        let sy = canvas.height() as f64 / vp_height;

        let x = (sx * x - vp_x * sx) as f32;
        let y = (sy * y - vp_y * sy) as f32;
        let dx = (dx * sx - vp_x * sx) as f32;
        let dy = (dy * sy - vp_y * sy) as f32;

        (x, y, dx, dy)
    }
}

/// # Description
/// checks if the browser is in portrait mode
fn is_in_portrait_mode() -> bool {
    let window = web_sys::window().unwrap();
    if let Ok(Some(query_list)) = window.match_media("(orientation: portrait)") {
        query_list.matches()
    } else {
        false
    }
}
/// MDN docs:
/// https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values
fn map_keycode(js_keycode: &str) -> KeyCode {
    match js_keycode {
        "KeyA" => KeyCode::KEY_A,
        "KeyB" => KeyCode::KEY_B,
        "KeyC" => KeyCode::KEY_C,
        "KeyD" => KeyCode::KEY_D,
        "KeyE" => KeyCode::KEY_E,
        "KeyF" => KeyCode::KEY_F,
        "KeyG" => KeyCode::KEY_G,
        "KeyH" => KeyCode::KEY_H,
        "KeyI" => KeyCode::KEY_I,
        "KeyJ" => KeyCode::KEY_J,
        "KeyK" => KeyCode::KEY_K,
        "KeyL" => KeyCode::KEY_L,
        "KeyM" => KeyCode::KEY_M,
        "KeyN" => KeyCode::KEY_N,
        "KeyO" => KeyCode::KEY_O,
        "KeyP" => KeyCode::KEY_P,
        "KeyQ" => KeyCode::KEY_Q,
        "KeyR" => KeyCode::KEY_R,
        "KeyS" => KeyCode::KEY_S,
        "KeyT" => KeyCode::KEY_T,
        "KeyU" => KeyCode::KEY_U,
        "KeyV" => KeyCode::KEY_V,
        "KeyW" => KeyCode::KEY_W,
        "KeyX" => KeyCode::KEY_X,
        "KeyY" => KeyCode::KEY_Y,
        "KeyZ" => KeyCode::KEY_Z,
        "Backquote" => KeyCode::BACK_QUOTE,
        "Digit0" => KeyCode::NUM_0,
        "Digit1" => KeyCode::NUM_1,
        "Digit2" => KeyCode::NUM_2,
        "Digit3" => KeyCode::NUM_3,
        "Digit4" => KeyCode::NUM_4,
        "Digit5" => KeyCode::NUM_5,
        "Digit6" => KeyCode::NUM_6,
        "Digit7" => KeyCode::NUM_7,
        "Digit8" => KeyCode::NUM_8,
        "Digit9" => KeyCode::NUM_9,
        "Numpad0" => KeyCode::KP_0,
        "Numpad1" => KeyCode::KP_1,
        "Numpad2" => KeyCode::KP_2,
        "Numpad3" => KeyCode::KP_3,
        "Numpad4" => KeyCode::KP_4,
        "Numpad5" => KeyCode::KP_5,
        "Numpad6" => KeyCode::KP_6,
        "Numpad7" => KeyCode::KP_7,
        "Numpad8" => KeyCode::KP_8,
        "Numpad9" => KeyCode::KP_9,
        "Minus" => KeyCode::MINUS,
        "Equal" => KeyCode::EQUALS,
        "Comma" => KeyCode::COMMA,
        "Semicolon" => KeyCode::COLON,
        "Quote" => KeyCode::QUOTE,
        "Slash" => KeyCode::FORDSLASH,
        "Backslash" => KeyCode::BACKSLASH,
        "Insert" => KeyCode::INSERT,
        "Home" => KeyCode::HOME,
        "PageUp" => KeyCode::PAGE_U,
        "PageDown" => KeyCode::PAGE_D,
        "End" => KeyCode::END,
        "Delete" => KeyCode::DELETE,
        "ShiftLeft" => KeyCode::SHIFT_L,
        "ShiftRight" => KeyCode::SHIFT_R,
        "ArrowUp" => KeyCode::ARROW_U,
        "ArrowLeft" => KeyCode::ARROW_L,
        "ArrowRight" => KeyCode::ARROW_R,
        "ArrowDown" => KeyCode::ARROW_D,
        "Space" => KeyCode::SPACE,
        "Period" => KeyCode::PERIOD,
        "AltLeft" => KeyCode::ALT_L,
        "AltRight" => KeyCode::ALT_R,
        "ControlLeft" => KeyCode::CTRL_L,
        "ControlRight" => KeyCode::CTRL_R,
        "BracketLeft" => KeyCode::BRACKET_L,
        "BracketRight" => KeyCode::BRACKET_R,
        _ => KeyCode::UNKNOWN,
    }
}