-
Notifications
You must be signed in to change notification settings - Fork 177
/
main.rs
48 lines (38 loc) · 1.22 KB
/
main.rs
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
extern crate stdweb;
use stdweb::traits::*;
use stdweb::unstable::TryInto;
use stdweb::web::{
document,
window,
CanvasRenderingContext2d
};
use stdweb::web::event::{
MouseMoveEvent,
ResizeEvent,
};
use stdweb::web::html_element::CanvasElement;
// Shamelessly stolen from webplatform's TodoMVC example.
macro_rules! enclose {
( ($( $x:ident ),*) $y:expr ) => {
{
$(let $x = $x.clone();)*
$y
}
};
}
fn main() {
stdweb::initialize();
let canvas: CanvasElement = document().query_selector( "#canvas" ).unwrap().unwrap().try_into().unwrap();
let context: CanvasRenderingContext2d = canvas.get_context().unwrap();
canvas.set_width(canvas.offset_width() as u32);
canvas.set_height(canvas.offset_height() as u32);
window().add_event_listener( enclose!( (canvas) move |_: ResizeEvent| {
canvas.set_width(canvas.offset_width() as u32);
canvas.set_height(canvas.offset_height() as u32);
}));
canvas.add_event_listener( enclose!( (context) move |event: MouseMoveEvent| {
context.fill_rect(f64::from(event.client_x() - 5), f64::from(event.client_y() - 5)
, 10.0, 10.0);
}));
stdweb::event_loop();
}