forked from seed-rs/seed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
86 lines (72 loc) · 1.73 KB
/
lib.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
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
use seed::{prelude::*, *};
use wasm_bindgen_futures::JsFuture;
use web_sys::{HtmlMediaElement, MediaStream, MediaStreamConstraints};
// ------ ------
// Init
// ------ ------
fn init(_: Url, orders: &mut impl Orders<Msg>) -> Model {
orders.perform_cmd(user_media());
Model::default()
}
async fn user_media() -> Msg {
let mut constraints = MediaStreamConstraints::new();
constraints.video(&JsValue::from(true));
let media_stream_promise = window()
.navigator()
.media_devices()
.unwrap()
.get_user_media_with_constraints(&constraints)
.unwrap();
Msg::UserMedia(
JsFuture::from(media_stream_promise)
.await
.map(MediaStream::from),
)
}
// ------ ------
// Model
// ------ ------
#[derive(Default)]
struct Model {
video: ElRef<HtmlMediaElement>,
}
// ------ ------
// Update
// ------ ------
enum Msg {
UserMedia(Result<MediaStream, JsValue>),
}
fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) {
match msg {
Msg::UserMedia(Ok(media_stream)) => {
model
.video
.get()
.expect("get video element")
.set_src_object(Some(&media_stream));
}
Msg::UserMedia(Err(error)) => {
log!(error);
}
}
}
// ------ ------
// View
// ------ ------
fn view(model: &Model) -> impl IntoNodes<Msg> {
video![
el_ref(&model.video),
attrs! {
At::Width => 320,
At::Height => 240,
At::AutoPlay => AtValue::None,
}
]
}
// ------ ------
// Start
// ------ ------
#[wasm_bindgen(start)]
pub fn start() {
App::start("app", init, update, view);
}