Skip to content

Instantly share code, notes, and snippets.

@fogleman
Created February 17, 2016 22:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogleman/9997401c65fa1e880451 to your computer and use it in GitHub Desktop.
Save fogleman/9997401c65fa1e880451 to your computer and use it in GitHub Desktop.
Drawing in Go?
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