-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas.rs
47 lines (40 loc) · 1.43 KB
/
canvas.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
use futures_util::StreamExt;
use wsdom::{js_types::JsValue, Browser, JsCast};
pub async fn app(browser: Browser) {
// create the UI
let document = wsdom::dom::document(&browser);
let body = document.get_body();
let canvas: wsdom::dom::HTMLCanvasElement = document
.create_element(&"canvas", &wsdom::null())
.unchecked_into();
canvas.set_width(&200);
canvas.set_height(&200);
body.append_child(&canvas);
let btn = document.create_element(&"button", &wsdom::null());
body.append_child(&btn);
// set up the click listener
let mut click_stream = {
let (callback, func) = wsdom::callback::new_callback::<JsValue>(&browser);
btn.add_event_listener(&"click", &func, &wsdom::null());
callback
};
let ctx: wsdom::dom::CanvasRenderingContext2D = canvas
.get_context(&"2d", &wsdom::null())
.unwrap()
.unchecked_into();
// let rect_drawer: &wsdom::dom::CanvasRect = ctx.as_ref();
// let line_drawer: &wsdom::dom::CanvasPath = ctx.as_ref();
ctx.clear_rect(&0, &0, &200, &200);
loop {
btn.set_inner_text(&"draw");
click_stream.next().await;
ctx.begin_path();
ctx.move_to(&75, &50);
ctx.line_to(&100, &75);
ctx.line_to(&100, &25);
ctx.fill(&wsdom::undefined());
btn.set_inner_text(&"clear");
click_stream.next().await;
ctx.clear_rect(&0, &0, &200, &200);
}
}