Skip to content

Instantly share code, notes, and snippets.

@klauspost
Created October 21, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klauspost/e6105a857de22a028171 to your computer and use it in GitHub Desktop.
Save klauspost/e6105a857de22a028171 to your computer and use it in GitHub Desktop.
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