p5

package module
v0.0.0-...-6526b04 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: BSD-3-Clause Imports: 34 Imported by: 0

README

p5

GitHub release go.dev reference CI GoDoc License codecov

p5 is a simple package that provides primitives resembling the ones exposed by the p5/processing library.

License

p5 is released under the BSD-3 license.

Documentation

Documentation for p5 is served by GoDev.

Contributing

Guidelines for contributing to go-p5 the same than for the Go project.

Installation

This project relies on Gio for the graphics parts. As Gio uses system libraries to display graphics, you need to install those for your system/OS for p5 to work properly. See Gio/install for details.

Example

package main

import (
	"image/color"
	"math"

	"github.com/go-p5/p5"
)

func main() {
	p5.Run(setup, draw)
}

func setup() {
	p5.Canvas(400, 400)
	p5.Background(color.Gray{Y: 220})
}

func draw() {
	p5.StrokeWidth(2)
	p5.Fill(color.RGBA{R: 255, A: 208})
	p5.Ellipse(50, 50, 80, 80)

	p5.Fill(color.RGBA{B: 255, A: 208})
	p5.Quad(50, 50, 80, 50, 80, 120, 60, 120)

	p5.Fill(color.RGBA{G: 255, A: 208})
	p5.Rect(200, 200, 50, 100)

	p5.Fill(color.RGBA{G: 255, A: 208})
	p5.Triangle(100, 100, 120, 120, 80, 120)

	p5.TextSize(24)
	p5.Text("Hello, World!", 10, 300)

	p5.Stroke(color.Black)
	p5.StrokeWidth(5)
	p5.Arc(300, 100, 80, 20, 0, 1.5*math.Pi)
}

img-hello

Documentation

Overview

Package p5 provides types and functions to draw geometrical shapes on a canvas, interact with the mouse and keyboard, with the aim to learn programming with a graphics aid.

p5 is inspired by Processing and its p5js port:

A very simple p5 program could look like:

func main() {
    p5.Run(setup, draw)
}

func setup() {
    p5.Canvas(200, 200)
    p5.Background(color.Black)
}

func draw() {
    p5.Fill(color.White)
    p5.Square(10, 10, 50, 50)
}

p5 actually provides two set of APIs:

  • one closely following the p5js API, with global functions and hidden state,
  • another one based on the p5.Proc type that encapsulates state.

Index

Constants

This section is empty.

Variables

View Source
var Event struct {
	Mouse struct {
		Pressed      bool
		PrevPosition struct {
			X float64
			Y float64
		}
		Position struct {
			X float64
			Y float64
		}
		Buttons Buttons
	}
}

Event is the current event pushed from the system.

Functions

func Arc

func Arc(x, y, w, h float64, beg, end float64)

Arc draws an ellipsoidal arc centered at (x,y), with the provided width and height, and a path from the beg to end radians. Positive angles denote a counter-clockwise path.

func Background

func Background(c color.Color)

Background defines the background color for the painting area. The default color is transparent.

func Bezier

func Bezier(x1, y1, x2, y2, x3, y3, x4, y4 float64)

Bezier draws a cubic Bézier curve from (x1,y1) to (x4,y4) and two control points (x2,y2) and (x3,y3).

func Canvas

func Canvas(w, h int)

Canvas defines the dimensions of the painting area, in pixels.

func Circle

func Circle(x, y, d float64)

Circle draws a circle at (x,y) with a diameter d.

func Curve

func Curve(x1, y1, x2, y2, x3, y3, x4, y4 float64)

Curve draws a curved line starting at (x2,y2) and ending at (x3,y3). (x1,y1) and (x4,y4) are the control points.

Curve is an implementation of Catmull-Rom splines.

func CurveTightness

func CurveTightness(v float64)

CurveTightness determines how the curve fits to the Curve vertex points. CurveTightness controls the Catmull-Rom tau tension.

The default value is 0.

func DrawImage

func DrawImage(img image.Image, x, y float64)

