Created
February 17, 2016 22:32
-
-
Save fogleman/9997401c65fa1e880451 to your computer and use it in GitHub Desktop.
Drawing in Go?
This file contains 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
package main | |
import ( | |
"image" | |
"image/draw" | |
"image/png" | |
"os" | |
"github.com/golang/freetype/raster" | |
"golang.org/x/image/math/fixed" | |
) | |
func SavePNG(path string, im image.Image) error { | |
file, err := os.Create(path) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
return png.Encode(file, im) | |
} | |
func main() { | |
const width = 1024 | |
const height = 1024 | |
im := image.NewRGBA(image.Rect(0, 0, width, height)) | |
draw.Draw(im, im.Bounds(), image.White, image.ZP, draw.Src) | |
painter := raster.NewRGBAPainter(im) | |
painter.SetColor(image.Black) | |
r := raster.NewRasterizer(width, height) | |
r.UseNonZeroWinding = true | |
var path raster.Path | |
path.Start(fixed.P(20, 40)) | |
path.Add1(fixed.P(width-20, height-40)) | |
path.Add1(fixed.P(width/2, 40)) | |
r.AddStroke(path, fixed.I(16), raster.RoundCapper, raster.RoundJoiner) | |
r.Rasterize(painter) | |
SavePNG("out.png", im) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment