Documentation ¶
Overview ¶
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 (draw2dgl), 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.
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 http://www.w3.org/TR/2dcontext for coordinate system, angles, etc...
Installation ¶
To install or update the package draw2d on your system, run:
go get -u github.com/sksullivan/draw2d
Quick Start ¶
Package draw2d itself provides a graphic context that can draw vector graphics and text on an image canvas. The following Go code generates a simple drawing and saves it to an image file:
// Initialize the graphic context on an RGBA image dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0)) gc := draw2dimg.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("hello.png", dest)
There are more examples here: https://github.com/sksullivan/draw2d/tree/master/samples
Drawing on pdf documents is provided by the draw2dpdf package. Drawing on opengl is provided by the draw2dgl package. See subdirectories at the bottom of this page.
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 (https://github.com/sksullivan/ps). Stani Michiels implemented the pdf backend with the gofpdf package.
Packages using draw2d ¶
- https://github.com/sksullivan/ps: Postscript interpreter written in Go
- https://github.com/gonum/plot: drawing plots in Go
- https://github.com/muesli/smartcrop: content aware image cropping
- https://github.com/peterhellberg/karta: drawing Voronoi diagrams
- https://github.com/vdobler/chart: basic charts in Go
Index ¶
- Constants
- func FontFileName(fontData FontData) string
- func GetFont(fontData FontData) *truetype.Font
- func GetFontFolder() string
- func RegisterFont(fontData FontData, font *truetype.Font)
- func SetFontFolder(folder string)
- func SetFontNamer(fn FontFileNamer)
- type FillRule
- type FontData
- type FontFamily
- type FontFileNamer
- type FontStyle
- type GraphicContext
- type Halign
- type ImageScaling
- type LineCap
- type LineJoin
- type Matrix
- func (tr *Matrix) Compose(trToCompose Matrix)
- func (tr Matrix) Copy() Matrix
- func (tr Matrix) Determinant() float64
- func (tr1 Matrix) Equals(tr2 Matrix) bool
- func (tr Matrix) GetScale() float64
- func (tr Matrix) GetScaling() (x, y float64)
- func (tr Matrix) GetTranslation() (x, y float64)
- func (tr *Matrix) Inverse()
- func (tr Matrix) InverseTransform(points []float64)
- func (tr Matrix) InverseTransformPoint(x, y float64) (xres, yres float64)
- func (tr Matrix) IsIdentity() bool
- func (tr Matrix) IsTranslation() bool
- func (tr *Matrix) Rotate(angle float64)
- func (tr *Matrix) Scale(sx, sy float64)
- func (tr Matrix) Transform(points []float64)
- func (tr Matrix) TransformPoint(x, y float64) (xres, yres float64)
- func (tr Matrix) TransformRectangle(x0, y0, x2, y2 float64) (nx0, ny0, nx2, ny2 float64)
- func (tr *Matrix) Translate(tx, ty float64)
- func (tr Matrix) VectorTransform(points []float64)
- type Path
- func (p *Path) ArcTo(cx, cy, rx, ry, startAngle, angle float64)
- func (p *Path) Clear()
- func (p *Path) Close()
- func (p *Path) Copy() (dest *Path)
- func (p *Path) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64)
- func (p *Path) IsEmpty() bool
- func (p *Path) LastPoint() (x, y float64)
- func (p *Path) LineTo(x, y float64)
- func (p *Path) MoveTo(x, y float64)
- func (p *Path) QuadCurveTo(cx, cy, x, y float64)
- func (p *Path) String() string
- type PathBuilder
- type PathCmp
- type ScalingPolicy
- type SolidFillStyle
- type StrokeStyle
- type TextStyle
- type Valign
Constants ¶
const ( // HalignLeft Horizontally align to left HalignLeft = iota // HalignCenter Horizontally align to center HalignCenter // HalignRight Horizontally align to right HalignRight )
Variables ¶
This section is empty.
Functions ¶
func FontFileName ¶
func GetFontFolder ¶
func GetFontFolder() string
func RegisterFont ¶
func SetFontFolder ¶
func SetFontFolder(folder string)
func SetFontNamer ¶
func SetFontNamer(fn FontFileNamer)
Types ¶
type FillRule ¶
type FillRule int
FillRule defines the type for fill rules
const ( // FillRuleEvenOdd determines the "insideness" of a point in the shape // by drawing a ray from that point to infinity in any direction // and counting the number of path segments from the given shape that the ray crosses. // If this number is odd, the point is inside; if even, the point is outside. FillRuleEvenOdd FillRule = iota // FillRuleWinding determines the "insideness" of a point in the shape // by drawing a ray from that point to infinity in any direction // and then examining the places where a segment of the shape crosses the ray. // Starting with a count of zero, add one each time a path segment crosses // the ray from left to right and subtract one each time // a path segment crosses the ray from right to left. After counting the crossings, // if the result is zero then the point is outside the path. Otherwise, it is inside. FillRuleWinding )
type FontData ¶
type FontData struct { Name string Family FontFamily Style FontStyle }
type FontFamily ¶
type FontFamily byte
const ( FontFamilySans FontFamily = iota FontFamilySerif FontFamilyMono )
type FontFileNamer ¶
type GraphicContext ¶
type GraphicContext interface { PathBuilder // BeginPath creates a new path BeginPath() // GetMatrixTransform returns the current transformation matrix GetMatrixTransform() Matrix // SetMatrixTransform sets the current transformation matrix SetMatrixTransform(tr Matrix) // ComposeMatrixTransform composes the current transformation matrix with tr ComposeMatrixTransform(tr Matrix) // Rotate applies a rotation to the current transformation matrix. angle is in radian. Rotate(angle float64) // Translate applies a translation to the current transformation matrix. Translate(tx, ty float64) // Scale applies a scale to the current transformation matrix. Scale(sx, sy float64) // SetStrokeColor sets the current stroke color SetStrokeColor(c color.Color) // SetStrokeColor sets the current fill color SetFillColor(c color.Color) // SetFillRule sets the current fill rule SetFillRule(f FillRule) // SetLineWidth sets the current line width SetLineWidth(lineWidth float64) // SetLineCap sets the current line cap SetLineCap(cap LineCap) // SetLineJoin sets the current line join SetLineJoin(join LineJoin) // SetLineJoin sets the current dash SetLineDash(dash []float64, dashOffset float64) // SetFontSize SetFontSize(fontSize float64) GetFontSize() float64 SetFontData(fontData FontData) GetFontData() FontData DrawImage(image image.Image) Save() Restore() Clear() ClearRect(x1, y1, x2, y2 int) SetDPI(dpi int) GetDPI() int GetStringBounds(s string) (left, top, right, bottom float64) CreateStringPath(text string, x, y float64) (cursor float64) FillString(text string) (cursor float64) FillStringAt(text string, x, y float64) (cursor float64) StrokeString(text string) (cursor float64) StrokeStringAt(text string, x, y float64) (cursor float64) Stroke(paths ...*Path) Fill(paths ...*Path) FillStroke(paths ...*Path) }
GraphicContext describes the interface for the various backends (images, pdf, opengl, ...)
type ImageScaling ¶
type ImageScaling struct { // Horizontal Alignment of the image Halign Halign // Vertical Alignment of the image Valign Valign // Width Height used by scaling policy Width, Height float64 // ScalingPolicy defines the scaling policy to applied to the image ScalingPolicy ScalingPolicy }
ImageScaling style attributes used to display the image
type Matrix ¶
type Matrix [6]float64
Matrix represents an affine transformation
func NewIdentityMatrix ¶
func NewIdentityMatrix() Matrix
NewIdentityMatrix creates an identity transformation matrix.
func NewMatrixFromRects ¶
NewMatrixFromRects creates a transformation matrix, combining a scale and a translation, that transform rectangle1 into rectangle2.
func NewRotationMatrix ¶
NewRotationMatrix creates a rotation transformation matrix. angle is in radian
func NewScaleMatrix ¶
NewScaleMatrix creates a transformation matrix with a sx, sy scale factor
func NewTranslationMatrix ¶
NewTranslationMatrix creates a transformation matrix with a translation tx and ty translation parameter
func (Matrix) Determinant ¶
Determinant compute the determinant of the matrix
func (Matrix) Equals ¶
Equals tests if a two transformation are equal. A tolerance is applied when comparing matrix elements.
func (Matrix) InverseTransform ¶
InverseTransform applies the transformation inverse matrix to the rectangle represented by the min and the max point of the rectangle
func (Matrix) InverseTransformPoint ¶
InverseTransformPoint applies the transformation inverse matrix to point. It returns the point the transformed point.
func (Matrix) IsIdentity ¶
IsIdentity tests if a transformation is the identity transformation. A tolerance is applied when comparing matrix elements.
func (Matrix) IsTranslation ¶
IsTranslation tests if a transformation is is a pure translation. A tolerance is applied when comparing matrix elements.
func (Matrix) Transform ¶
Transform applies the transformation matrix to points. It modify the points passed in parameter.
func (Matrix) TransformPoint ¶
TransformPoint applies the transformation matrix to point. It returns the point the transformed point.
func (Matrix) TransformRectangle ¶
Transform applies the transformation matrix to the rectangle represented by the min and the max point of the rectangle
func (Matrix) VectorTransform ¶
VectorTransform applies the transformation matrix to points without using the translation parameter of the affine matrix. It modify the points passed in parameter.
type Path ¶
type Path struct { // Components is a slice of PathCmp in a Path and mark the role of each points in the Path Components []PathCmp // Points are combined with Components to have a specific role in the path Points []float64 // contains filtered or unexported fields }
Path stores points
func (*Path) CubicCurveTo ¶
CubicCurveTo adds a cubic bezier curve to the current path
func (*Path) QuadCurveTo ¶
QuadCurveTo adds a quadratic bezier curve to the current path
type PathBuilder ¶
type PathBuilder interface { // LastPoint returns the current point of the current sub path LastPoint() (x, y float64) // MoveTo creates a new subpath that start at the specified point MoveTo(x, y float64) // LineTo adds a line to the current subpath LineTo(x, y float64) // QuadCurveTo adds a quadratic Bézier curve to the current subpath QuadCurveTo(cx, cy, x, y float64) // CubicCurveTo adds a cubic Bézier curve to the current subpath CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) // ArcTo adds an arc to the current subpath ArcTo(cx, cy, rx, ry, startAngle, angle float64) // Close creates a line from the current point to the last MoveTo // point (if not the same) and mark the path as closed so the // first and last lines join nicely. Close() }
PathBuilder describes the interface for path drawing.
type PathCmp ¶
type PathCmp int
PathCmp represents component of a path
const ( // MoveToCmp is a MoveTo component in a Path MoveToCmp PathCmp = iota // LineToCmp is a LineTo component in a Path LineToCmp // QuadCurveToCmp is a QuadCurveTo component in a Path QuadCurveToCmp // CubicCurveToCmp is a CubicCurveTo component in a Path CubicCurveToCmp // ArcToCmp is a ArcTo component in a Path ArcToCmp // CloseCmp is a ArcTo component in a Path CloseCmp )
type ScalingPolicy ¶
type ScalingPolicy int
ScalingPolicy is a constant to define how to scale an image
const ( // ScalingNone no scaling applied ScalingNone ScalingPolicy = iota // ScalingStretch the image is stretched so that its width and height are exactly the given width and height ScalingStretch // ScalingWidth the image is scaled so that its width is exactly the given width ScalingWidth // ScalingHeight the image is scaled so that its height is exactly the given height ScalingHeight // ScalingFit the image is scaled to the largest scale that allow the image to fit within a rectangle width x height ScalingFit // ScalingSameArea the image is scaled so that its area is exactly the area of the given rectangle width x height ScalingSameArea // ScalingFill the image is scaled to the smallest scale that allow the image to fully cover a rectangle width x height ScalingFill )
type SolidFillStyle ¶
type SolidFillStyle struct { // Color defines the line color Color color.Color // FillRule defines the file rule to used FillRule FillRule }
SolidFillStyle define style attributes for a solid fill style
type StrokeStyle ¶
type StrokeStyle struct { // Color defines the color of stroke Color color.Color // Line width Width float64 // Line cap style rounded, butt or square LineCap LineCap // Line join style bevel, round or miter LineJoin LineJoin // offset of the first dash DashOffset float64 // array represented dash length pair values are plain dash and impair are space between dash // if empty display plain line Dash []float64 }
StrokeStyle keeps stroke style attributes that is used by the Stroke method of a Drawer
Directories ¶
Path | Synopsis |
---|---|
Package draw2dgl provides a graphic context that can draw vector graphics and text on OpenGL.
|
Package draw2dgl provides a graphic context that can draw vector graphics and text on OpenGL. |
Package draw2dkit provides helpers to draw common figures using a Path
|
Package draw2dkit provides helpers to draw common figures using a Path |
Package draw2dpdf provides a graphic context that can draw vector graphics and text on pdf file with the gofpdf package.
|
Package draw2dpdf provides a graphic context that can draw vector graphics and text on pdf file with the gofpdf package. |
Package samples provides examples which can be used with different backends.
|
Package samples provides examples which can be used with different backends. |
android
Package android draws an android avatar.
|
Package android draws an android avatar. |
frameimage
Package frameimage centers a png image and rotates it.
|
Package frameimage centers a png image and rotates it. |
geometry
Package geometry draws some geometric tests.
|
Package geometry draws some geometric tests. |
gopher
Package gopher draws a gopher avatar based on a svg of: https://github.com/golang-samples/gopher-vector/
|
Package gopher draws a gopher avatar based on a svg of: https://github.com/golang-samples/gopher-vector/ |
gopher2
Package gopher2 draws a gopher avatar based on a svg of: https://github.com/golang-samples/gopher-vector/
|
Package gopher2 draws a gopher avatar based on a svg of: https://github.com/golang-samples/gopher-vector/ |
helloworld
Package helloworld displays multiple "Hello World" (one rotated) in a rounded rectangle.
|
Package helloworld displays multiple "Hello World" (one rotated) in a rounded rectangle. |
helloworldgl
Open an OpenGl window and display a rectangle using a OpenGl GraphicContext
|
Open an OpenGl window and display a rectangle using a OpenGl GraphicContext |
line
Package line draws vertically spaced lines.
|
Package line draws vertically spaced lines. |
linecapjoin
Package linecapjoin demonstrates the different line caps and joins.
|
Package linecapjoin demonstrates the different line caps and joins. |
postscript
Package postscript reads the tiger.ps file and draws it to a backend.
|
Package postscript reads the tiger.ps file and draws it to a backend. |
postscriptgl
Open a OpenGL window and display a tiger interpreting a postscript file
|
Open a OpenGL window and display a tiger interpreting a postscript file |