diff --git a/internal/graphicsdriver/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go index 58268d40f..8409fed40 100644 --- a/internal/graphicsdriver/opengl/context_js.go +++ b/internal/graphicsdriver/opengl/context_js.go @@ -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) diff --git a/internal/graphicsdriver/opengl/graphics_js.go b/internal/graphicsdriver/opengl/graphics_js.go index f02e003d7..631e9dc7e 100644 --- a/internal/graphicsdriver/opengl/graphics_js.go +++ b/internal/graphicsdriver/opengl/graphics_js.go @@ -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 } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 13061833a..a73dd24c4 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -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) }