diff --git a/canvas/engine.go b/canvas/engine.go index 10d5563..5f7f49e 100644 --- a/canvas/engine.go +++ b/canvas/engine.go @@ -1,16 +1,10 @@ package canvas import ( - "bytes" - "encoding/base64" - "errors" - "image" - "image/png" "syscall/js" "time" "git.kirsle.net/apps/doodle/lib/events" - "git.kirsle.net/apps/doodle/lib/render" ) // Engine implements a rendering engine targeting an HTML canvas for @@ -79,87 +73,6 @@ func (e *Engine) Present() error { return nil } -// Texture can hold on to cached image textures. -type Texture struct { - data string // data:image/png URI - image js.Value // DOM image element - canvas js.Value // Warmed up canvas element - ctx2d js.Value // 2D drawing context for the canvas. - width int - height int -} - -// NewTexture caches a texture from a bitmap. -func (e *Engine) NewTexture(filename string, img image.Image) (render.Texturer, error) { - var ( - fh = bytes.NewBuffer([]byte{}) - imageSize = img.Bounds().Size() - width = imageSize.X - height = imageSize.Y - ) - - // Encode to PNG format. - if err := png.Encode(fh, img); err != nil { - return nil, err - } - - var dataURI = "data:image/png;base64," + base64.StdEncoding.EncodeToString(fh.Bytes()) - - tex := &Texture{ - data: dataURI, - width: width, - height: height, - } - - // Preheat a cached Canvas object. - canvas := js.Global().Get("document").Call("createElement", "canvas") - canvas.Set("width", width) - canvas.Set("height", height) - tex.canvas = canvas - - ctx2d := canvas.Call("getContext", "2d") - tex.ctx2d = ctx2d - - // Load as a JS Image object. - image := js.Global().Call("eval", "new Image()") - image.Call("addEventListener", "load", js.FuncOf(func(this js.Value, args []js.Value) interface{} { - ctx2d.Call("drawImage", image, 0, 0) - return nil - })) - image.Set("src", tex.data) - tex.image = image - - // Cache the texture in memory. - e.textures[filename] = tex - - return tex, nil -} - -// Size returns the dimensions of the texture. -func (t *Texture) Size() render.Rect { - return render.NewRect(int32(t.width), int32(t.height)) -} - -// NewBitmap initializes a texture from a bitmap image. The image is stored -// in HTML5 Session Storage. -func (e *Engine) NewBitmap(filename string) (render.Texturer, error) { - if tex, ok := e.textures[filename]; ok { - return tex, nil - } - - panic("no bitmap for " + filename) - return nil, errors.New("no bitmap data stored for " + filename) -} - -// Copy a texturer bitmap onto the canvas. -func (e *Engine) Copy(t render.Texturer, src, dist render.Rect) { - tex := t.(*Texture) - - // e.canvas.ctx2d.Call("drawImage", tex.image, dist.X, dist.Y) - e.canvas.ctx2d.Call("drawImage", tex.canvas, dist.X, dist.Y) - -} - // Delay for a moment. func (e *Engine) Delay(delay uint32) { time.Sleep(time.Duration(delay) * time.Millisecond) diff --git a/canvas/texture.go b/canvas/texture.go new file mode 100644 index 0000000..be6da8a --- /dev/null +++ b/canvas/texture.go @@ -0,0 +1,91 @@ +package canvas + +import ( + "bytes" + "encoding/base64" + "errors" + "image" + "image/png" + "syscall/js" + + "git.kirsle.net/apps/doodle/lib/render" +) + +// Texture can hold on to cached image textures. +type Texture struct { + data string // data:image/png URI + image js.Value // DOM image element + canvas js.Value // Warmed up canvas element + ctx2d js.Value // 2D drawing context for the canvas. + width int + height int +} + +// StoreTexture caches a texture from a bitmap. +func (e *Engine) StoreTexture(name string, img image.Image) (render.Texturer, error) { + var ( + fh = bytes.NewBuffer([]byte{}) + imageSize = img.Bounds().Size() + width = imageSize.X + height = imageSize.Y + ) + + // Encode to PNG format. + if err := png.Encode(fh, img); err != nil { + return nil, err + } + + var dataURI = "data:image/png;base64," + base64.StdEncoding.EncodeToString(fh.Bytes()) + + tex := &Texture{ + data: dataURI, + width: width, + height: height, + } + + // Preheat a cached Canvas object. + canvas := js.Global().Get("document").Call("createElement", "canvas") + canvas.Set("width", width) + canvas.Set("height", height) + tex.canvas = canvas + + ctx2d := canvas.Call("getContext", "2d") + tex.ctx2d = ctx2d + + // Load as a JS Image object. + image := js.Global().Call("eval", "new Image()") + image.Call("addEventListener", "load", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + ctx2d.Call("drawImage", image, 0, 0) + return nil + })) + image.Set("src", tex.data) + tex.image = image + + // Cache the texture in memory. + e.textures[name] = tex + + return tex, nil +} + +// Size returns the dimensions of the texture. +func (t *Texture) Size() render.Rect { + return render.NewRect(int32(t.width), int32(t.height)) +} + +// LoadTexture recalls a cached texture image. +func (e *Engine) LoadTexture(name string) (render.Texturer, error) { + if tex, ok := e.textures[name]; ok { + return tex, nil + } + + return nil, errors.New("no bitmap data stored for " + name) +} + +// Copy a texturer bitmap onto the canvas. +func (e *Engine) Copy(t render.Texturer, src, dist render.Rect) { + tex := t.(*Texture) + + // e.canvas.ctx2d.Call("drawImage", tex.image, dist.X, dist.Y) + e.canvas.ctx2d.Call("drawImage", tex.canvas, dist.X, dist.Y) + +} diff --git a/interface.go b/interface.go index 5f912df..82d3966 100644 --- a/interface.go +++ b/interface.go @@ -32,8 +32,8 @@ type Engine interface { ComputeTextRect(Text) (Rect, error) // Texture caching. - NewBitmap(filename string) (Texturer, error) - NewTexture(filename string, img image.Image) (Texturer, error) + StoreTexture(name string, img image.Image) (Texturer, error) + LoadTexture(name string) (Texturer, error) Copy(t Texturer, src, dst Rect) // Delay for a moment using the render engine's delay method, diff --git a/sdl/texture.go b/sdl/texture.go index 677ef72..27b0cc7 100644 --- a/sdl/texture.go +++ b/sdl/texture.go @@ -28,8 +28,8 @@ type Texture struct { height int32 } -// NewTexture caches an SDL texture from a bitmap. -func (r *Renderer) NewTexture(filename string, img image.Image) (render.Texturer, error) { +// StoreTexture caches an SDL texture from a bitmap. +func (r *Renderer) StoreTexture(name string, img image.Image) (render.Texturer, error) { var ( fh = bytes.NewBuffer([]byte{}) ) @@ -65,7 +65,7 @@ func (r *Renderer) NewTexture(filename string, img image.Image) (render.Texturer height: surface.H, tex: texture, } - r.textures[filename] = tex + r.textures[name] = tex return tex, nil } @@ -75,10 +75,10 @@ func (t *Texture) Size() render.Rect { return render.NewRect(t.width, t.height) } -// NewBitmap initializes a texture from a bitmap image. -func (r *Renderer) NewBitmap(filename string) (render.Texturer, error) { - if tex, ok := r.textures[filename]; ok { +// LoadTexture initializes a texture from a bitmap image. +func (r *Renderer) LoadTexture(name string) (render.Texturer, error) { + if tex, ok := r.textures[name]; ok { return tex, nil } - return nil, fmt.Errorf("NewBitmap(%s): not found in texture cache", filename) + return nil, fmt.Errorf("LoadTexture(%s): not found in texture cache", name) }