Documentation
¶
Overview ¶
Package p5go provides a bridge between Go and p5.js.
This package allows you to create interactive graphics and animations using p5.js from Go code that compiles to WebAssembly. The package is organized into the following components:
- Constants and type definitions (constants.go) - Canvas management (canvas.go) - Lifecycle functions like Setup and Draw (lifecycle.go) - Event handlers for mouse and keyboard (events.go) - Drawing primitives and shapes (drawing.go) - Color manipulation (color.go) - Transformations (transform.go) - Mathematical functions (math.go) - Text rendering (text.go) - Pixel and image operations (pixels.go) - Input handling (input.go) - Environment and frame control (environment.go) - Helper types and structures (types.go)
Example usage:
err := p5go.Run("#app",
p5go.Setup(func(c *p5go.Canvas) {
c.CreateCanvas(400, 400)
}),
p5go.Draw(func(c *p5go.Canvas) {
c.Background(220)
c.Ellipse(200, 200, 50, 50)
}),
)
Index ¶
- Constants
- func Run(query string, fs ...Func) error
- type AngleMode
- type BlendMode
- type Canvas
- func (c *Canvas) Abs(n float64) float64
- func (c *Canvas) Acos(value float64) float64
- func (c *Canvas) Alpha(color js.Value) float64
- func (c *Canvas) AngleMode(mode AngleMode)
- func (c *Canvas) ApplyMatrix(a, b, c1, d, e, f float64)
- func (c *Canvas) Arc(x, y, w, h, start, stop float64)
- func (c *Canvas) Asin(value float64) float64
- func (c *Canvas) Atan(value float64) float64
- func (c *Canvas) Atan2(y, x float64) float64
- func (c *Canvas) Background(args ...any)
- func (c *Canvas) BeginContour()
- func (c *Canvas) BeginShape(kind ...ShapeType)
- func (c *Canvas) Bezier(x1, y1, x2, y2, x3, y3, x4, y4 float64)
- func (c *Canvas) BezierVertex(cx1, cy1, cx2, cy2, x, y float64)
- func (c *Canvas) Blend(sx, sy, sw, sh, dx, dy, dw, dh float64, blendMode BlendMode)
- func (c *Canvas) BlendMode(mode BlendMode)
- func (c *Canvas) Blue(color js.Value) float64
- func (c *Canvas) Box(opts ...any)
- func (c *Canvas) Brightness(color js.Value) float64
- func (c *Canvas) Ceil(n float64) float64
- func (c *Canvas) Circle(x, y, d float64)
- func (c *Canvas) Clear()
- func (c *Canvas) Close()
- func (c *Canvas) Color(args ...any) js.Value
- func (c *Canvas) ColorMode(mode ColorMode, max ...float64)
- func (c *Canvas) Constrain(n, low, high float64) float64
- func (c *Canvas) Copy(srcImage js.Value, sx, sy, sw, sh, dx, dy, dw, dh float64)
- func (c *Canvas) Cos(value float64) float64
- func (c *Canvas) CreateCanvas(w, h int, opts ...RendererMode)
- func (c *Canvas) CreateCapture(kind CaptureKind) js.Value
- func (c *Canvas) CreateGraphics(w, h float64, renderer ...any) *Canvas
- func (c *Canvas) Cursor(style CursorStyle)
- func (c *Canvas) Curve(x1, y1, x2, y2, x3, y3, x4, y4 float64)
- func (c *Canvas) CurveVertex(x, y float64)
- func (c *Canvas) Degrees(value float64) float64
- func (c *Canvas) Dist(x1, y1, x2, y2 float64) float64
- func (c *Canvas) DrawCircle(circle Circle)
- func (c *Canvas) DrawLine(line Line)
- func (c *Canvas) DrawRect(r Rectangle)
- func (c *Canvas) DrawTriangle(t Triangle)
- func (c *Canvas) Ellipse(x, y, w, h float64)
- func (c *Canvas) EllipseMode(mode DrawingMode)
- func (c *Canvas) EndContour()
- func (c *Canvas) EndShape(mode ...ShapeType)
- func (c *Canvas) Erase(opt ...any)
- func (c *Canvas) Exp(n float64) float64
- func (c *Canvas) Fill(args ...any)
- func (c *Canvas) FillColor(color Color)
- func (c *Canvas) FillRGB(r, g, b float64)
- func (c *Canvas) FillRGBA(r, g, b, a float64)
- func (c *Canvas) Filter(filterType FilterType, value ...float64)
- func (c *Canvas) Floor(n float64) float64
- func (c *Canvas) FrameCount() int
- func (c *Canvas) FrameRate(fps float64)
- func (c *Canvas) Get(x, y float64) js.Value
- func (c *Canvas) GetFrameRate() float64
- func (c *Canvas) Green(color js.Value) float64
- func (c *Canvas) Height() float64
- func (c *Canvas) Hide()
- func (c *Canvas) Hue(color js.Value) float64
- func (c *Canvas) Image(img any, opts ...any)
- func (c *Canvas) IsLooping() bool
- func (c *Canvas) Key() string
- func (c *Canvas) KeyCode() int
- func (c *Canvas) KeyIsPressed() bool
- func (c *Canvas) Lerp(start, stop, amt float64) float64
- func (c *Canvas) LerpColor(c1 js.Value, c2 js.Value, amt float64) js.Value
- func (c *Canvas) Line(x1, y1, x2, y2 float64)
- func (c *Canvas) LoadImage(path string) js.Value
- func (c *Canvas) LoadPixels()
- func (c *Canvas) Log(n float64) float64
- func (c *Canvas) Loop()
- func (c *Canvas) Mag(x, y float64) float64
- func (c *Canvas) Map(value, start1, stop1, start2, stop2 float64) float64
- func (c *Canvas) Mask(img js.Value)
- func (c *Canvas) Max(args ...float64) float64
- func (c *Canvas) Min(args ...float64) float64
- func (c *Canvas) MouseButton() string
- func (c *Canvas) MouseIsPressed() bool
- func (c *Canvas) MouseX() float64
- func (c *Canvas) MouseY() float64
- func (c *Canvas) MovedX() float64
- func (c *Canvas) MovedY() float64
- func (c *Canvas) NoCursor()
- func (c *Canvas) NoErase()
- func (c *Canvas) NoFill()
- func (c *Canvas) NoLoop()
- func (c *Canvas) NoSmooth()
- func (c *Canvas) NoStroke()
- func (c *Canvas) Norm(value, start, stop float64) float64
- func (c *Canvas) OrbitControl(opts ...any)
- func (c *Canvas) PMouseX() float64
- func (c *Canvas) PMouseY() float64
- func (c *Canvas) Point(x, y float64, z ...float64)
- func (c *Canvas) Pop()
- func (c *Canvas) Pow(n, e float64) float64
- func (c *Canvas) Push()
- func (c *Canvas) QuadraticVertex(cx, cy, x, y float64)
- func (c *Canvas) Radians(value float64) float64
- func (c *Canvas) Random(min, max float64) float64
- func (c *Canvas) Rect(x, y, w, h float64)
- func (c *Canvas) RectMode(mode DrawingMode)
- func (c *Canvas) Red(color js.Value) float64
- func (c *Canvas) Redraw()
- func (c *Canvas) ResetMatrix()
- func (c *Canvas) Rotate(angle float64)
- func (c *Canvas) RotateX(angle float64)
- func (c *Canvas) RotateY(angle float64)
- func (c *Canvas) RotateZ(angle float64)
- func (c *Canvas) Round(n float64) float64
- func (c *Canvas) Saturation(color js.Value) float64
- func (c *Canvas) Save(filename string)
- func (c *Canvas) SaveCanvas(filename, extension string)
- func (c *Canvas) SaveFrames(filename string, extension string, duration float64, fps float64)
- func (c *Canvas) SaveGif(name string, second float64)
- func (c *Canvas) Scale(s float64)
- func (c *Canvas) Set(x, y float64, color js.Value)
- func (c *Canvas) ShearX(angle float64)
- func (c *Canvas) ShearY(angle float64)
- func (c *Canvas) Sin(value float64) float64
- func (c *Canvas) Size(width, height float64)
- func (c *Canvas) Smooth()
- func (c *Canvas) Sq(n float64) float64
- func (c *Canvas) Sqrt(n float64) float64
- func (c *Canvas) Square(x, y, s float64)
- func (c *Canvas) Stroke(args ...any)
- func (c *Canvas) StrokeCap(cap ShapeType)
- func (c *Canvas) StrokeColor(color Color)
- func (c *Canvas) StrokeJoin(join ShapeType)
- func (c *Canvas) StrokeRGB(r, g, b float64)
- func (c *Canvas) StrokeRGBA(r, g, b, a float64)
- func (c *Canvas) StrokeWeight(weight float64)
- func (c *Canvas) Tan(value float64) float64
- func (c *Canvas) Text(str string, x, y float64)
- func (c *Canvas) TextAlign(align DrawingMode)
- func (c *Canvas) TextAscent() float64
- func (c *Canvas) TextDescent() float64
- func (c *Canvas) TextFont(font string, size float64)
- func (c *Canvas) TextLeading(leading float64)
- func (c *Canvas) TextSize(size float64)
- func (c *Canvas) TextStyle(style TextStyle)
- func (c *Canvas) TextWidth(text string) float64
- func (c *Canvas) TextWrap(w DrawingMode)
- func (c *Canvas) Translate(x, y float64, z ...float64)
- func (c *Canvas) Triangle(x1, y1, x2, y2, x3, y3 float64)
- func (c *Canvas) UpdatePixels()
- func (c *Canvas) Validate() error
- func (c *Canvas) Vertex(x, y float64, z ...float64)
- func (c *Canvas) Width() float64
- func (c *Canvas) WindowHeight() float64
- func (c *Canvas) WindowWidth() float64
- type CaptureKind
- type Circle
- type Color
- type ColorMode
- type CursorStyle
- type DoubleClickedEvent
- type DoubleClickedHandler
- type DrawingMode
- type FilterType
- type Func
- func DoubleClicked(handler DoubleClickedHandler) Func
- func Draw(handler func(c *Canvas)) Func
- func KeyPressed(handler func(c *Canvas)) Func
- func KeyReleased(handler func(c *Canvas)) Func
- func KeyTyped(handler func(c *Canvas)) Func
- func MouseClicked(handler MouseClickedHandler) Func
- func MouseDragged(handler MouseDraggedHandler) Func
- func MouseMoved(handler func(c *Canvas)) Func
- func MousePressed(handler MousePressedHandler) Func
- func MouseReleased(handler MouseReleasedHandler) Func
- func MouseWheel(handler func(c *Canvas)) Func
- func Preload(handler func(c *Canvas)) Func
- func Setup(handler func(c *Canvas)) Func
- type Line
- type MouseClickedEvent
- type MouseClickedHandler
- type MouseDraggedEvent
- type MouseDraggedHandler
- type MouseEvent
- type MousePressedHandler
- type MouseReleasedEvent
- type MouseReleasedHandler
- type Orientation
- type Rectangle
- type RendererMode
- type ShapeType
- type TextStyle
- type Triangle
- type Vector
- type WebGLMode
Constants ¶
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BlendMode ¶
type BlendMode string
BlendMode represents the blending mode
const ( // Blend modes BLEND BlendMode = "source-over" REMOVE BlendMode = "destination-out" ADD BlendMode = "lighter" DARKEST BlendMode = "darken" LIGHTEST BlendMode = "lighten" DIFFERENCE BlendMode = "difference" SUBTRACT BlendMode = "subtract" EXCLUSION BlendMode = "exclusion" MULTIPLY BlendMode = "multiply" SCREEN BlendMode = "screen" REPLACE BlendMode = "copy" OVERLAY BlendMode = "overlay" HARD_LIGHT BlendMode = "hard-light" SOFT_LIGHT BlendMode = "soft-light" DODGE BlendMode = "color-dodge" BURN BlendMode = "color-burn" )
type Canvas ¶
type Canvas struct {
// contains filtered or unexported fields
}
Canvas represents a p5.js canvas.
func (*Canvas) ApplyMatrix ¶
ApplyMatrix applies a transformation matrix to the canvas.
func (*Canvas) Background ¶
Background sets the background color of the canvas.
func (*Canvas) BeginContour ¶
func (c *Canvas) BeginContour()
BeginContour begins recording vertices for a contour.
func (*Canvas) BeginShape ¶
BeginShape begins recording vertices for a shape.
func (*Canvas) BezierVertex ¶
BezierVertex adds a bezier vertex to the current shape.
func (*Canvas) Brightness ¶
Brightness returns the brightness value of a color.
func (*Canvas) CreateCanvas ¶
func (c *Canvas) CreateCanvas(w, h int, opts ...RendererMode)
CreateCanvas creates a new canvas with the specified width and height.
func (*Canvas) CreateCapture ¶
func (c *Canvas) CreateCapture(kind CaptureKind) js.Value
CreateCapture creates a capture object.
func (*Canvas) CreateGraphics ¶
CreateGraphics creates and returns a new graphics buffer.
func (*Canvas) CurveVertex ¶
CurveVertex adds a curve vertex to the current shape.
func (*Canvas) DrawCircle ¶
DrawCircle draws a circle using a Circle struct
func (*Canvas) DrawTriangle ¶
DrawTriangle draws a triangle using a Triangle struct
func (*Canvas) EllipseMode ¶
func (c *Canvas) EllipseMode(mode DrawingMode)
EllipseMode sets the location from which ellipses are drawn.
func (*Canvas) EndContour ¶
func (c *Canvas) EndContour()
EndContour ends recording vertices for a contour.
func (*Canvas) Filter ¶
func (c *Canvas) Filter(filterType FilterType, value ...float64)
Filter applies a filter to the canvas.
func (*Canvas) FrameCount ¶
FrameCount returns the number of frames that have been displayed.
func (*Canvas) GetFrameRate ¶
GetFrameRate returns the current frame rate.
func (*Canvas) KeyIsPressed ¶
KeyIsPressed returns true if a key is currently pressed.
func (*Canvas) LoadPixels ¶
func (c *Canvas) LoadPixels()
LoadPixels loads the pixel data for the canvas into the pixels[] array.
func (*Canvas) MouseButton ¶
MouseButton returns the current mouse button being pressed.
func (*Canvas) MouseIsPressed ¶
MouseIsPressed returns true if the mouse is currently pressed.
func (*Canvas) NoSmooth ¶
func (c *Canvas) NoSmooth()
NoSmooth draws all geometry with jagged (aliased) edges.
func (*Canvas) NoStroke ¶
func (c *Canvas) NoStroke()
NoStroke disables drawing the stroke for shapes.
func (*Canvas) OrbitControl ¶
OrbitControl represents a control for orbiting around an object
func (*Canvas) Pop ¶
func (c *Canvas) Pop()
Pop restores the previous drawing style settings and transformations.
func (*Canvas) Push ¶
func (c *Canvas) Push()
Push saves the current drawing style settings and transformations.
func (*Canvas) QuadraticVertex ¶
QuadraticVertex draws a quadratic vertex on the canvas.
func (*Canvas) RectMode ¶
func (c *Canvas) RectMode(mode DrawingMode)
RectMode sets the location from which rectangles are drawn.
func (*Canvas) ResetMatrix ¶
func (c *Canvas) ResetMatrix()
ResetMatrix resets the transformation matrix.
func (*Canvas) Saturation ¶
Saturation returns the saturation value of a color.
func (*Canvas) SaveCanvas ¶
SaveCanvas saves the canvas as an image file.
func (*Canvas) SaveFrames ¶
SaveFrames saves a sequence of frames as image files.
func (*Canvas) Smooth ¶
func (c *Canvas) Smooth()
Smooth draws all geometry with smooth (anti-aliased) edges.
func (*Canvas) StrokeColor ¶
StrokeColor sets the stroke color using a Color struct
func (*Canvas) StrokeJoin ¶
StrokeJoin sets the style of the joints which connect line segments.
func (*Canvas) StrokeRGBA ¶
StrokeRGBA sets the stroke color using RGBA values
func (*Canvas) StrokeWeight ¶
StrokeWeight sets the weight of the stroke.
func (*Canvas) TextAlign ¶
func (c *Canvas) TextAlign(align DrawingMode)
TextAlign sets the alignment for text.
func (*Canvas) TextAscent ¶
TextAscent returns the ascent of the current font.
func (*Canvas) TextDescent ¶
TextDescent returns the descent of the current font.
func (*Canvas) TextLeading ¶
TextLeading sets the leading for text.
func (*Canvas) TextWrap ¶
func (c *Canvas) TextWrap(w DrawingMode)
TextWrap sets the wrap mode for text.
func (*Canvas) UpdatePixels ¶
func (c *Canvas) UpdatePixels()
UpdatePixels updates the canvas with the data in the pixels[] array.
func (*Canvas) WindowHeight ¶
WindowHeight returns the height of the window.
func (*Canvas) WindowWidth ¶
WindowWidth returns the width of the window.
type CaptureKind ¶
type CaptureKind string
CaptureKind is a type that represents the kind of capture.
const ( CaptureKindVIDEO CaptureKind = "VIDEO" CaptureKindIMAGE CaptureKind = "IMAGE" )
type CursorStyle ¶
type CursorStyle string
CursorStyle represents the cursor style
const ( // Environment ARROW CursorStyle = "default" CROSS CursorStyle = "crosshair" HAND CursorStyle = "pointer" MOVE CursorStyle = "move" TEXT CursorStyle = "text" WAIT CursorStyle = "wait" )
type DoubleClickedEvent ¶
DoubleClickedEvent represents a double clicked event
type DoubleClickedHandler ¶
type DoubleClickedHandler func(c *Canvas, e DoubleClickedEvent)
DoubleClickedHandler is a type for double clicked event handlers
type DrawingMode ¶
type DrawingMode string
DrawingMode represents the drawing mode
const ( // Drawing modes CORNER DrawingMode = "corner" CORNERS DrawingMode = "corners" RADIUS DrawingMode = "radius" RIGHT DrawingMode = "right" LEFT DrawingMode = "left" CENTER DrawingMode = "center" TOP DrawingMode = "top" BOTTOM DrawingMode = "bottom" BASELINE DrawingMode = "alphabetic" )
type FilterType ¶
type FilterType string
FilterType represents the type of filter
const ( // Image filters THRESHOLD FilterType = "threshold" GRAY FilterType = "gray" OPAQUE FilterType = "opaque" INVERT FilterType = "invert" POSTERIZE FilterType = "posterize" DILATE FilterType = "dilate" ERODE FilterType = "erode" BLUR FilterType = "blur" )
type Func ¶
type Func func(c *Canvas)
Func is a type that represents a function that takes a Canvas pointer as an argument.
func DoubleClicked ¶
func DoubleClicked(handler DoubleClickedHandler) Func
DoubleClicked sets the doubleClicked handler with a DoubleClickedEvent
func KeyPressed ¶
KeyPressed sets the keyPressed handler for the canvas.
func KeyReleased ¶
KeyReleased sets the keyReleased handler for the canvas.
func MouseClicked ¶
func MouseClicked(handler MouseClickedHandler) Func
MouseClicked sets the mouseClicked handler with a MouseClickedEvent
func MouseDragged ¶
func MouseDragged(handler MouseDraggedHandler) Func
MouseDragged sets the mouseDragged handler with a MouseDraggedEvent
func MouseMoved ¶
MouseMoved sets the mouseMoved handler for the canvas.
func MousePressed ¶
func MousePressed(handler MousePressedHandler) Func
MousePressed sets the mousePressed handler with a MouseEvent
func MouseReleased ¶
func MouseReleased(handler MouseReleasedHandler) Func
MouseReleased sets the mouseReleased handler with a MouseReleasedEvent
func MouseWheel ¶
MouseWheel sets the mouseWheel handler for the canvas.
type MouseClickedEvent ¶
MouseClickedEvent represents a mouse clicked event
type MouseClickedHandler ¶
type MouseClickedHandler func(c *Canvas, e MouseClickedEvent)
MouseClickedHandler is a type for mouse clicked event handlers
type MouseDraggedEvent ¶
MouseDraggedEvent represents a mouse dragged event
type MouseDraggedHandler ¶
type MouseDraggedHandler func(c *Canvas, e MouseDraggedEvent)
MouseDraggedHandler is a type for mouse dragged event handlers
type MouseEvent ¶
MouseEvent represents a mouse event
type MousePressedHandler ¶
type MousePressedHandler func(c *Canvas, e MouseEvent)
MousePressedHandler is a type for mouse pressed event handlers
type MouseReleasedEvent ¶
MouseReleasedEvent represents a mouse released event
type MouseReleasedHandler ¶
type MouseReleasedHandler func(c *Canvas, e MouseReleasedEvent)
MouseReleasedHandler is a type for mouse released event handlers
type Orientation ¶
type Orientation string
Orientation represents the device orientation
const ( // Device orientation LANDSCAPE Orientation = "landscape" PORTRAIT Orientation = "portrait" )
type RendererMode ¶
type RendererMode string
RendererMode represents the rendering mode for the canvas
const ( // Renderer modes P2D RendererMode = "p2d" WEBGL RendererMode = "webgl" )
type ShapeType ¶
type ShapeType string
ShapeType represents the type of shape P5.jsの beginShape で使う型 https://p5js.org/reference/#/p5/beginShape "POINTS", "LINES", "TRIANGLES", "TRIANGLE_FAN", "TRIANGLE_STRIP", "QUADS", "QUAD_STRIP", "TESS"
const ( POINTS ShapeType = "POINTS" LINES ShapeType = "LINES" LINE_STRIP ShapeType = "LINE_STRIP" LINE_LOOP ShapeType = "LINE_LOOP" TRIANGLES ShapeType = "TRIANGLES" TRIANGLE_FAN ShapeType = "TRIANGLE_FAN" TRIANGLE_STRIP ShapeType = "TRIANGLE_STRIP" QUADS ShapeType = "QUADS" QUAD_STRIP ShapeType = "QUAD_STRIP" TESS ShapeType = "TESS" CLOSE ShapeType = "CLOSE" OPEN ShapeType = "OPEN" CHORD ShapeType = "CHORD" PIE ShapeType = "PIE" PROJECT ShapeType = "PROJECT" SQUARE ShapeType = "SQUARE" ROUND ShapeType = "ROUND" BEVEL ShapeType = "BEVEL" MITER ShapeType = "MITER" )