Skip to content

Files

Latest commit

eb90594 · Jul 23, 2023

History

History

samples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Aug 14, 2015
Aug 19, 2015
Aug 14, 2015
Jan 7, 2018
Aug 14, 2015
Aug 14, 2015
Aug 14, 2015
Aug 14, 2015
Aug 14, 2015
Aug 14, 2015
Jul 23, 2023
Jul 23, 2023
Mar 6, 2021
Dec 22, 2017

draw2d samples

Various samples for using draw2d

Using the image backend

The following Go code draws the android sample on a png image:

package main

import "log"
import "image"
import "github.com/llgcode/draw2d/draw2dimg"
import "github.com/llgcode/draw2d/samples/android"

func main(){
	// Initialize the graphic context on an RGBA image
	dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
	gc := draw2dimg.NewGraphicContext(dest)
	// Draw Android logo
	fn, err := android.Main(gc, "png")
	if err != nil {
		log.Printf("Drawing %q failed: %v", fn, err)
		return
	}
	// Save to png
	err = draw2dimg.SaveToPngFile(fn, dest)
	if err != nil {
		log.Printf("Saving %q failed: %v", fn, err)
		return
	}

	log.Printf("Succesfully created %q", fn)
}

Using the pdf backend

The following Go code draws the android sample on a pdf document:

import (
	"image"

	"github.com/llgcode/draw2d/draw2dpdf"
	"github.com/llgcode/draw2d/samples/android"
)

function main(){}
	// Initialize the graphic context on a pdf document
	dest := draw2dpdf.NewPdf("L", "mm", "A4")
	gc := draw2dpdf.NewGraphicContext(dest)
	// Draw Android logo
	fn, err := android.Main(gc, "png")
	if err != nil {
		t.Errorf("Drawing %q failed: %v", fn, err)
		return
	}
	// Save to pdf
	err = draw2dpdf.SaveToPdfFile(fn, dest)
	if err != nil {
		t.Errorf("Saving %q failed: %v", fn, err)
	}
}

Testing

These 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"

The following files are responsible to run the image tests:

draw2d/test_test.go
draw2d/samples_test.go

The following files are responsible to run the pdf tests:

draw2d/pdf/test_test.go
draw2dpdf/samples_test.go