DrawImage draws the provided image at (x,y).

func Ellipse

func Ellipse(x, y, w, h float64)

Ellipse draws an ellipse at (x,y) with the provided width and height.

func Fill

func Fill(c color.Color)

Fill sets the color used to fill shapes.

func FrameCount

func FrameCount() uint64

FrameCount returns the number of frames that have been displayed since the program started.

func IsLooping

func IsLooping() bool

IsLooping checks whether p5 is continuously executing the code within Draw.

func Line

func Line(x1, y1, x2, y2 float64)

Line draws a line between (x1,y1) and (x2,y2).

func LoadFonts

func LoadFonts(fnt []text.FontFace)

LoadFonts sets the fonts collection to use for text.

func Loop

func Loop()

By default, p5 continuously executes the code within Draw. Loop starts the draw loop again, if it was stopped previously by calling NoLoop.

func Matrix

func Matrix(a, b, c, d, e, f float64)

Matrix sets the affine matrix transformation.

func NoLoop

func NoLoop()

NoLoop stops p5 from continuously executing the code within Draw.

func PhysCanvas

func PhysCanvas(w, h int, xmin, xmax, ymin, ymax float64)

PhysCanvas sets the dimensions of the painting area, in pixels, and associates physical quantities.

func Pop

func Pop()

Pop restores the previous drawing style settings and transformations.

func Push

func Push()

Push saves the current drawing style settings and transformations.

func Quad

func Quad(x1, y1, x2, y2, x3, y3, x4, y4 float64)

Quad draws a quadrilateral, connecting the 4 points (x1,y1), (x2,y2), (x3,y3) and (x4,y4) together.

func Random

func Random(min, max float64) float64

Random returns a pseudo-random number in [min,max). Random will produce the same sequence of numbers every time the program runs. Use RandomSeed with a seed that changes (like time.Now().UnixNano()) in order to produce different sequence of numbers.

func RandomGaussian

func RandomGaussian(mean, stdDev float64) float64

RandomGaussian returns a random number fitting a Gaussian (normal) distribution.

func RandomSeed

func RandomSeed(seed uint64)

RandomSeed changes the sequence of numbers generated by Random.

func ReadImage

func ReadImage(fname string) (image.Image, error)

ReadImage reads a BMP, JPG, GIF, PNG or TIFF image from the provided path.

func Rect

func Rect(x, y, w, h float64)

Rect draws a rectangle at (x,y) with width w and height h.

func Rotate

func Rotate(angle float64)

Rotate rotates the graphical context by angle radians. Positive angles rotate counter-clockwise.

func Run

func Run(setup, draw Func)

Run executes the user functions setup and draw. Run never exits.

func Scale

func Scale(x, y float64)

Scale rescales the graphical context by x and y.

func Screenshot

func Screenshot(fname string)

Screenshot saves the current canvas to the provided file. Supported file formats are: PNG, JPEG and GIF.

func Shear

func Shear(x, y float64)

Shear shears the graphical context by the given x and y angles in radians.

func Square

func Square(x, y, s float64)

Square draws a square at (x,y) with size s.

func Stroke

func Stroke(c color.Color)

Stroke sets the color of the strokes.

func StrokeWidth

func StrokeWidth(v float64)

StrokeWidth sets the size of the strokes.

func Text

func Text(txt string, x, y float64)

Text draws txt on the screen at (x,y).

func TextFont

func TextFont(fnt text.Font)

TextFont sets the text font.

func TextSize

func TextSize(size float64)

TextSize sets the text size.

func Translate

func Translate(x, y float64)

Translate applies a translation by x and y.

func Triangle

func Triangle(x1, y1, x2, y2, x3, y3 float64)

Triangle draws a triangle, connecting the 3 points (x1,y1), (x2,y2) and (x3,y3) together.

Types

type Buttons

type Buttons uint8

Buttons is a set of mouse buttons.

const (
	ButtonLeft Buttons = 1 << iota
	ButtonRight
	ButtonMiddle
)

