From 46f21a3096b43354eb9d156d8851c547a1fa2d1a Mon Sep 17 00:00:00 2001
From: faiface <faiface@ksp.sk>
Date: Wed, 8 Mar 2017 19:19:20 +0100
Subject: [PATCH] fix changing Canvas Bounds

---
 pixelgl/canvas.go | 44 +++++++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 21 deletions(-)

diff --git a/pixelgl/canvas.go b/pixelgl/canvas.go
index b3a57a1..34033c1 100644
--- a/pixelgl/canvas.go
+++ b/pixelgl/canvas.go
@@ -16,20 +16,20 @@ import (
 //
 // It supports TrianglesPosition, TrianglesColor, TrianglesPicture and PictureColor.
 type Canvas struct {
-	f *glhf.Frame
-	s *glhf.Shader
+	// these should **only** be accessed through orig
+	f       *glhf.Frame
+	borders pixel.Rect
+	pixels  []uint8
+	dirty   bool
 
+	// these should **never** be accessed through orig
+	s      *glhf.Shader
+	bounds pixel.Rect
+	mat    mgl32.Mat3
+	col    mgl32.Vec4
 	smooth bool
 
-	mat mgl32.Mat3
-	col mgl32.Vec4
-
-	pixels []uint8
-	dirty  bool
-
-	borders pixel.Rect
-	bounds  pixel.Rect
-	orig    *Canvas
+	orig *Canvas
 }
 
 // NewCanvas creates a new empty, fully transparent Canvas with given bounds. If the smooth flag
@@ -169,15 +169,15 @@ func (c *Canvas) SetBounds(bounds pixel.Rect) {
 	}
 
 	mainthread.Call(func() {
-		oldF := c.f
+		oldF := c.orig.f
 
 		_, _, w, h := intBounds(bounds)
 		c.f = glhf.NewFrame(w, h, c.smooth)
 
 		// preserve old content
 		if oldF != nil {
-			relBounds := c.bounds
-			relBounds.Pos -= bounds.Pos
+			relBounds := bounds
+			relBounds.Pos -= c.orig.borders.Pos
 			ox, oy, ow, oh := intBounds(relBounds)
 			oldF.Blit(
 				c.f,
@@ -187,9 +187,11 @@ func (c *Canvas) SetBounds(bounds pixel.Rect) {
 		}
 	})
 
-	c.orig = c // detach from the Original
+	// detach from orig
 	c.borders = bounds
+	c.pixels = nil
 	c.dirty = true
+	c.orig = c
 }
 
 // Bounds returns the rectangular bounds of the Canvas.
@@ -233,14 +235,14 @@ func (c *Canvas) Clear(color color.Color) {
 
 	mainthread.CallNonBlock(func() {
 		c.setGlhfBounds()
-		c.f.Begin()
+		c.orig.f.Begin()
 		glhf.Clear(
 			float32(nrgba.R),
 			float32(nrgba.G),
 			float32(nrgba.B),
 			float32(nrgba.A),
 		)
-		c.f.End()
+		c.orig.f.End()
 	})
 }
 
@@ -267,7 +269,7 @@ func (c *Canvas) Original() pixel.Picture {
 func (c *Canvas) Color(at pixel.Vec) pixel.NRGBA {
 	if c.orig.dirty {
 		mainthread.Call(func() {
-			tex := c.f.Texture()
+			tex := c.orig.f.Texture()
 			tex.Begin()
 			c.orig.pixels = tex.Pixels(0, 0, tex.Width(), tex.Height())
 			tex.End()
@@ -303,7 +305,7 @@ func (ct *canvasTriangles) draw(tex *glhf.Texture, borders, bounds pixel.Rect) {
 
 	mainthread.CallNonBlock(func() {
 		ct.dst.setGlhfBounds()
-		ct.dst.f.Begin()
+		ct.dst.orig.f.Begin()
 		ct.dst.s.Begin()
 
 		ct.dst.s.SetUniformAttr(canvasBounds, mgl32.Vec4{
@@ -347,7 +349,7 @@ func (ct *canvasTriangles) draw(tex *glhf.Texture, borders, bounds pixel.Rect) {
 		}
 
 		ct.dst.s.End()
-		ct.dst.f.End()
+		ct.dst.orig.f.End()
 	})
 }
 
@@ -436,7 +438,7 @@ func (ccp *canvasCanvasPicture) Draw(t pixel.TargetTriangles) {
 	if ccp.dst != ct.dst {
 		panic(fmt.Errorf("%T.Draw: TargetTriangles generated by different Canvas", ccp))
 	}
-	ct.draw(ccp.src.f.Texture(), ccp.src.orig.borders, ccp.bounds)
+	ct.draw(ccp.src.orig.f.Texture(), ccp.src.orig.borders, ccp.bounds)
 }
 
 const (
-- 
GitLab