Skip to content

Getting Started

Taco de Wolff edited this page Apr 13, 2021 · 12 revisions

Requirements

If you need advanced script support for text rendering (such as Arabic, Hebrew, Asian languages, ...) you will also need:

Create project

Create a new project that will be using the canvas library:

mkdir project
cd project
export GO111MODULE=on
go mod init main
go get -u github.com/tdewolff/canvas

Basic template

Create main.go with the following contents:

package main

import (
    "github.com/tdewolff/canvas"
    "github.com/tdewolff/canvas/rasterizer"
)

func main() {
    // Create new canvas of dimension 100x100 mm
    c := canvas.New(100, 100)

    // Create a canvas context used to keep drawing state
    ctx := canvas.NewContext(c)

    // Create a triangle path from an SVG path and draw it to the canvas
    triangle, err := canvas.ParseSVG("L60 0L30 60z")
    if err != nil {
        panic(err)
    }
    ctx.DrawPath(20, 20, triangle)

    // Rasterize the canvas and write to a PNG file with 3.2 dots-per-mm (320x320 px)
    c.WriteFile("getting-started.png", rasterizer.PNGWriter(3.2))
}

Run

We run the basic template which will output a file getting-started.png.

go run main.go

Triangle

Clone this wiki locally