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) }
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 ¶
- Variables
- func Arc(x, y, w, h float64, beg, end float64)
- func Background(c color.Color)
- func Bezier(x1, y1, x2, y2, x3, y3, x4, y4 float64)
- func Canvas(w, h int)
- func Circle(x, y, d float64)
- func Curve(x1, y1, x2, y2, x3, y3, x4, y4 float64)
- func CurveTightness(v float64)
- func DrawImage(img image.Image, x, y float64)
- func Ellipse(x, y, w, h float64)
- func Fill(c color.Color)
- func FrameCount() uint64
- func IsLooping() bool
- func Line(x1, y1, x2, y2 float64)
- func LoadFonts(fnt []text.FontFace)
- func Loop()
- func Matrix(a, b, c, d, e, f float64)
- func NoLoop()
- func PhysCanvas(w, h int, xmin, xmax, ymin, ymax float64)
- func Pop()
- func Push()
- func Quad(x1, y1, x2, y2, x3, y3, x4, y4 float64)
- func Random(min, max float64) float64
- func RandomGaussian(mean, stdDev float64) float64
- func RandomSeed(seed uint64)
- func ReadImage(fname string) (image.Image, error)
- func Rect(x, y, w, h float64)
- func Rotate(angle float64)
- func Run(setup, draw Func)
- func Scale(x, y float64)
- func Screenshot(fname string)
- func Shear(x, y float64)
- func Square(x, y, s float64)
- func Stroke(c color.Color)
- func StrokeWidth(v float64)
- func Text(txt string, x, y float64)
- func TextFont(fnt text.Font)
- func TextSize(size float64)
- func Translate(x, y float64)
- func Triangle(x1, y1, x2, y2, x3, y3 float64)
- type Buttons
- type Func
- type Path
- type Proc
- func (p *Proc) Arc(x, y, w, h float64, beg, end float64)
- func (p *Proc) Background(c color.Color)
- func (p *Proc) BeginPath() *Path
- func (p *Proc) Bezier(x1, y1, x2, y2, x3, y3, x4, y4 float64)
- func (p *Proc) Canvas(w, h int)
- func (p *Proc) Circle(x, y, d float64)
- func (p *Proc) Curve(x1, y1, x2, y2, x3, y3, x4, y4 float64)
- func (p *Proc) CurveTightness(v float64)
- func (p *Proc) DrawImage(img image.Image, x, y float64)
- func (p *Proc) Ellipse(x, y, w, h float64)
- func (p *Proc) Fill(c color.Color)
- func (p *Proc) FrameCount() uint64
- func (p *Proc) IsLooping() bool
- func (p *Proc) Line(x1, y1, x2, y2 float64)
- func (p *Proc) LoadFonts(fnt []text.FontFace)
- func (p *Proc) Loop()
- func (p *Proc) Matrix(a, b, c, d, e, f float64)
- func (p *Proc) NoLoop()
- func (p *Proc) PhysCanvas(w, h int, xmin, xmax, ymin, ymax float64)
- func (p *Proc) Pop()
- func (p *Proc) Push()
- func (p *Proc) Quad(x1, y1, x2, y2, x3, y3, x4, y4 float64)
- func (p *Proc) Random(min, max float64) float64
- func (p *Proc) RandomGaussian(mean, stdDev float64) float64
- func (p *Proc) RandomSeed(seed uint64)
- func (p *Proc) ReadImage(fname string) (img image.Image, err error)
- func (p *Proc) Rect(x, y, w, h float64)
- func (p *Proc) Rotate(angle float64)
- func (p *Proc) Run()
- func (p *Proc) Scale(x, y float64)
- func (p *Proc) Screenshot(fname string) error
- func (p *Proc) Shear(x, y float64)
- func (p *Proc) Square(x, y, s float64)
- func (p *Proc) Stroke(c color.Color)
- func (p *Proc) StrokeWidth(v float64)
- func (p *Proc) Text(txt string, x, y float64)
- func (p *Proc) TextFont(fnt text.Font)
- func (p *Proc) TextSize(size float64)
- func (p *Proc) Translate(x, y float64)
- func (p *Proc) Triangle(x1, y1, x2, y2, x3, y3 float64)
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
Background defines the background color for the painting area. The default color is transparent.
func Bezier ¶ added in v0.6.0
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 Curve ¶ added in v0.8.0
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 ¶ added in v0.8.0
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 Ellipse ¶
func Ellipse(x, y, w, h float64)
Ellipse draws an ellipse at (x,y) with the provided width and height.
func FrameCount ¶ added in v0.8.0
func FrameCount() uint64
FrameCount returns the number of frames that have been displayed since the program started.
func IsLooping ¶ added in v0.8.0
func IsLooping() bool
IsLooping checks whether p5 is continuously executing the code within Draw.
func Loop ¶ added in v0.8.0
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 ¶ added in v0.7.0
func Matrix(a, b, c, d, e, f float64)
Matrix sets the affine matrix transformation.
func NoLoop ¶ added in v0.8.0
func NoLoop()
NoLoop stops p5 from continuously executing the code within Draw.
func PhysCanvas ¶ added in v0.8.0
PhysCanvas sets the dimensions of the painting area, in pixels, and associates physical quantities.
func Pop ¶ added in v0.6.0
func Pop()
Pop restores the previous drawing style settings and transformations.
func Push ¶ added in v0.6.0
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 ¶ added in v0.8.0
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 ¶ added in v0.8.0
RandomGaussian returns a random number fitting a Gaussian (normal) distribution.
func RandomSeed ¶ added in v0.8.0
func RandomSeed(seed uint64)
RandomSeed changes the sequence of numbers generated by Random.
func ReadImage ¶ added in v0.9.0
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 ¶ added in v0.6.0
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 ¶ added in v0.6.0
func Scale(x, y float64)
Scale rescales the graphical context by x and y.
func Screenshot ¶ added in v0.3.0
func Screenshot(fname string)
Screenshot saves the current canvas to the provided file. Supported file formats are: PNG, JPEG and GIF.
func Shear ¶ added in v0.7.0
func Shear(x, y float64)
Shear shears the graphical context by the given x and y angles in radians.
func StrokeWidth ¶ added in v0.2.0
func StrokeWidth(v float64)
StrokeWidth sets the size of the strokes.
Types ¶
type Path ¶ added in v0.5.0
type Path struct {
// contains filtered or unexported fields
}
func (*Path) Cube ¶ added in v0.5.0
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.
type Proc ¶
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 (*Proc) Arc ¶ added in v0.2.0
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 ¶ added in v0.2.0
Background defines the background color for the painting area. The default color is transparent.
func (*Proc) Bezier ¶ added in v0.6.0
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 ¶ added in v0.2.0
Canvas defines the dimensions of the painting area, in pixels.
func (*Proc) Curve ¶ added in v0.8.0
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 ¶ added in v0.8.0
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) FrameCount ¶ added in v0.8.0
FrameCount returns the number of frames that have been displayed since the program started.
func (*Proc) IsLooping ¶ added in v0.8.0
IsLooping checks if p5 is continuously executing the code within draw() or not.
func (*Proc) Loop ¶ added in v0.8.0
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) NoLoop ¶ added in v0.8.0
func (p *Proc) NoLoop()
NoLoop stops p5 from continuously executing the code within draw().
func (*Proc) PhysCanvas ¶ added in v0.8.0
PhysCanvas sets the dimensions of the painting area, in pixels, and associates physical quantities.
func (*Proc) Pop ¶ added in v0.6.0
func (p *Proc) Pop()
Pop restores the previous drawing style settings and transformations.
func (*Proc) Push ¶ added in v0.6.0
func (p *Proc) Push()
Push saves the current drawing style settings and transformations.
func (*Proc) Quad ¶
Quad draws a quadrilateral, connecting the 4 points (x1,y1), (x2,y2), (x3,y3) and (x4,y4) together.
func (*Proc) Random ¶ added in v0.8.0
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 ¶ added in v0.8.0
RandomGaussian returns a random number following a Gaussian distribution with the provided mean and standard deviation.
func (*Proc) RandomSeed ¶ added in v0.8.0
RandomSeed changes the sequence of numbers generated by Random.
func (*Proc) ReadImage ¶ added in v0.9.0
ReadImage reads a BMP, JPG, GIF, PNG or TIFF image from the provided path.
func (*Proc) Rotate ¶ added in v0.6.0
Rotate rotates the graphical context by angle radians. Positive angles rotate counter-clockwise.
func (*Proc) Screenshot ¶ added in v0.3.0
Screenshot saves the current canvas to the provided file. Supported file formats are: PNG, JPEG and GIF.
func (*Proc) Shear ¶ added in v0.7.0
Shear shears the graphical context by the given x and y angles in radians.
func (*Proc) StrokeWidth ¶ added in v0.2.0
StrokeWidth sets the size of the strokes.
Source Files ¶
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, ...). |