// drawPencil takes an (x, y) position (from a MotionNotify event) and draws // a rectangle of size pencilTip on to canvas. func drawPencil(canvas *xgraphics.Image, win *xwindow.Window, x, y int) { // Create a subimage at (x, y) with pencilTip width and height from canvas. // Creating subimages is very cheap---no pixels are copied. // Moreover, when subimages are drawn to the screen, only the pixels in // the sub-image are sent to X. tipRect := midRect(x, y, pencilTip, pencilTip, width, height) // If the rectangle contains no pixels, don't draw anything. if tipRect.Empty() { return } // Output a little message. log.Printf("Drawing pencil point at (%d, %d)", x, y) // Create the subimage of the canvas to draw to. tip := canvas.SubImage(tipRect) // Now color each pixel in tip with the pencil color. tip.For(func(x, y int) xgraphics.BGRA { return xgraphics.BlendBGRA(canvas.At(x, y).(xgraphics.BGRA), pencil) }) // Now draw the changes to the pixmap. tip.XDraw() // And paint them to the window. tip.XPaint(win.Id) }
// clearCanvas erases all your pencil marks. func clearCanvas(canvas *xgraphics.Image, win *xwindow.Window) { log.Println("Clearing canvas...") canvas.For(func(x, y int) xgraphics.BGRA { return bg }) canvas.XDraw() canvas.XPaint(win.Id) }
// drawGopher draws the gopher image to the canvas. func drawGopher(canvas *xgraphics.Image, gopher image.Image, win *xwindow.Window, x, y int) { // Find the rectangle of the canvas where we're going to draw the gopher. gopherRect := midRect(x, y, gopherWidth, gopherHeight, width, height) // If the rectangle contains no pixels, don't draw anything. if gopherRect.Empty() { return } // Output a little message. log.Printf("Drawing gopher at (%d, %d)", x, y) // Get a subimage of the gopher that's in sync with gopherRect. gopherPt := image.Pt(gopher.Bounds().Min.X, gopher.Bounds().Min.Y) if gopherRect.Min.X == 0 { gopherPt.X = gopherWidth - gopherRect.Dx() } if gopherRect.Min.Y == 0 { gopherPt.Y = gopherHeight - gopherRect.Dy() } // Create the canvas subimage. subCanvas := canvas.SubImage(gopherRect) // Blend the gopher image into the sub-canvas. // This does alpha blending. xgraphics.Blend(subCanvas, gopher, gopherPt) // Now draw the changes to the pixmap. subCanvas.XDraw() // And paint them to the window. subCanvas.XPaint(win.Id) }