func (Buttons) Contain

func (b Buttons) Contain(buttons Buttons) bool

Contain reports whether the set b contains all of the buttons.

type Func

type Func func()

Func is the type of functions users provide to p5.

type Path

type Path struct {
	// contains filtered or unexported fields
}

func (*Path) Close

func (p *Path) Close()

Close closes the current path.

func (*Path) Cube

func (p *Path) Cube(x1, y1, x2, y2, x3, y3 float64)

Cube draws a cubic Bézier curve from the current position to the (x3,y3) point, with the (x1,y1) and (x2,y2) control points.

func (*Path) End

func (p *Path) End()

func (*Path) Quad

func (p *Path) Quad(x1, y1, x2, y2 float64)

Quad draws a quadratic Bézier curve from the current position to the (x2,y2) point, with the (x1,y1) control point.

func (*Path) Vertex

func (p *Path) Vertex(x, y float64)

type Proc

type Proc struct {
	Setup Func
	Draw  Func
	Mouse Func
	// contains filtered or unexported fields
}

Proc is a p5 processor.

Proc runs the bound Setup function once before the event loop. Proc then runs the bound Draw function once per event loop iteration.

func NewProc

func NewProc(w, h int) *Proc

func (*Proc) Arc

func (p *Proc) Arc(x, y, w, h float64, beg, end float64)

Arc draws an ellipsoidal arc centered at (x,y), with the provided width and height, and a path from the beg to end radians. Positive angles denote a counter-clockwise path.

func (*Proc) Background

func (p *Proc) Background(c color.Color)

Background defines the background color for the painting area. The default color is transparent.

func (*Proc) BeginPath

func (p *Proc) BeginPath() *Path

func (*Proc) Bezier

func (p *Proc) Bezier(x1, y1, x2, y2, x3, y3, x4, y4 float64)

Bezier draws a cubic Bézier curve from (x1,y1) to (x4,y4) and two control points (x2,y2) and (x3,y3)

func (*Proc) Canvas

func (p *Proc) Canvas(w, h int)

Canvas defines the dimensions of the painting area, in pixels.

func (*Proc) Circle

func (p *Proc) Circle(x, y, d float64)

Circle draws a circle at (x,y) with a diameter d.

func (*Proc) Curve

func (p *Proc) Curve(x1, y1, x2, y2, x3, y3, x4, y4 float64)

Curve draws a curved line starting at (x2,y2) and ending at (x3,y3). (x1,y1) and (x4,y4) are the control points.

Curve is an implementation of Catmull-Rom splines.

func (*Proc) CurveTightness

func (p *Proc) CurveTightness(v float64)

CurveTightness determines how the curve fits to the Curve vertex points. CurveTightness controls the Catmull-Rom tau tension.

The default value is 0.

func (*Proc) DrawImage

func (p *Proc) DrawImage(img image.Image, x, y float64)

DrawImage draws the provided image at (x,y).

func (*Proc) Ellipse

func (p *Proc) Ellipse(x, y, w, h float64)

Ellipse draws an ellipse at (x,y) with the provided width and height.

func (*Proc) Fill

func (p *Proc) Fill(c color.Color)

Fill sets the color used to fill shapes.

func (*Proc) FrameCount

func (p *Proc) FrameCount() uint64

FrameCount returns the number of frames that have been displayed since the program started.

func (*Proc) IsLooping

func (p *Proc) IsLooping() bool

IsLooping checks if p5 is continuously executing the code within draw() or not.

func (*Proc) Line

func (p *Proc) Line(x1, y1, x2, y2 float64)

Line draws a line between (x1,y1) and (x2,y2).

func (*Proc) LoadFonts

func (p *Proc) LoadFonts(fnt []text.FontFace)

LoadFonts sets the fonts collection to use for text.

func (*Proc) Loop

func (p *Proc) Loop()

By default, p5 continuously executes the code within draw(). Loop starts the draw loop again, if it was stopped previously by calling NoLoop().

func (*Proc) Matrix

