Skip to content

Instantly share code, notes, and snippets.

@klauspost
Created Oct 21, 2015
Embed
What would you like to do?
type Drawer interface {
DrawTo(image.Image)
}
type Line struct {
color Color
a Point
b Point
}
func (d Line) DrawTo(bg image.Image) {}
type Triangle struct {
color Color
fill Color
a Point
b Point
c Point
}
func (d Triangle) DrawTo(bg image.Image) {}
type Fill struct {
color Color
}
func (d Fill) DrawTo(bg image.Image) {}
func paintingLoop(img image.Image, ch chan Drawer) {
// shorthand for a loop over receiving over
// a channel
for msg := range ch {
msg.DrawTo(img)
}
}
func drawThingy(ch chan Drawer) {
ch <- Line{color: Red, a: Point{x: 0, y: 0}, b: Point{x: 1, y: 1}}
ch <- Background{color: Blue}
}
func main() {
// do some setup
ch := make(chan Drawer, 100)
// Create out destination image
var img = image.NewNRGBA(image.Rectangle(100, 100))
// Creates a goroutine which will handle the
// event loop
go func() {
// create the window, etc
paintingLoop(ch, img)
}()
drawThingy(ch)
// Close when done.
close(ch)
// TODO: Add a channel to indicate image is done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment