Draw2d changed to use our own freetype fork
Find a file
2015-07-11 19:31:36 +02:00
curve redirect curve tests results to output.curve folder 2015-07-10 17:20:23 +02:00
draw2dgl replace gc.Current.Path.Clear() with gc.Current.Path = draw2d.NewPathStorage(); fixes #61 2015-07-11 01:22:50 +02:00
draw2dpdf only regenerate pdf if it has been deleted 2015-07-11 19:19:03 +02:00
output only regenerate pdf if it has been deleted 2015-07-11 19:19:03 +02:00
raster redirect curve tests results to output.curve folder 2015-07-10 17:20:23 +02:00
resource remove resource/result 2015-07-11 19:06:10 +02:00
samples golint fixes 2015-07-11 19:17:24 +02:00
.gitignore redirect curve tests results to output.curve folder 2015-07-10 17:20:23 +02:00
.project Remove oldies 2011-04-27 10:06:14 +02:00
advanced_path.go fix golint for advanced_path.go 2015-07-08 00:17:58 +02:00
arc.go go vet fixes 2015-07-10 15:24:23 +02:00
AUTHORS Added Stani to authors 2015-07-11 19:21:23 +02:00
curves.go Merge simpler import path 2015-04-19 17:14:42 +02:00
dasher.go Merge simpler import path 2015-04-19 17:14:42 +02:00
demux_converter.go Merge simpler import path 2015-04-19 17:14:42 +02:00
draw2d.go merge upstream 2015-07-10 12:48:04 +02:00
fileutil.go Add Save and Load util function 2015-04-22 15:59:01 +02:00
font.go use clean path for font folder 2015-07-10 02:06:47 +02:00
gc.go fix golint for gc.go 2015-07-08 00:42:00 +02:00
image.go replace gc.Current.Path.Clear() with gc.Current.Path = draw2d.NewPathStorage(); fixes #61 2015-07-11 01:22:50 +02:00
LICENSE Remove oldies 2011-04-27 10:06:14 +02:00
math.go Merge simpler import path 2015-04-19 17:14:42 +02:00
paint.go Merge simpler import path 2015-04-19 17:14:42 +02:00
path.go add docstrings to path.go 2015-07-08 10:53:35 +02:00
path_adder.go go vet fixes 2015-07-10 15:24:23 +02:00
path_converter.go Preparing for pdf backend 2015-06-27 01:03:41 +02:00
path_storage.go replace gc.Current.Path.Clear() with gc.Current.Path = draw2d.NewPathStorage(); fixes #61 2015-07-11 01:22:50 +02:00
README.md add image to README 2015-07-11 19:31:36 +02:00
rgba_interpolation.go golint fixes 2015-07-10 16:58:36 +02:00
samples_test.go add geometry and gopher2 samples 2015-07-11 18:44:36 +02:00
stack_gc.go replace gc.Current.Path.Clear() with gc.Current.Path = draw2d.NewPathStorage(); fixes #61 2015-07-11 01:22:50 +02:00
stroker.go Merge simpler import path 2015-04-19 17:14:42 +02:00
test golint fixes 2015-07-10 16:58:36 +02:00
test_test.go prepare testing for including samples 2015-07-10 02:07:18 +02:00
transform.go Merge simpler import path 2015-04-19 17:14:42 +02:00
vertex2d.go golint fixes 2015-07-10 16:58:36 +02:00

draw2d

Package draw2d is a pure go 2D vector graphics library with support for multiple output devices such as images (draw2d), pdf documents (draw2dpdf) and opengl (draw2dopengl), which can also be used on the google app engine. It can be used as a pure go Cairo alternative. draw2d is released under the BSD license.

See the documentation for more details.

demonstration of the image backend The image above is generated with the image backend. The pdf which is generated with exactly the same code is here.

Features

Operations in draw2d include stroking and filling polygons, arcs, Bézier curves, drawing images and text rendering with truetype fonts. All drawing operations can be transformed by affine transformations (scale, rotation, translation).

Package draw2d follows the conventions of the HTML Canvas 2D Context for coordinate system, angles, etc...

Installation

Install golang. To install or update the package draw2d on your system, run:

go get -u github.com/llgcode/draw2d

Quick Start

The following Go code generates a simple drawing and saves it to an image file with package draw2d:

// Initialize the graphic context on an RGBA image
dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
gc := draw2d.NewGraphicContext(dest)

// Set some properties
gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
gc.SetLineWidth(5)

// Draw a closed shape
gc.MoveTo(10, 10) // should always be called first for a new path
gc.LineTo(100, 50)
gc.QuadCurveTo(100, 10, 10, 10)
gc.Close()
gc.FillStroke()

// Save to file
draw2d.SaveToPngFile(fn, dest)

The same Go code can also generate a pdf document with package draw2dpdf:

// Initialize the graphic context on an RGBA image
dest := draw2dpdf.NewPdf("L", "mm", "A4")
gc := draw2d.NewGraphicContext(dest)

// Set some properties
gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
gc.SetLineWidth(5)

// Draw a closed shape
gc.MoveTo(10, 10) // should always be called first for a new path
gc.LineTo(100, 50)
gc.QuadCurveTo(100, 10, 10, 10)
gc.Close()
gc.FillStroke()

// Save to file
draw2dpdf.SaveToPdfFile(fn, dest)

There are more examples here: https://github.com/llgcode/draw2d/tree/master/samples

Drawing on opengl is provided by the draw2dgl package.

Testing

The samples are run as tests from the root package folder draw2d by:

go test ./...

Or if you want to run with test coverage:

go test -cover ./... | grep -v "no test"

This will generate output by the different backends in the output folder.

Acknowledgments

Laurent Le Goff wrote this library, inspired by Postscript and HTML5 canvas. He implemented the image and opengl backend with the freetype-go package. Also he created a pure go Postscript interpreter, which can read postscript images and draw to a draw2d graphic context. Stani Michiels implemented the pdf backend with the gofpdf package.

Packages using draw2d

  • ps: Postscript interpreter written in Go
  • gonum/plot: drawing plots in Go
  • go.uik: a concurrent UI kit written in pure go.
  • smartcrop: content aware image cropping
  • karta: drawing Voronoi diagrams
  • chart: basic charts in Go

References