Add loadimage_js.go

This commit is contained in:
Hajime Hoshi 2015-01-02 16:48:07 +09:00
parent e9527df0f5
commit bd4434c62c
6 changed files with 82 additions and 5 deletions

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !js
package ebitenutil
import (

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -28,5 +28,5 @@ type Context struct {
ElementArrayBuffer BufferType
DynamicDraw BufferUsageType
StaticDraw BufferUsageType
*context
context
}

View File

@ -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
}