-
-
Save klauspost/e6105a857de22a028171 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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