From cf788d55f37f5dbdd3827c769806cafae2756071 Mon Sep 17 00:00:00 2001 From: Thomas Friedel Date: Fri, 22 Feb 2019 11:50:23 +0100 Subject: [PATCH] unified Clear and ClearRect --- backend/backendbase/base.go | 2 +- backend/gogl/fill.go | 30 ++++++++++++++++++++++-------- backend/gogl/gogl.go | 2 ++ paths.go | 7 ------- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/backend/backendbase/base.go b/backend/backendbase/base.go index 205af19..0dccb25 100644 --- a/backend/backendbase/base.go +++ b/backend/backendbase/base.go @@ -14,7 +14,7 @@ type Backend interface { LoadLinearGradient(data *LinearGradientData) LinearGradient LoadRadialGradient(data *RadialGradientData) RadialGradient - ClearRect(x, y, w, h int) + // ClearRect(x, y, w, h int) Clear(pts [4][2]float64) Fill(style *FillStyle, pts [][2]float64) DrawImage(dimg Image, sx, sy, sw, sh, dx, dy, dw, dh float64, alpha float64) diff --git a/backend/gogl/fill.go b/backend/gogl/fill.go index a18e410..f77ca01 100644 --- a/backend/gogl/fill.go +++ b/backend/gogl/fill.go @@ -9,15 +9,22 @@ import ( "github.com/tfriedel6/canvas/backend/backendbase" ) -// ClearRect sets the color of the rectangle to transparent black -func (b *GoGLBackend) ClearRect(x, y, w, h int) { - gl.Scissor(int32(x), int32(b.h-y-h), int32(w), int32(h)) - gl.ClearColor(0, 0, 0, 0) - gl.Clear(gl.COLOR_BUFFER_BIT) - // cv.applyScissor() -} - func (b *GoGLBackend) Clear(pts [4][2]float64) { + // first check if the four points are aligned to form a nice rectangle, which can be more easily + // cleared using glScissor and glClear + aligned := pts[0][0] == pts[1][0] && pts[2][0] == pts[3][0] && pts[0][1] == pts[3][1] && pts[1][1] == pts[2][1] + if !aligned { + aligned = pts[0][0] == pts[3][0] && pts[1][0] == pts[2][0] && pts[0][1] == pts[1][1] && pts[2][1] == pts[3][1] + } + if aligned { + minX := math.Floor(math.Min(pts[0][0], pts[2][0])) + maxX := math.Ceil(math.Max(pts[0][0], pts[2][0])) + minY := math.Floor(math.Min(pts[0][1], pts[2][1])) + maxY := math.Ceil(math.Max(pts[0][1], pts[2][1])) + b.clearRect(int(minX), int(minY), int(maxX)-int(minX), int(maxY)-int(minY)) + return + } + data := [8]float32{ float32(pts[0][0]), float32(pts[0][1]), float32(pts[1][0]), float32(pts[1][1]), @@ -46,6 +53,13 @@ func (b *GoGLBackend) Clear(pts [4][2]float64) { gl.Enable(gl.BLEND) } +func (b *GoGLBackend) clearRect(x, y, w, h int) { + gl.Scissor(int32(x), int32(b.h-y-h), int32(w), int32(h)) + gl.ClearColor(0, 0, 0, 0) + gl.Clear(gl.COLOR_BUFFER_BIT) + // cv.applyScissor() +} + func (b *GoGLBackend) Fill(style *backendbase.FillStyle, pts [][2]float64) { if style.Blur > 0 { b.offscr1.alpha = true diff --git a/backend/gogl/gogl.go b/backend/gogl/gogl.go index 720641d..89a5c94 100644 --- a/backend/gogl/gogl.go +++ b/backend/gogl/gogl.go @@ -414,6 +414,8 @@ func (b *GoGLBackend) enableTextureRenderTarget(offscr *offscreenBuffer) { } gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) + gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) + gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.GenFramebuffers(1, &offscr.frameBuf) gl.BindFramebuffer(gl.FRAMEBUFFER, offscr.frameBuf) diff --git a/paths.go b/paths.go index 39c146c..75c9fa0 100644 --- a/paths.go +++ b/paths.go @@ -536,17 +536,10 @@ func (cv *Canvas) FillRect(x, y, w, h float64) { func (cv *Canvas) ClearRect(x, y, w, h float64) { cv.activate() - if cv.state.transform == matIdentity() { - cv.b.ClearRect(int(x+0.5), int(y+0.5), int(w+0.5), int(h+0.5)) - cv.applyScissor() - return - } - p0 := cv.tf(vec{x, y}) p1 := cv.tf(vec{x, y + h}) p2 := cv.tf(vec{x + w, y + h}) p3 := cv.tf(vec{x + w, y}) - data := [4][2]float64{{p0[0], p0[1]}, {p1[0], p1[1]}, {p2[0], p2[1]}, {p3[0], p3[1]}} cv.b.Clear(data)