internal/ui: pass the canvas element directly instead of an ID

This commit is contained in:
Hajime Hoshi 2022-06-11 15:34:17 +09:00
parent 95628ee5f7
commit 71a32d2036
3 changed files with 10 additions and 9 deletions

View File

@ -110,7 +110,7 @@ func uint8ArrayToSlice(value js.Value, length int) []byte {
type contextImpl struct {
gl *gl
canvasID string
canvas js.Value
lastProgramID programID
webGLVersion webGLVersion
}
@ -126,7 +126,7 @@ func (c *context) initGL() error {
// TODO: Define id?
if doc := js.Global().Get("document"); doc.Truthy() {
canvas := doc.Call("getElementById", c.canvasID)
canvas := c.canvas
attr := js.Global().Get("Object").New()
attr.Set("alpha", true)
attr.Set("premultipliedAlpha", true)

View File

@ -14,6 +14,10 @@
package opengl
func (g *Graphics) SetCanvasID(id string) {
g.context.canvasID = id
import (
"syscall/js"
)
func (g *Graphics) SetCanvas(canvas js.Value) {
g.context.canvas = canvas
}

View File

@ -15,7 +15,6 @@
package ui
import (
"fmt"
"syscall/js"
"time"
@ -438,8 +437,6 @@ func init() {
document.Get("head").Call("appendChild", meta)
canvas = document.Call("createElement", "canvas")
now := time.Now().UnixNano()
canvas.Set("id", fmt.Sprintf("ebitengine-%d.%d", now/1e9, (now%1e9)/1e6))
canvas.Set("width", 16)
canvas.Set("height", 16)
@ -620,8 +617,8 @@ func (u *userInterfaceImpl) Run(game Game) error {
return err
}
u.graphicsDriver = g
if g, ok := u.graphicsDriver.(interface{ SetCanvasID(string) }); ok {
g.SetCanvasID(canvas.Get("id").String())
if g, ok := u.graphicsDriver.(interface{ SetCanvas(js.Value) }); ok {
g.SetCanvas(canvas)
}
return <-u.loop(game)
}