Skip to content

WebAssembly

Hajime Hoshi edited this page Aug 27, 2018 · 28 revisions

The easiest way is to use WasmServe with the latest Go.

This article introduces a regular way to build an Ebiten app as a WebAssembly port.

Requirements

  • Go 1.11
  • Ebiten master branch (1.8.0-alpha.0.XXX) / 2.0.0-alpha

1. WasmServe

go get github.com/hajimehoshi/wasmserve
cd yourgame
wasmserve

Then access http://localhost:8080/.

2. Regular

Compile your game

GOOS=js GOARCH=wasm go build -o yourgame.wasm github.com/yourname/yourgame

Copy wasm_exec.js to execute the Wasm binary

cp $GOROOT/misc/wasm/wasm_exec.js .

Source: https://github.com/golang/go/tree/master/misc/wasm

Create an HTML

<!DOCTYPE html>
<script src="wasm_exec.js"></script>
<script>
// Polyfill
if (!WebAssembly.instantiateStreaming) {
  WebAssembly.instantiateStreaming = async (resp, importObject) => {
    const source = await (await resp).arrayBuffer();
    return await WebAssembly.instantiate(source, importObject);
  };
}

const go = new Go();
WebAssembly.instantiateStreaming(fetch("yourgame.wasm"), go.importObject).then(result => {
  go.run(result.instance);
});
</script>

TBD

Clone this wiki locally