drawing

package module
v0.0.0-...-109dd2e Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2018 License: MIT Imports: 7 Imported by: 0

README

drawing - a 2D drawing package

This is a simple Go package for drawing on a 2D surface. It includes primitives and PNG export, and will also support sprites and related higher-level constructs.

Currently contains:

  • Create surface
  • Clear surface, with transparency
  • Plot point
  • Horizontal and vertical line
  • Non-aliased line with fast integer routines
  • Anti-aliased line and plot
  • Rectangle outline
  • Filled rectangle
  • Circle outline
  • Filled circle
  • Polygon outline
  • Antialiased polygon outline
  • Pre-calculated color gradient
  • Weighted color on a scale
  • Write as a PNG stream
  • Some predefined colors
  • Some functionality shows timings

Instructions

See the HOW-TO.

Example Output

Building the example for all platforms

Substitute the script file according to your own platform:

cd example
./scripts/macos.sh

Creating and running a one-off build of the example

Substitute the file name according to your own platform:

cd example
go build -o ./builds/macos/example && ./builds/macos/example

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Transparent ... Fully transparent.
	Transparent = color.RGBA{R: 0, G: 0, B: 0, A: 0}
	// Black ... Returns opaque Black as RGBA.
	Black = color.RGBA{R: 0, G: 0, B: 0, A: 255}
	// Blue ... Returns opaque Blue as RGBA.
	Blue = color.RGBA{R: 32, G: 0, B: 192, A: 255}
	// Red ... Returns opaque Red as RGBA.
	Red = color.RGBA{R: 176, G: 0, B: 0, A: 255}
	// Magenta ... Returns opaque Magenta as RGBA.
	Magenta = color.RGBA{R: 192, G: 0, B: 176, A: 255}
	// Green ... Returns opaque Green as RGBA.
	Green = color.RGBA{R: 0, G: 176, B: 16, A: 255}
	// Cyan ... Returns opaque Cyan as RGBA.
	Cyan = color.RGBA{R: 0, G: 176, B: 176, A: 255}
	// Yellow ... Returns opaque Yellow as RGBA.
	Yellow = color.RGBA{R: 176, G: 176, B: 0, A: 255}
	// Gray ... Returns opaque Gray as RGBA.
	Gray = color.RGBA{R: 176, G: 176, B: 176, A: 255}
	// LightBlue ... Returns opaque LightBlue as RGBA.
	LightBlue = color.RGBA{R: 64, G: 0, B: 255, A: 255}
	// LightRed ... Returns opaque LightRed as RGBA.
	LightRed = color.RGBA{R: 255, G: 0, B: 0, A: 255}
	// LightMagenta ... Returns opaque LightMagenta as RGBA.
	LightMagenta = color.RGBA{R: 255, G: 0, B: 255, A: 255}
	// LightGreen ... Returns opaque LightGreen as RGBA.
	LightGreen = color.RGBA{R: 0, G: 255, B: 32, A: 255}
	// LightCyan ... Returns opaque LightCyan as RGBA.
	LightCyan = color.RGBA{R: 0, G: 255, B: 255, A: 255}
	// LightYellow ... Returns opaque LightYellow as RGBA.
	LightYellow = color.RGBA{R: 255, G: 255, B: 0, A: 255}
	// White ... Returns opaque White as RGBA.
	White = color.RGBA{R: 255, G: 255, B: 255, A: 255}
)

Functions

func LogDuration

func LogDuration(operation string, taken time.Duration)

LogDuration ... Sample duration logger that uses the standard logger.

func NoDurationLog

func NoDurationLog(operation string, taken time.Duration)

NoDurationLog ... Sample duration logger that does nothing.

Types

type Point

type Point struct {
	X, Y int
}

Point ... An X/Y coordinate.

func NewPoint

func NewPoint(x1, y1 int) Point

NewPoint ... Returns a new point.

type Polygon

type Polygon []Point

Polygon ... Defines an auto-closing shape.

func NewPolygon