func (p *Proc) Matrix(a, b, c, d, e, f float64)

Matrix sets the affine matrix transformation.

func (*Proc) NoLoop

func (p *Proc) NoLoop()

NoLoop stops p5 from continuously executing the code within draw().

func (*Proc) PhysCanvas

func (p *Proc) PhysCanvas(w, h int, xmin, xmax, ymin, ymax float64)

PhysCanvas sets the dimensions of the painting area, in pixels, and associates physical quantities.

func (*Proc) Pop

func (p *Proc) Pop()

Pop restores the previous drawing style settings and transformations.

func (*Proc) Push

func (p *Proc) Push()

Push saves the current drawing style settings and transformations.

func (*Proc) Quad

func (p *Proc) Quad(x1, y1, x2, y2, x3, y3, x4, y4 float64)

Quad draws a quadrilateral, connecting the 4 points (x1,y1), (x2,y2), (x3,y3) and (x4,y4) together.

func (*Proc) Random

func (p *Proc) Random(min, max float64) float64

Random returns a pseudo-random number in [min,max). Random will produce the same sequence of numbers every time the program runs. Use RandomSeed with a seed that changes (like time.Now().UnixNano()) in order to produce different sequences of numbers.

func (*Proc) RandomGaussian

func (p *Proc) RandomGaussian(mean, stdDev float64) float64

RandomGaussian returns a random number following a Gaussian distribution with the provided mean and standard deviation.

func (*Proc) RandomSeed

func (p *Proc) RandomSeed(seed uint64)

RandomSeed changes the sequence of numbers generated by Random.

func (*Proc) ReadImage

func (p *Proc) ReadImage(fname string) (img image.Image, err error)

ReadImage reads a BMP, JPG, GIF, PNG or TIFF image from the provided path.

func (*Proc) Rect

func (p *Proc) Rect(x, y, w, h float64)

Rect draws a rectangle at (x,y) with width w and height h.

func (*Proc) Rotate

func (p *Proc) Rotate(angle float64)

Rotate rotates the graphical context by angle radians. Positive angles rotate counter-clockwise.

func (*Proc) Run

func (p *Proc) Run()

func (*Proc) Scale

func (p *Proc) Scale(x, y float64)

Scale rescales the graphical context by x and y.

func (*Proc) Screenshot

func (p *Proc) Screenshot(fname string) error

Screenshot saves the current canvas to the provided file. Supported file formats are: PNG, JPEG and GIF.

func (*Proc) Shear

func (p *Proc) Shear(x, y float64)

Shear shears the graphical context by the given x and y angles in radians.

func (*Proc) Square

func (p *Proc) Square(x, y, s float64)

Square draws a square at (x,y) with size s.

func (*Proc) Stroke

func (p *Proc) Stroke(c color.Color)

Stroke sets the color of the strokes.

func (*Proc) StrokeWidth

func (p *Proc) StrokeWidth(v float64)

StrokeWidth sets the size of the strokes.

func (*Proc) Text

func (p *Proc) Text(txt string, x, y float64)

Text draws txt on the screen at (x,y).

func (*Proc) TextFont

func (p *Proc) TextFont(fnt text.Font)

func (*Proc) TextSize

func (p *Proc) TextSize(size float64)

TextSize sets the text size.

func (*Proc) Translate

func (p *Proc) Translate(x, y float64)

Translate applies a translation by x and y.

func (*Proc) Triangle

func (p *Proc) Triangle(x1, y1, x2, y2, x3, y3 float64)

Triangle draws a triangle, connecting the 3 points (x1,y1), (x2,y2) and (x3,y3) together.

Directories

Path Synopsis
cmd
example
internal
cmpimg
Package cmpimg compares the raw representation of images taking into account idiosyncracies related to their underlying format (SVG, PDF, PNG, ...).
Package cmpimg compares the raw representation of images taking into account idiosyncracies related to their underlying format (SVG, PDF, PNG, ...).

Jump to

Keyboard shortcuts

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