Alex Crichton 36b854b69c web-sys: Add support for Global-scope methods
This commit adds further support for the `Global` attribute to not only emit
structural accessors but also emit functions that don't take `&self`. All
methods on a `[Global]` interface will not require `&self` and will call
functions and/or access properties on the global scope.

This should enable things like:

    Window::location() // returns `Location`
    Window::fetch(...) // invokes the `fetch` function

Closes #659
2018-08-28 17:20:31 -07:00

68 lines
1.3 KiB
Rust
Executable File

extern crate js_sys;
extern crate wasm_bindgen;
extern crate web_sys;
use std::f64;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
#[wasm_bindgen]
pub fn draw() {
let document = web_sys::Window::document().unwrap();
let canvas = document.get_element_by_id("canvas").unwrap();
let canvas: web_sys::HtmlCanvasElement = canvas
.dyn_into::<web_sys::HtmlCanvasElement>()
.map_err(|_| ())
.unwrap();
let context = canvas
.get_context("2d")
.unwrap()
.unwrap()
.dyn_into::<web_sys::CanvasRenderingContext2D>()
.unwrap();
context.begin_path();
// Draw the outer circle.
context.arc(
75.0,
75.0,
50.0,
0.0,
f64::consts::PI * 2.0,
).unwrap();
// Draw the mouth.
context.move_to(110.0, 75.0);
context.arc(
75.0,
75.0,
35.0,
0.0,
f64::consts::PI,
).unwrap();
// Draw the left eye.
context.move_to(65.0, 65.0);
context.arc(
60.0,
65.0,
5.0,
0.0,
f64::consts::PI * 2.0,
).unwrap();
// Draw the right eye.
context.move_to(95.0, 65.0);
context.arc(
90.0,
65.0,
5.0,
0.0,
f64::consts::PI * 2.0,
).unwrap();
context.stroke();
}