diff --git a/canvas.go b/canvas.go index aa9b19907085bccf0a6a29ad2e4471786b7f2945..e082c186bf41e6e4276238ebd40006acc999cb57 100644 --- a/canvas.go +++ b/canvas.go @@ -108,11 +108,7 @@ func (c *Canvas) Size() (width, height float64) { // as you draw onto the Canvas, so there is no real need to call this method more than once (but it // might be beneficial to your code to do so). func (c *Canvas) Content() *Picture { - tex := c.f.Texture() - return &Picture{ - texture: tex, - bounds: R(0, 0, float64(tex.Width()), float64(tex.Height())), - } + return PictureFromTexture(c.f.Texture()) } // Clear fills the whole Canvas with one specified color. diff --git a/picture.go b/picture.go index c90b232e923fb113500265f84eb9f5bdb7ef4635..17569ac2d9d1cae9049b2380d2df11e3432739d7 100644 --- a/picture.go +++ b/picture.go @@ -14,8 +14,8 @@ import ( // generated. After the creation, Pictures can be sliced (slicing creates a "sub-Picture" // from a Picture) into smaller Pictures. type Picture struct { - texture *pixelgl.Texture - bounds Rect + tex *pixelgl.Texture + bounds Rect } // NewPicture creates a new Picture from an image.Image. @@ -35,9 +35,9 @@ func NewPicture(img image.Image, smooth bool) *Picture { copy(jSlice, tmp) } - var texture *pixelgl.Texture + var tex *pixelgl.Texture mainthread.Call(func() { - texture = pixelgl.NewTexture( + tex = pixelgl.NewTexture( img.Bounds().Dx(), img.Bounds().Dy(), smooth, @@ -45,9 +45,14 @@ func NewPicture(img image.Image, smooth bool) *Picture { ) }) + return PictureFromTexture(tex) +} + +// PictureFromTexture returns a new Picture that spans the whole supplied Texture. +func PictureFromTexture(tex *pixelgl.Texture) *Picture { return &Picture{ - texture: texture, - bounds: R(0, 0, float64(texture.Width()), float64(texture.Height())), + tex: tex, + bounds: R(0, 0, float64(tex.Width()), float64(tex.Height())), } } @@ -57,14 +62,14 @@ func (p *Picture) Image() *image.NRGBA { nrgba := image.NewNRGBA(image.Rect(0, 0, int(bounds.W()), int(bounds.H()))) mainthread.Call(func() { - p.texture.Begin() - nrgba.Pix = p.texture.Pixels( + p.tex.Begin() + nrgba.Pix = p.tex.Pixels( int(bounds.X()), int(bounds.Y()), int(bounds.W()), int(bounds.H()), ) - p.texture.End() + p.tex.End() }) // flip the image vertically @@ -82,7 +87,7 @@ func (p *Picture) Image() *image.NRGBA { // Texture returns a pointer to the underlying OpenGL texture of the Picture. func (p *Picture) Texture() *pixelgl.Texture { - return p.texture + return p.tex } // Slice returns a Picture within the supplied rectangle of the original picture. The original @@ -92,8 +97,8 @@ func (p *Picture) Texture() *pixelgl.Texture { // 100, 50, 100), we get the upper-right quadrant of the original Picture. func (p *Picture) Slice(slice Rect) *Picture { return &Picture{ - texture: p.texture, - bounds: Rect{p.bounds.Pos + slice.Pos, slice.Size}, + tex: p.tex, + bounds: Rect{p.bounds.Pos + slice.Pos, slice.Size}, } }