diff --git a/ebitenutil/image.go b/ebitenutil/loadimage.go similarity index 98% rename from ebitenutil/image.go rename to ebitenutil/loadimage.go index 4f24447c2..6bc8e91e9 100644 --- a/ebitenutil/image.go +++ b/ebitenutil/loadimage.go @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +// +build !js + package ebitenutil import ( diff --git a/ebitenutil/loadimage_js.go b/ebitenutil/loadimage_js.go new file mode 100644 index 000000000..764dc4c61 --- /dev/null +++ b/ebitenutil/loadimage_js.go @@ -0,0 +1,66 @@ +// Copyright 2015 Hajime Hoshi +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build js + +package ebitenutil + +import ( + "bytes" + "errors" + "fmt" + "github.com/gopherjs/gopherjs/js" + "github.com/hajimehoshi/ebiten" + "image" +) + +func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.Image, error) { + var err error + var content js.Object + ch := make(chan struct{}) + req := js.Global.Get("XMLHttpRequest").New() + req.Set("responseType", "arraybuffer") + req.Call("open", "GET", path, true) + req.Set("onload", func() { + defer close(ch) + status := req.Get("status").Int() + if 200 <= status && status < 400 { + content = req.Get("response") + return + } + err = errors.New(fmt.Sprintf("http error: %d", status)) + }) + req.Set("onerror", func() { + defer close(ch) + // TODO: Add more information. + err = errors.New("http error") + }) + req.Call("send") + <-ch + if err != nil { + return nil, nil, err + } + + data := js.Global.Get("Uint8Array").New(content).Interface().([]uint8) + b := bytes.NewBuffer(data) + img, _, err := image.Decode(b) + if err != nil { + return nil, nil, err + } + img2, err := ebiten.NewImageFromImage(img, filter) + if err != nil { + return nil, nil, err + } + return img2, img, nil +} diff --git a/internal/opengl/context.go b/internal/opengl/context.go index 920a74e2c..3b1eb8833 100644 --- a/internal/opengl/context.go +++ b/internal/opengl/context.go @@ -46,7 +46,6 @@ func NewContext() *Context { func (c *Context) init() { gl.Init() - gl.Enable(gl.TEXTURE_2D) // Textures' pixel formats are alpha premultiplied. gl.Enable(gl.BLEND) gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) diff --git a/internal/opengl/context_js.go b/internal/opengl/context_js.go index f211e4abb..41d8598b9 100644 --- a/internal/opengl/context_js.go +++ b/internal/opengl/context_js.go @@ -50,7 +50,6 @@ func NewContext(gl *webgl.Context) *Context { func (c *Context) init() { gl := c.gl - gl.Enable(gl.TEXTURE_2D) // Textures' pixel formats are alpha premultiplied. gl.Enable(gl.BLEND) gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) diff --git a/internal/opengl/types.go b/internal/opengl/types.go index a93e76648..933ce09f9 100644 --- a/internal/opengl/types.go +++ b/internal/opengl/types.go @@ -28,5 +28,5 @@ type Context struct { ElementArrayBuffer BufferType DynamicDraw BufferUsageType StaticDraw BufferUsageType - *context + context } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index b17b88559..bfe894d46 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -47,8 +47,17 @@ func SwapBuffers() { } func init() { - canvas = js.Global.Get("Canvas").New() - js.Global.Get("document").Get("body").Call("appendChild", canvas) + ch := make(chan struct{}) + js.Global.Get("window").Set("onload", func() { + close(ch) + }) + <-ch + + doc := js.Global.Get("document") + canvas = doc.Call("createElement", "canvas") + canvas.Set("width", 16) + canvas.Set("height", 16) + doc.Get("body").Call("appendChild", canvas) webglContext, err := webgl.NewContext(canvas, &webgl.ContextAttributes{ Alpha: true, PremultipliedAlpha: true, @@ -60,5 +69,7 @@ func init() { } func Start(width, height, scale int, title string) (actualScale int, err error) { + canvas.Set("width", width*scale) + canvas.Set("height", height*scale) return scale, nil }