Convert Figma logo to code with AI

llgcode logodraw2d

2D rendering for different output (raster, pdf, svg)

1,085
105
1,085
39

Top Related Projects

4,378

Go Graphics - 2D rendering in Go with a simple API.

2,138

Go Language Library for SVG generation

Quick Overview

Draw2d is a 2D vector graphics library for Go, providing an API for drawing shapes, text, and images. It supports various output formats including raster images, SVG, and PDF, making it versatile for different applications ranging from data visualization to document generation.

Pros

  • Supports multiple output formats (PNG, JPG, SVG, PDF)
  • Provides a simple and intuitive API for drawing operations
  • Offers a wide range of drawing primitives and transformations
  • Actively maintained with regular updates and improvements

Cons

  • Limited documentation and examples compared to some other graphics libraries
  • Performance may not be optimal for complex or large-scale drawings
  • Lacks some advanced features found in more specialized libraries
  • May have a steeper learning curve for beginners due to its flexibility

Code Examples

  1. Drawing a simple shape:
package main

import (
    "github.com/llgcode/draw2d/draw2dimg"
    "image"
    "image/color"
)

func main() {
    dest := image.NewRGBA(image.Rect(0, 0, 200, 200))
    gc := draw2dimg.NewGraphicContext(dest)

    gc.SetFillColor(color.RGBA{255, 0, 0, 255})
    gc.MoveTo(10, 10)
    gc.LineTo(100, 10)
    gc.LineTo(100, 100)
    gc.LineTo(10, 100)
    gc.Close()
    gc.Fill()

    draw2dimg.SaveToPngFile("rectangle.png", dest)
}
  1. Drawing text:
package main

import (
    "github.com/llgcode/draw2d/draw2dimg"
    "image"
    "image/color"
)

func main() {
    dest := image.NewRGBA(image.Rect(0, 0, 200, 100))
    gc := draw2dimg.NewGraphicContext(dest)

    gc.SetFontSize(20)
    gc.SetFillColor(color.RGBA{0, 0, 255, 255})
    gc.FillStringAt("Hello, Draw2D!", 10, 50)

    draw2dimg.SaveToPngFile("text.png", dest)
}
  1. Drawing a curve:
package main

import (
    "github.com/llgcode/draw2d/draw2dimg"
    "image"
    "image/color"
)

func main() {
    dest := image.NewRGBA(image.Rect(0, 0, 200, 200))
    gc := draw2dimg.NewGraphicContext(dest)

    gc.SetStrokeColor(color.RGBA{0, 255, 0, 255})
    gc.SetLineWidth(3)
    gc.MoveTo(10, 10)
    gc.CubicCurveTo(100, 10, 100, 100, 10, 100)
    gc.Stroke()

    draw2dimg.SaveToPngFile("curve.png", dest)
}

Getting Started

To start using Draw2d, first install it using Go modules:

go get -u github.com/llgcode/draw2d

Then, import the necessary packages in your Go code:

import (
    "github.com/llgcode/draw2d/draw2dimg"
    "image"
    "image/color"
)

Now you can create a new image, initialize a graphic context, and start drawing using the Draw2d API. Remember to save your output to a file when you're done.

Competitor Comparisons

4,378

Go Graphics - 2D rendering in Go with a simple API.

Pros of gg

  • Simpler API with fewer concepts to learn
  • Better performance, especially for complex drawings
  • More active development and maintenance

Cons of gg

  • Less comprehensive feature set compared to draw2d
  • Fewer built-in shapes and primitives
  • Limited support for image manipulation and processing

Code Comparison

draw2d:

gc := draw2d.NewGraphicContext(dest)
gc.SetStrokeColor(color.Black)
gc.SetLineWidth(1)
gc.MoveTo(10, 10)
gc.LineTo(100, 100)
gc.Stroke()

gg:

dc := gg.NewContext(width, height)
dc.SetColor(color.Black)
dc.SetLineWidth(1)
dc.DrawLine(10, 10, 100, 100)
dc.Stroke()

Both libraries provide similar functionality for basic drawing operations, but gg's API is generally more concise and straightforward. draw2d offers more fine-grained control over the drawing process, while gg focuses on simplicity and ease of use.

gg is better suited for projects requiring high performance and simple drawing operations, while draw2d may be preferable for more complex graphics tasks or when compatibility with the standard image/draw package is needed.

2,138

Go Language Library for SVG generation

Pros of svgo

  • Simpler API, focused specifically on SVG generation
  • Lightweight with minimal dependencies
  • Better suited for quick SVG creation tasks

Cons of svgo

  • Limited to SVG output only
  • Fewer advanced drawing features compared to draw2d
  • Less actively maintained (last commit over 2 years ago)

Code Comparison

svgo:

canvas := svg.New(os.Stdout)
canvas.Start(500, 500)
canvas.Circle(250, 250, 125, "fill:none;stroke:black")
canvas.Text(250, 250, "Hello, SVG", "text-anchor:middle")
canvas.End()

draw2d:

gc := draw2dimg.NewGraphicContext(dest)
gc.SetStrokeColor(color.Black)
gc.SetLineWidth(1)
gc.MoveTo(10, 10)
gc.LineTo(100, 50)
gc.Stroke()

Key Differences

  • svgo is focused on SVG generation, while draw2d supports multiple output formats
  • draw2d offers more advanced drawing capabilities and transformations
  • svgo has a simpler API, making it easier to use for basic SVG tasks
  • draw2d is more actively maintained and has a larger community

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

draw2d

Coverage GoDoc BuyMeaBeer

Package draw2d is a go 2D vector graphics library with support for multiple outputs such as images (draw2d), pdf documents (draw2dpdf), opengl (draw2dgl) and svg (draw2dsvg). There's also a Postscript reader that uses draw2d. draw2d is released under the BSD license. See the documentation for more details.

geometrypostscript

Click on an image above to get the pdf, generated with exactly the same draw2d code. The first image is the output of samples/geometry. The second image is the result of samples/postcript, which demonstrates that draw2d can draw postscript files into images or pdf documents with the ps package.

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:

Stable release

go get -u gopkg.in/llgcode/draw2d.v1

or Current release

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:

package main

import (
	"github.com/llgcode/draw2d/draw2dimg"
	"image"
	"image/color"
)

func main() {
	// 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.BeginPath() // Initialize a new path
	gc.MoveTo(10, 10) // Move to a position to start the new path
	gc.LineTo(100, 50)
	gc.QuadCurveTo(100, 10, 10, 10)
	gc.Close()
	gc.FillStroke()

	// Save to file
	draw2dimg.SaveToPngFile("hello.png", dest)
}

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

package main

import (
	"github.com/llgcode/draw2d/draw2dpdf"
	"image/color"
)

func main() {
	// Initialize the graphic context on an RGBA image
	dest := draw2dpdf.NewPdf("L", "mm", "A4")
	gc := draw2dpdf.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("hello.pdf", 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
  • go.uik: a concurrent UI kit written in pure go.
  • smartcrop: content aware image cropping
  • karta: drawing Voronoi diagrams
  • chart: basic charts in Go
  • hilbert: package for drawing Hilbert curves

References