mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
Add loadimage_js.go
This commit is contained in:
parent
e9527df0f5
commit
bd4434c62c
@ -12,6 +12,8 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// +build !js
|
||||||
|
|
||||||
package ebitenutil
|
package ebitenutil
|
||||||
|
|
||||||
import (
|
import (
|
66
ebitenutil/loadimage_js.go
Normal file
66
ebitenutil/loadimage_js.go
Normal 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
|
||||||
|
}
|
@ -46,7 +46,6 @@ func NewContext() *Context {
|
|||||||
|
|
||||||
func (c *Context) init() {
|
func (c *Context) init() {
|
||||||
gl.Init()
|
gl.Init()
|
||||||
gl.Enable(gl.TEXTURE_2D)
|
|
||||||
// Textures' pixel formats are alpha premultiplied.
|
// Textures' pixel formats are alpha premultiplied.
|
||||||
gl.Enable(gl.BLEND)
|
gl.Enable(gl.BLEND)
|
||||||
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
||||||
|
@ -50,7 +50,6 @@ func NewContext(gl *webgl.Context) *Context {
|
|||||||
|
|
||||||
func (c *Context) init() {
|
func (c *Context) init() {
|
||||||
gl := c.gl
|
gl := c.gl
|
||||||
gl.Enable(gl.TEXTURE_2D)
|
|
||||||
// Textures' pixel formats are alpha premultiplied.
|
// Textures' pixel formats are alpha premultiplied.
|
||||||
gl.Enable(gl.BLEND)
|
gl.Enable(gl.BLEND)
|
||||||
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
||||||
|
@ -28,5 +28,5 @@ type Context struct {
|
|||||||
ElementArrayBuffer BufferType
|
ElementArrayBuffer BufferType
|
||||||
DynamicDraw BufferUsageType
|
DynamicDraw BufferUsageType
|
||||||
StaticDraw BufferUsageType
|
StaticDraw BufferUsageType
|
||||||
*context
|
context
|
||||||
}
|
}
|
||||||
|
@ -47,8 +47,17 @@ func SwapBuffers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
canvas = js.Global.Get("Canvas").New()
|
ch := make(chan struct{})
|
||||||
js.Global.Get("document").Get("body").Call("appendChild", canvas)
|
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{
|
webglContext, err := webgl.NewContext(canvas, &webgl.ContextAttributes{
|
||||||
Alpha: true,
|
Alpha: true,
|
||||||
PremultipliedAlpha: true,
|
PremultipliedAlpha: true,
|
||||||
@ -60,5 +69,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Start(width, height, scale int, title string) (actualScale int, err error) {
|
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
|
return scale, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user