Skip to content

Commit

Permalink
Merge #105
Browse files Browse the repository at this point in the history
105: Simplify the example a bit r=grovesNL a=17cupsofcoffee

Here's a few tweaks that make the example a bit simpler/up-to-date:

* A seperate `wasm_main` is no longer required due to rustwasm/wasm-bindgen#1843.
* The prelude import wasn't actually being used anywhere.
* Nightly is no longer required to build this project, so I removed that from the instructions.
* `--no-modules` has been replaced with `--target web`, which doesn't rely on global variables and allows support for [JS snippets](https://rustwasm.github.io/docs/wasm-bindgen/reference/js-snippets.html).
    * The only caveat to this is that the generates JS is a ES module instead of a basic JS file - but any browser that supports WASM will also support ES modules, so I don't think this is an issue in practice.

Co-authored-by: Joe Clay <27cupsofcoffee@gmail.com>
  • Loading branch information
bors[bot] and 17cupsofcoffee committed May 14, 2020
2 parents 4ed4327 + 2620d64 commit 2c8ad75
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/hello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ cargo run --features=window-sdl2
To run with web-sys:

```shell
cargo +nightly build --target wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown
mkdir -p generated
wasm-bindgen ../../target/wasm32-unknown-unknown/debug/hello.wasm --out-dir generated --no-modules
wasm-bindgen ../../target/wasm32-unknown-unknown/debug/hello.wasm --out-dir generated --target web
cp index.html generated
```

Expand Down
8 changes: 4 additions & 4 deletions examples/hello/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<body>
<canvas id="canvas" width="640" height="480"></canvas>
<script src="./hello.js"></script>
<script>
window.addEventListener("load", async () => {
await wasm_bindgen("./hello_bg.wasm");
});
<script type="module">
import init from "./hello.js";

init();
</script>
</body>
</html>
9 changes: 1 addition & 8 deletions examples/hello/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
use glow::*;

#[cfg(all(target_arch = "wasm32", feature = "web-sys"))]
use wasm_bindgen::prelude::*;

#[cfg(all(target_arch = "wasm32", feature = "stdweb"))]
use std_web::{
traits::*,
unstable::TryInto,
web::{document, html_element::*},
};

#[cfg(all(target_arch = "wasm32", feature = "stdweb"))]
use webgl_stdweb::WebGL2RenderingContext;

#[cfg_attr(all(target_arch = "wasm32", feature = "web-sys"), wasm_bindgen(start))]
pub fn wasm_main() {
main();
}

fn main() {
unsafe {
// Create a context from a WebGL2 context on wasm32 targets
Expand Down

0 comments on commit 2c8ad75

Please sign in to comment.