-
Notifications
You must be signed in to change notification settings - Fork 16
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
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee6097a
change to support gopherjs/wasm
dbriemann fb0146a
add init for opengl constants
dbriemann ab96214
remove constants that are not defined in webgl
dbriemann d8188a9
update readme
dbriemann 4045118
implement first batch of review changes
dbriemann e93c0cc
panic on missing value
dbriemann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
} | ||
|
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was it renamed? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(".")))'` | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.