Draw2d changed to use our own freetype fork
Find a file
Stani 1da2ef6b1e Merge pull request #58 from stanim/raster
redirect results of raster_test to output
2015-07-10 16:21:05 +02:00
curve remove deprecated files 2015-04-22 15:59:32 +02:00
draw2dgl improve documentation for godoc 2015-07-08 00:01:01 +02:00
draw2dpdf add copyright information 2015-07-10 15:11:19 +02:00
output add empty output folder for sample tests 2015-07-10 02:09:19 +02:00
raster redirect results of raster_test to output 2015-07-10 16:18:49 +02:00
resource add resource images for sample tests 2015-07-10 02:10:50 +02:00
samples add license to README 2015-07-10 12:45:52 +02:00
.gitignore add empty output folder for sample tests 2015-07-10 02:09:19 +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 Remove oldies 2011-04-27 10:06:14 +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 improve docstrings of image.go 2015-07-08 00:49:12 +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 Preparing for pdf backend 2015-06-27 01:03:41 +02:00
README.md merge upstream 2015-07-10 12:48:04 +02:00
rgba_interpolation.go Merge simpler import path 2015-04-19 17:14:42 +02:00
samples_test.go prepare testing for including samples 2015-07-10 02:07:18 +02:00
stack_gc.go - fix font size 2015-07-01 01:06:53 +02:00
stroker.go Merge simpler import path 2015-04-19 17:14:42 +02:00
test add test script 2015-07-10 15:11:57 +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 Merge simpler import path 2015-04-19 17:14:42 +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.

See the documentation for more details.

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). draw2d is released under the BSD license.

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