Skip to content
Snippets Groups Projects
Commit 30681534 authored by faiface's avatar faiface
Browse files

add Canvas.SetComposeMethod

parent c1d00085
No related branches found
No related tags found
No related merge requests found
......@@ -19,11 +19,14 @@ type Canvas struct {
gf *GLFrame
shader *glhf.Shader
cmp pixel.ComposeMethod
mat mgl32.Mat3
col mgl32.Vec4
smooth bool
}
var _ pixel.ComposeTarget = (*Canvas)(nil)
// NewCanvas creates a new empty, fully transparent Canvas with given bounds. If the smooth flag is
// set, then stretched Pictures will be smoothed and will not be drawn pixely onto this Canvas.
func NewCanvas(bounds pixel.Rect, smooth bool) *Canvas {
......@@ -107,6 +110,12 @@ func (c *Canvas) SetColorMask(col color.Color) {
}
}
// SetComposeMethod sets a Porter-Duff composition method to be used in the following draws onto
// this Canvas.
func (c *Canvas) SetComposeMethod(cmp pixel.ComposeMethod) {
c.cmp = cmp
}
// SetBounds resizes the Canvas to the new bounds. Old content will be preserved.
func (c *Canvas) SetBounds(bounds pixel.Rect) {
c.gf.SetBounds(bounds)
......@@ -135,6 +144,32 @@ func (c *Canvas) setGlhfBounds() {
glhf.Bounds(bx, by, bw, bh)
}
// must be manually called inside mainthread
func (c *Canvas) setBlendFunc() {
switch c.cmp {
case pixel.ComposeOver:
glhf.BlendFunc(glhf.One, glhf.OneMinusSrcAlpha)
case pixel.ComposeIn:
glhf.BlendFunc(glhf.DstAlpha, glhf.Zero)
case pixel.ComposeOut:
glhf.BlendFunc(glhf.OneMinusDstAlpha, glhf.Zero)
case pixel.ComposeAtop:
glhf.BlendFunc(glhf.DstAlpha, glhf.OneMinusSrcAlpha)
case pixel.ComposeDstOver:
glhf.BlendFunc(glhf.OneMinusDstAlpha, glhf.One)
case pixel.ComposeDstIn:
glhf.BlendFunc(glhf.Zero, glhf.SrcAlpha)
case pixel.ComposeDstOut:
glhf.BlendFunc(glhf.Zero, glhf.OneMinusSrcAlpha)
case pixel.ComposeDstAtop:
glhf.BlendFunc(glhf.OneMinusDstAlpha, glhf.SrcAlpha)
case pixel.ComposeXor:
glhf.BlendFunc(glhf.OneMinusDstAlpha, glhf.OneMinusSrcAlpha)
default:
panic(errors.New("Canvas: invalid compose method"))
}
}
// Clear fills the whole Canvas with a single color.
func (c *Canvas) Clear(color color.Color) {
c.gf.Dirty()
......@@ -188,7 +223,7 @@ func (ct *canvasTriangles) draw(tex *glhf.Texture, bounds pixel.Rect) {
mainthread.CallNonBlock(func() {
ct.dst.setGlhfBounds()
glhf.BlendFunc(glhf.One, glhf.OneMinusSrcAlpha)
ct.dst.setBlendFunc()
frame := ct.dst.gf.Frame()
shader := ct.dst.shader
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment