griddle

package module
v0.0.0-...-778b85b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2016 License: MIT Imports: 4 Imported by: 0

README

griddle

A grid based pattern drawing helper using http://github.com/llgcode/draw2d/draw2dimg

Top Example Image

Usage

Import giddle, image, and draw2dimg

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

Instantiate a griddle canvas by calling New(Width, Height, CanvasWidth, CanvasHeight)

  griddle.New(800, 400, 800, 400)

Create a GraphicContext to draw with

	dest := image.NewRGBA(image.Rect(0, 0, int(mainCanvas.Width), int(mainCanvas.Height)))
	gc := draw2dimg.NewGraphicContext(dest)
	gc.SetDPI(300)

Fill the canvas with canvas.Fill

	mainCanvas.Fill(gc, color.RGBA{0, 0, 0, 255})

Repeat a pattern over a grid with DrawGrid((*draw2dimg.GraphicContext, Element, box, variance)

	element := func(gc *draw2dimg.GraphicContext, coord griddle.Coord, box float64) {
		fillColor := color.RGBA{100, 0, 0, 255}
		gc.SetFillColor(fillColor)
		gc.MoveTo(coord.X, coord.Y+box)
		gc.ArcTo(coord.X, coord.Y, box/4, box/4, 0, -math.Pi*2)
		gc.Close()
		gc.FillStroke()
	}

	mainCanvas.DrawGrid(gc, element, 42.0, 0)

Simple complete Example:

package main

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

func main() {
	mainCanvas := griddle.New(800, 400, 1000, 600)
	dest := image.NewRGBA(image.Rect(0, 0, int(mainCanvas.Width), int(mainCanvas.Height)))
	gc := draw2dimg.NewGraphicContext(dest)
	gc.SetDPI(300)

	mainCanvas.Fill(gc, color.RGBA{0, 0, 0, 255})
	element := func(gc *draw2dimg.GraphicContext, coord griddle.Coord, box float64) {
		fillColor := color.RGBA{100, 0, 0, 255}
		gc.SetFillColor(fillColor)
		gc.MoveTo(coord.X+box, coord.Y+box)
		gc.ArcTo(coord.X, coord.Y, box/4, box/4, 0, -math.Pi*2)
		gc.Close()
		gc.FillStroke()
	}

	mainCanvas.DrawGrid(gc, element, 40.0, 0)

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

Simple Example Image

More Complex Example:

package main

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

func main() {
	mainCanvas := griddle.New(4200, 2520, 5600, 3360)
	dest := image.NewRGBA(image.Rect(0, 0, int(mainCanvas.Width), int(mainCanvas.Height)))
	gc := draw2dimg.NewGraphicContext(dest)
	gc.SetDPI(300)

	mainCanvas.Fill(gc, color.RGBA{0, 0, 0, 255})
	mainCanvas.DrawGrid(gc, elementFunction(mainCanvas), 42.0, 42.0)

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

func elementFunction(c griddle.Canvas) griddle.Element {
	return func(gc *draw2dimg.GraphicContext, coord griddle.Coord, box float64) {
		yFade := uint8(coord.Y / c.CanvasHeight)
		high := 255 - yFade
		strokeColor := color.RGBA{0, 7, 120, 1}
		fillColor := color.RGBA{0, 0, high, 255}
		gc.SetFillColor(fillColor)
		gc.SetStrokeColor(strokeColor)
		gc.SetLineWidth(200.0)
		gc.MoveTo(coord.X, coord.Y+box)
		if coord.Column%2 == 0 {
			gc.LineTo(coord.X, coord.Y)
			gc.LineTo(coord.X+c.Height, coord.Y)
			gc.LineTo(coord.X+c.Width, coord.Y+c.Height)
		} else {
			box := 42.0
			boxMultiplier := 0.5 + coord.Y/c.Height
			gc.ArcTo(coord.X, coord.Y, box*boxMultiplier, box*boxMultiplier, 0, -math.Pi*2)
		}
		gc.Close()
		gc.FillStroke()
	}
}

Complex Example Image

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Canvas

type Canvas struct {
	Width, Height, CanvasWidth, CanvasHeight float64
}

func New

func New(width, height, canvasWidth, canvasHeight float64) Canvas

func (Canvas) DrawGrid

func (c Canvas) DrawGrid(gc *draw2dimg.GraphicContext, element Element, box, variance float64)

func (Canvas) Fill

func (c Canvas) Fill(gc *draw2dimg.GraphicContext, fill color.RGBA)

func (Canvas) GenerateGrid

func (c Canvas) GenerateGrid(offset float64, box float64) []Coord

func (Canvas) HLine

func (c Canvas) HLine(yOffset float64, box float64) []Coord

func (Canvas) VLine

func (c Canvas) VLine(xOffset float64, box float64) []Coord

type Coord

type Coord struct {
	X, Y        float64
	Column, Row int
}

type Element

type Element func(gc *draw2dimg.GraphicContext, c Coord, box float64)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL