canvas

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, setup, draw func(c *Canvas), opts ...CallbackOption)

func Start

func Start(ctx context.Context, done chan struct{}, setup, draw func(c *Canvas), opts ...CallbackOption) (*sync.WaitGroup, error)

Types

type CallbackOption

type CallbackOption func(*callbacks)

func WithOnKey

func WithOnKey(onKey func(c *Canvas, e KeyEvent)) CallbackOption

func WithOnMouseClick

func WithOnMouseClick(onMouseClick func(c *Canvas, e MouseEvent)) CallbackOption

func WithOnMouseMove

func WithOnMouseMove(onMouseMove func(c *Canvas, e MouseEvent)) CallbackOption

func WithOnMouseRelease

func WithOnMouseRelease(onMouseRelease func(c *Canvas, e MouseEvent)) CallbackOption

func WithOnMouseWheel

func WithOnMouseWheel(onMouseWheel func(c *Canvas, e MouseEvent)) CallbackOption

type Canvas

type Canvas struct {
	Width, Height  int
	MouseX, MouseY int
	Framecount     int

	IsLooping bool
	// contains filtered or unexported fields
}

Canvas represents a drawable area where shapes, text, and effects can be rendered.

func (*Canvas) Background

func (c *Canvas) Background(text, fg, bg string)

Background sets the background character and colors for the entire canvas.

func (*Canvas) BackgroundBg

func (c *Canvas) BackgroundBg(bg string)

BackgroundBg sets the background color used by the background fill.

func (*Canvas) BackgroundFg

func (c *Canvas) BackgroundFg(fg string)

BackgroundFg sets the foreground (text) color used by the background fill.

func (*Canvas) BackgroundText

func (c *Canvas) BackgroundText(text string)

BackgroundText sets the character used for the background fill.

func (*Canvas) Bezier

func (c *Canvas) Bezier(x1, y1, x2, y2, x3, y3, x4, y4 int)

Bezier draws a Bézier curve using four control points.

func (*Canvas) CellModeCustom

func (c *Canvas) CellModeCustom(char string)

CellModeCustom sets a specific character used for cell spacing between elements.

func (*Canvas) CellModeDefault

func (c *Canvas) CellModeDefault()

CellModeDefault disables cell mode.

func (*Canvas) CellModeDouble

func (c *Canvas) CellModeDouble()

CellModeDouble makes every cell duplicated.

func (*Canvas) CellPadding

func (c *Canvas) CellPadding(char string)

DEPRECATED: Use CellModeCustom() instead.

func (*Canvas) CellPaddingDouble

func (c *Canvas) CellPaddingDouble(char string)

DEPRECATED: Use CellModeDouble() instead.

func (*Canvas) Circle

func (c *Canvas) Circle(xCenter, yCenter, r int)

Circle draws a circle centered at (x, y) with the given radius.

func (*Canvas) Clear

func (c *Canvas) Clear()

Clear clears the canvas contents.

func (*Canvas) ColorHSL

func (c *Canvas) ColorHSL(h, s, l int) string

ColorHSL converts a hue (0-360), saturation (0-100) and lightness (0-100) to its hex representation.

func (*Canvas) ColorHSV

func (c *Canvas) ColorHSV(h, s, v int) string

ColorHSV converts a hue (0-360), saturation (0-100) and value (0-100) to its hex representation.

func (*Canvas) ColorRGB

func (c *Canvas) ColorRGB(r, g, b int) string

func (*Canvas) Debug

func (c *Canvas) Debug(messages ...any)

func (*Canvas) DisableRendering

func (c *Canvas) DisableRendering()

DisableRendering disables all rendering updates. Used when an error is rendered.

func (*Canvas) Dist

func (c *Canvas) Dist(x1, y1, x2, y2 int) float64

Dist returns the Euclidean distance between two points.

func (*Canvas) Ellipse

func (c *Canvas) Ellipse(xCenter, yCenter, rx, ry int)

Ellipse draws an ellipse centered at (x, y) with radiuses rx and ry.

func (*Canvas) Exit

func (c *Canvas) Exit()

Exit ends the program execution.

func (*Canvas) Fill

func (c *Canvas) Fill(text, fg, bg string)

Fill sets the fill character and its foreground and background colors.

func (*Canvas) FillBg

func (c *Canvas) FillBg(bg string)

FillBg sets the background color used for fill operations.

func (*Canvas) FillFg

func (c *Canvas) FillFg(fg string)

FillFg sets the foreground color used for fill operations.

func (*Canvas) FillText

func (c *Canvas) FillText(text string)

FillText sets the character used for fill operations.

func (*Canvas) Fps

func (c *Canvas) Fps(fps int)

Fps sets the rendering framerate in frames per second. Values outside [minFPS, maxFPS] are clamped.

func (*Canvas) Get

func (c *Canvas) Get(x, y, w, h int) Image

func (*Canvas) Image

func (c *Canvas) Image(img Image, x, y, w, h int)

func (*Canvas) Line

func (c *Canvas) Line(x1, y1, x2, y2 int)

Line draws a straight line between two points.

func (*Canvas) LoadImage

func (c *Canvas) LoadImage(path string) Image

func (*Canvas) Loop

func (c *Canvas) Loop()

Loop enables continuous redrawing of the canvas.

func (*Canvas) LoopAngle

func (c *Canvas) LoopAngle(duration int) float64

LoopAngle returns the angular progress (in radians, range [0, 2π]) through a looping cycle of given duration in seconds.

func (*Canvas) Map

func (c *Canvas) Map(value, inputStart, inputEnd, outputStart, outputEnd float64) float64

Map linearly maps a value from one range to another.

func (*Canvas) NoCellPadding

func (c *Canvas) NoCellPadding()

DEPRECATED: Use CellModeDefault() instead.

func (*Canvas) NoFill

func (c *Canvas) NoFill()

NoFill disables fill for subsequent shapes.

func (*Canvas) NoLoop

func (c *Canvas) NoLoop()

NoLoop disables automatic canvas redrawing.

func (*Canvas) NoStroke

func (c *Canvas) NoStroke()

NoStroke disables stroke for subsequent shapes.

func (*Canvas) Noise1D

func (c *Canvas) Noise1D(x float64) float64

Noise1D generates 1D Perlin noise (range [0, 1]) for a given input.

func (*Canvas) Noise2D

func (c *Canvas) Noise2D(x, y float64) float64

Noise2D generates 2D Perlin noise (range [0, 1]) for a given (x, y) coordinate.

func (*Canvas) NoiseLoop

func (c *Canvas) NoiseLoop(angle, radius float64) float64

NoiseLoop returns a noise value by sampling the noise on a circular path of the given radius. This is useful for creating cyclic animations or evolving patterns that repeat perfectly after one full loop.

Parameters:

  • angle: the loop angle in radians, from 0 to 2π.
  • radius: the radius of the circular path in noise space. the higher the radius, the more instable it gets.

Returns:

  • A float64 noise value in the range [0, 1].

func (*Canvas) NoiseLoop1D

func (c *Canvas) NoiseLoop1D(angle, radius float64, x int) float64

NoiseLoop1D returns a 1D noise value that loops as the angle progresses. It samples a 2D noise space using the given radius and combines it with a horizontal offset.

Parameters:

  • angle: the loop angle in radians, typically from 0 to 2π.
  • radius: the radius of the circular path in noise space.
  • x: horizontal position to offset the noise sampling.

Returns:

  • A float64 noise value in the range [0, 1].

func (*Canvas) NoiseLoop2D

func (c *Canvas) NoiseLoop2D(angle, radius float64, x, y int) float64

NoiseLoop2D returns a 2D noise value that loops as the angle progresses. It samples a circular path in 2D noise space, offset by the (x, y) coordinates.

Parameters:

  • angle: the loop angle in radians, typically from 0 to 2π.
  • radius: the radius of the circular path in noise space.
  • x, y: coordinates used to offset the sampled position in the noise field.

Returns:

  • A float64 noise value in the range [0, 1].

func (*Canvas) NoiseSeed

func (c *Canvas) NoiseSeed(seed int64)

NoiseSeed sets the random seed for noise generation.

func (*Canvas) Point

func (c *Canvas) Point(x, y int)

Point draws a single point at the given position.

func (*Canvas) Pop

func (c *Canvas) Pop()

Pop restores the most recently pushed drawing state.

func (*Canvas) Push

func (c *Canvas) Push()

Push saves the current drawing state onto the stack. Every Push must be matched by a Pop; the stack is cleared at the end of each frame.

func (*Canvas) Quad

func (c *Canvas) Quad(x1, y1, x2, y2, x3, y3, x4, y4 int)

Quad draws a quadrilateral defined by four points.

func (*Canvas) Random

func (c *Canvas) Random(minimum, maximum int) float64

Random returns a random float between minimum and maximum.

func (*Canvas) RandomSeed

func (c *Canvas) RandomSeed(seed int64)

RandomSeed sets the random number generator seed.

func (*Canvas) Rect

func (c *Canvas) Rect(x, y, w, h int)

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

func (*Canvas) Redraw

func (c *Canvas) Redraw()

Redraw triggers a manual rendering pass.

func (*Canvas) Rotate

func (c *Canvas) Rotate(angle float64)

Rotate rotates the drawing context by the given angle in radians.

func (*Canvas) SaveCanvasToGIF

func (c *Canvas) SaveCanvasToGIF(filename string, duration int)

SaveCanvasToGIF exports the canvas to an animated gif for a given duration (in seconds).

func (*Canvas) SaveCanvasToMP4

func (c *Canvas) SaveCanvasToMP4(filename string, duration int)

SaveCanvasToMP4 exports the canvas to a mp4 (h264) video for a given duration (in seconds). Depends on ffmpeg.

func (*Canvas) SaveCanvasToPNG

func (c *Canvas) SaveCanvasToPNG(filename string)

SaveCanvasToPNG exports the canvas to a png image file.

func (*Canvas) SaveCanvasToText added in v0.13.0

func (c *Canvas) SaveCanvasToText(filename string)

SaveCanvasToText exports the canvas to a plain text file, dropping all color information.

func (*Canvas) SavedCanvasFont

func (c *Canvas) SavedCanvasFont(filename string)

SavedCanvasFont sets a custom font (tff) file used for rendering text characters in exported images generated via SaveCanvasTo...().

func (*Canvas) SavedCanvasFontSize

func (c *Canvas) SavedCanvasFontSize(size int)

SavedCanvasFontSize sets the font size used for rendering text characters in exported images generated via SaveCanvas().

func (*Canvas) Scale

func (c *Canvas) Scale(scale float64)

Scale scales the drawing context by the given factor.

func (*Canvas) Set

func (c *Canvas) Set(x, y int, cells writable)

func (*Canvas) Size

func (c *Canvas) Size(w, h int)

Size sets the dimensions of the canvas.

func (*Canvas) Square

func (c *Canvas) Square(x, y, size int)

Square draws a square with the given top-left corner and side length.

func (*Canvas) Stroke

func (c *Canvas) Stroke(text, fg, bg string)

Stroke sets the stroke (outline) character and colors.

func (*Canvas) StrokeBg

func (c *Canvas) StrokeBg(bg string)

StrokeBg sets the background color for strokes.

func (*Canvas) StrokeFg

func (c *Canvas) StrokeFg(fg string)

StrokeFg sets the foreground color for strokes.

func (*Canvas) StrokeText

func (c *Canvas) StrokeText(text string)

StrokeText sets the character used for strokes.

func (*Canvas) Text

func (c *Canvas) Text(text string, x, y int)

Text renders a string at the given canvas coordinates.

func (*Canvas) Translate

func (c *Canvas) Translate(x, y int)

Translate offsets the drawing context by (x, y).

func (*Canvas) Triangle

func (c *Canvas) Triangle(x1, y1, x2, y2, x3, y3 int)

Triangle draws a triangle using three vertex points.

type Cell

type Cell struct {
	Char       string
	Foreground string
	Background string
}

type Image

type Image interface {
	Cell(x, y int) Cell
	// contains filtered or unexported methods
}

type KeyEvent

type KeyEvent struct {
	Key  string
	Code int
}

type MouseEvent

type MouseEvent struct {
	X      int
	Y      int
	Button string
}

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

Jump to

Keyboard shortcuts

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