Skip to content

WebAssembly

Hajime Hoshi edited this page Oct 10, 2018 · 28 revisions

The easiest way is to Dave's awesome wasmgo

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

Option 1. WasmGo

Upload your package via wasmgo. See https://github.com/dave/wasmgo

Option 2. WasmServe

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

Then access http://localhost:8080/.

Option 3. 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