GopherJS
GopherJS is a software to convert Go to JavaScript. You can run your Ebitengine game on browsers. As Ebitengine v2 does not support GopherJS and vice versa, it is recomended to use WebAssembly if possible. If you want to use GopherJS, you have to use Ebitengine v1.
Installing GopherJS
Install gopherjs command. You might need to set GO111MODULE
off
.
go1.12.17 get github.com/gopherjs/gopherjs
Compile your game with GopherJS:
gopherjs build -o yourgame.js path/to/yourgame
Then, open the below HTML on your HTTP server:
<!DOCTYPE html>
<script src="yourgame.js"></script></code>
NOTE: file://
URL may not work with Ebitengine. Execute your game on a HTTP server.
If you want to embed your game into your web page, using iframe is strongly recommended.
You might find this message with Chrome:
The AudioContext was not allowed to start. It must be resume (or created) after a user gesture on the page. https://goo.gl/7K7WLu
In this case, you can solve this by putting allow="autoplay"
on the iframe.
Emulating context-lost
document.getElementsByTagName("canvas")[0].getContext("webgl").getExtension('WEBGL_lose_context').loseContext();
Note that Ebitengine automatically restores images on desktop browsers when the context is lost. This restoring doesn't work on mobile browsers for a performance reason.
Running examples on browsers
gopherjs serve --tags=example
And access to http://127.0.0.1:8080/github.com/hajimehoshi/ebiten/examples/blocks
.
On Windows, you might need to set GOOS environment variable:
set GOOS=linux