func NewPolygon(points ...Point) Polygon

NewPolygon ... Returns a new polygon with the given points.

type Rect

type Rect struct {
	TopLeft, TopRight       Point
	BottomLeft, BottomRight Point
	Width, Height           int
}

Rect ... Defines a rectangular area.

func NewRect

func NewRect(x1, y1, x2, y2 int) Rect

NewRect ... Returns a new rectangle.

type Surface

type Surface struct {
	Image         *image.RGBA
	Bounds        Rect
	Width, Height int
	Background    color.RGBA
	// contains filtered or unexported fields
}

Surface ... Drawing surface with various primitives and helpers.

func NewSurface

func NewSurface(width, height int, logger TimingLogFunc) Surface

NewSurface ... Returns a new drawing surface.

func (*Surface) Circle

func (s *Surface) Circle(centreX, centreY, radius int, c color.RGBA)

Circle ... Draws a fast circle.

func (*Surface) Clear

func (s *Surface) Clear(c color.RGBA)

Clear ... Clears the entire surface.

func (*Surface) DrawPolygon

func (s *Surface) DrawPolygon(polygon Polygon, c color.RGBA)

DrawPolygon ... Draws a polygon, joining the end point back to the start.

func (*Surface) DrawPolygonA

func (s *Surface) DrawPolygonA(polygon Polygon, c color.RGBA)

DrawPolygonA ... Draws an antialiased polygon, joining the end point back to the start.

func (*Surface) DrawRect

func (s *Surface) DrawRect(rect Rect, c color.RGBA)

DrawRect ... Draws a rectangular outline.

func (*Surface) FillCircle

func (s *Surface) FillCircle(centreX, centreY, radius int, c color.RGBA)

FillCircle ... Draws a filled circle.

func (*Surface) FillRect

func (s *Surface) FillRect(rect Rect, c color.RGBA)

FillRect ... Draws a solid rectangle.

func (*Surface) GetColorGradient

func (s *Surface) GetColorGradient(start, end color.RGBA) [101]color.RGBA

GetColorGradient ... Returns colors mapped by integer percentage from start to end.

func (*Surface) GetPoint

func (s *Surface) GetPoint(x, y int) color.RGBA

GetPoint ... Gets a point from the surface.

func (*Surface) GetWeightedColor

func (s *Surface) GetWeightedColor(c1, c2 color.RGBA, weight float64) color.RGBA

GetWeightedColor ... Calculate weighted (0..100) color moving from c1 to c2.

func (*Surface) Hline

func (s *Surface) Hline(start, end Point, c color.RGBA)

Hline ... Draws a horizontal line, ignoring any Y deviation.

func (*Surface) Line

func (s *Surface) Line(start, end Point, c color.RGBA)

Line ... Draws a line (using Bresenham, no anti-aliasing).

func (*Surface) LineA

func (s *Surface) LineA(start, end Point, c color.RGBA)

LineA ... Draws an arbitrary naive antialiased line (Xiaolin Wu's algorithm).

func (*Surface) Plot

func (s *Surface) Plot(x, y int, c color.RGBA)

Plot ... Places a point on the surface.

func (*Surface) PlotA

func (s *Surface) PlotA(x1, y1, x2, y2 float64, c color.RGBA, weight float64)

PlotA ... Plots, with an antialiased weighted (0..1) second pixel.

func (*Surface) PlotPoint

func (s *Surface) PlotPoint(point Point, c color.RGBA)

PlotPoint ... Places a point on the surface.

func (*Surface) Vline

func (s *Surface) Vline(start, end Point, c color.RGBA)

Vline ... Draws a vertical line, ignoring any X deviation.

func (*Surface) WritePNG

func (s *Surface) WritePNG(w io.Writer) error

WritePNG ... Writes a PNG stream to the output (eg *io.File).

type TimingLogFunc

type TimingLogFunc func(string, time.Duration)

TimingLogFunc ... Logs timings. Sample LogDuration and NoDurationLog implementations are provided.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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