Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm support and some cleanup. #18

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,63 @@ webgl_example.go:
package main

import (
"github.com/gopherjs/gopherjs/js"
"fmt"

"github.com/gopherjs/gopherwasm/js"
"github.com/gopherjs/webgl"
)

func main() {
document := js.Global.Get("document")
document := js.Global().Get("document")
canvas := document.Call("createElement", "canvas")
if canvas == js.Null() {
fmt.Println("CANVAS IS NULL")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Early return would be nice here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I was just using the existing example but yea sounds like an improvement. Will add that.

}

canvas.Set("width", 800)
canvas.Set("height", 600)
canvas.Get("style").Set("outline", "none")
document.Get("body").Call("appendChild", canvas)

attrs := webgl.DefaultAttributes()
attrs.Alpha = false

gl, err := webgl.NewContext(canvas, attrs)
if err != nil {
js.Global.Call("alert", "Error: "+err.Error())
js.Global().Call("alert", "Error: "+err.Error())
}

gl.ClearColor(0.8, 0.3, 0.01, 1)
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Call("clearColor", 0.8, 0.3, 0.01, 1)
gl.Call("clear", gl.COLOR_BUFFER_BIT)
}
```

webgl_example.html:
js_example.html:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was it renamed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it to make a clearer distinction between the JS and the WASM example. Webgl seemed ambiguous.


```html
<html><body><script src="webgl_example.js"></script></body></html>
```

To produce `webgl_example.js` file, run `gopherjs build webgl_example.go`.

wasm_example:
```html
<html>
<head>
<meta charset="utf-8">
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
go.run(result.instance);
});
</script>
</head>
<body></body>
</html>
```

1. `GOOS=js GOARCH=wasm go build -o main.wasm`
2. `cp "$(go env GOROOT)/misc/wasm/wasm_exec.js"`
3. Serve the files. E.g. with `goexec 'http.ListenAndServe(":8080", http.FileServer(http.Dir(".")))'`

Loading