|
1 | | -use stdweb::js; |
2 | | -use stdweb::traits::*; |
3 | | -use stdweb::web::event::KeyDownEvent; |
| 1 | +use wasm_bindgen::JsCast; |
| 2 | +use web_sys::window; |
4 | 3 | use wasm_bindgen::prelude::*; |
5 | 4 |
|
6 | 5 | /** |
@@ -56,42 +55,51 @@ pub fn main() { |
56 | 55 | let mut f = 1.0; |
57 | 56 | let mut af = 1.0; |
58 | 57 |
|
59 | | - // We are using webstd here to make things easy. |
60 | | - // Listen to arrow key to move and reposition all div |
61 | | - stdweb::web::document().add_event_listener(move |e: KeyDownEvent| { |
62 | | - match e.key().as_str() { |
63 | | - "ArrowUp" => y = y.saturating_sub(10), |
64 | | - "ArrowDown" => y += 10, |
65 | | - "ArrowLeft" => x = x.saturating_sub(10), |
66 | | - "ArrowRight" => x += 10, |
67 | | - "+" => f *= 1.5, |
68 | | - "-" => f /= 1.5, |
| 58 | + // Listen to keydown events to move and reposition all divs |
| 59 | + let closure = Closure::wrap(Box::new(move |event: web_sys::Event| { |
| 60 | + let keyboard_event = event.dyn_ref::<web_sys::KeyboardEvent>(); |
| 61 | + if let Some(e) = keyboard_event { |
| 62 | + let key = e.key(); |
| 63 | + match key.as_str() { |
| 64 | + "ArrowUp" => y = y.saturating_sub(10), |
| 65 | + "ArrowDown" => y += 10, |
| 66 | + "ArrowLeft" => x = x.saturating_sub(10), |
| 67 | + "ArrowRight" => x += 10, |
| 68 | + "+" => f *= 1.5, |
| 69 | + "-" => f /= 1.5, |
69 | 70 |
|
70 | | - "w" => ay = ay.saturating_sub(10), |
71 | | - "a" => ax = ax.saturating_sub(10), |
72 | | - "s" => ay += 10, |
73 | | - "d" => ax += 10, |
74 | | - "1" => af *= 1.5, |
75 | | - "2" => af /= 1.5, |
| 71 | + "w" => ay = ay.saturating_sub(10), |
| 72 | + "a" => ax = ax.saturating_sub(10), |
| 73 | + "s" => ay += 10, |
| 74 | + "d" => ax += 10, |
| 75 | + "1" => af *= 1.5, |
| 76 | + "2" => af /= 1.5, |
76 | 77 |
|
77 | | - key => { |
78 | | - js! { @(no_return) console.log("pressed " + @{key}); }; |
79 | | - return; |
| 78 | + _ => { |
| 79 | + web_sys::console::log_1(&format!("pressed {}", key).into()); |
| 80 | + return; |
| 81 | + } |
80 | 82 | } |
| 83 | + div::reposition(x, y).unwrap(); |
| 84 | + let w = f * w as f32; |
| 85 | + let h = f * h as f32; |
| 86 | + div::resize(w as u32, h as u32).unwrap(); |
| 87 | + |
| 88 | + let aw = af * aw as f32; |
| 89 | + let ah = af * ah as f32; |
| 90 | + pane_a |
| 91 | + .reposition_and_resize(ax, ay, aw as u32, ah as u32) |
| 92 | + .unwrap(); |
| 93 | + // Same as |
| 94 | + // pane_a.reposition(ax,ay).unwrap(); |
| 95 | + // pane_a.resize(aw as u32, ah as u32).unwrap(); |
| 96 | + // but avoids extra redraw of div |
81 | 97 | } |
82 | | - div::reposition(x, y).unwrap(); |
83 | | - let w = f * w as f32; |
84 | | - let h = f * h as f32; |
85 | | - div::resize(w as u32, h as u32).unwrap(); |
| 98 | + }) as Box<dyn FnMut(_)>); |
86 | 99 |
|
87 | | - let aw = af * aw as f32; |
88 | | - let ah = af * ah as f32; |
89 | | - pane_a |
90 | | - .reposition_and_resize(ax, ay, aw as u32, ah as u32) |
91 | | - .unwrap(); |
92 | | - // Same as |
93 | | - // pane_a.reposition(ax,ay).unwrap(); |
94 | | - // pane_a.resize(aw as u32, ah as u32).unwrap(); |
95 | | - // but avoids extra redraw of div |
96 | | - }); |
| 100 | + window() |
| 101 | + .unwrap() |
| 102 | + .add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref()) |
| 103 | + .unwrap(); |
| 104 | + closure.forget(); |
97 | 105 | } |
0 commit comments