canvas

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package canvas provides a 2D pixel buffer and drawing operations for HTML5 canvas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Canvas

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

------------------------------------------------------------------------------------------------ Canvas represents the in-memory pixel buffer and its associated HTML canvas.

func NewCanvas

func NewCanvas(width, height int, pixels []byte, parentID string) *Canvas

------------------------------------------------------------------------------------------------ NewCanvas creates a canvas in-memory and associates it with a new canvas element appended to the DOM element identified by parentID.

func NewOffscreenCanvas added in v1.0.2

func NewOffscreenCanvas(width, height int, pixels []byte) *Canvas

------------------------------------------------------------------------------------------------ NewOffscreenCanvas creates a Canvas backed only by a Go pixel buffer, with no DOM or JS objects attached. Useful for benchmarking and unit testing. Calling Render() on an offscreen canvas is a no-op.

func (*Canvas) BorderCircle

func (c *Canvas) BorderCircle(midX, midY, radius, borderWidth int32)

------------------------------------------------------------------------------------------------ BorderCircle draws a ring (annulus) at (midX, midY) with the specified radius and border width using the active color.

func (*Canvas) CanvasHandle

func (c *Canvas) CanvasHandle() dom.Handle

------------------------------------------------------------------------------------------------ CanvasHandle returns the DOM handle for the underlying HTML canvas element.

func (*Canvas) Circle

func (c *Canvas) Circle(midX, midY, radius int32)

------------------------------------------------------------------------------------------------ Circle draws an outline circle at (midX, midY) with the specified radius using the active color. Uses the Bresenham midpoint circle algorithm — integer arithmetic only, no trigonometry.

func (*Canvas) ClearScreen

func (c *Canvas) ClearScreen(col colour.Colour)

------------------------------------------------------------------------------------------------ ClearScreen fills the entire canvas with the specified color.

func (*Canvas) ColourBorderCircle

func (c *Canvas) ColourBorderCircle(midX, midY, radius, borderWidth int32, col colour.Colour)

------------------------------------------------------------------------------------------------ ColourBorderCircle draws a ring (annulus) at (midX, midY) with the specified radius, border width, and color.

func (*Canvas) ColourCircle

func (c *Canvas) ColourCircle(midX, midY, radius int32, col colour.Colour)

------------------------------------------------------------------------------------------------ ColourCircle draws an outline circle at (midX, midY) with the specified radius and color.

func (*Canvas) ColourFilledCircle

func (c *Canvas) ColourFilledCircle(midX, midY, radius int32, col colour.Colour)

------------------------------------------------------------------------------------------------ ColourFilledCircle draws a filled circle at (midX, midY) with the specified radius and color.

func (*Canvas) ColourFilledRectangle

func (c *Canvas) ColourFilledRectangle(xStart, yStart, width, height int32, col colour.Colour)

------------------------------------------------------------------------------------------------ ColourFilledRectangle draws a filled rectangle starting at (xStart, yStart) with the specified width, height, and color.

func (*Canvas) ColourLine

func (c *Canvas) ColourLine(x1, y1, x2, y2 int32, col colour.Colour)

------------------------------------------------------------------------------------------------ ColourLine draws a line from (x1, y1) to (x2, y2) with the specified color.

func (*Canvas) ColourLinePoint

func (c *Canvas) ColourLinePoint(p1, p2 Point, col colour.Colour)

------------------------------------------------------------------------------------------------ ColourLinePoint draws a line between two Points with the specified color.

func (*Canvas) ColourPutPixel

func (c *Canvas) ColourPutPixel(x, y int32, col colour.Colour)

------------------------------------------------------------------------------------------------ ColourPutPixel draws a pixel at (x, y) with the specified color.

func (*Canvas) ColourRectangle

func (c *Canvas) ColourRectangle(xStart, yStart, width, height, thickness int32, col colour.Colour)

------------------------------------------------------------------------------------------------ ColourRectangle draws an outline rectangle starting at (xStart, yStart) with the specified width, height, thickness, and color.

func (*Canvas) FilledCircle

func (c *Canvas) FilledCircle(midX, midY, radius int32)

------------------------------------------------------------------------------------------------ FilledCircle draws a filled circle at (midX, midY) with the specified radius using the active color.

func (*Canvas) FilledRectangle

func (c *Canvas) FilledRectangle(xStart, yStart, width, height int32)

------------------------------------------------------------------------------------------------ FilledRectangle draws a filled rectangle starting at (xStart, yStart) with the specified width and height using the active color.

func (*Canvas) GetColour

func (c *Canvas) GetColour() colour.Colour

------------------------------------------------------------------------------------------------ GetColour retrieves the active drawing color.

func (*Canvas) GetContext2D

func (c *Canvas) GetContext2D() dom.Context2D

------------------------------------------------------------------------------------------------ GetContext2D returns a Context2D wrapper for the HTML canvas context. Panics if called on a canvas created with NewOffscreenCanvas.

func (*Canvas) GetPixel

func (c *Canvas) GetPixel(x, y int32) (colour.Colour, bool)

------------------------------------------------------------------------------------------------ GetPixel retrieves the color of the pixel at (x, y). It returns false if the coordinates are out of bounds.

func (*Canvas) Height

func (c *Canvas) Height() int

------------------------------------------------------------------------------------------------ Height returns the canvas height in pixels.

func (*Canvas) Line

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

------------------------------------------------------------------------------------------------ Line draws a 1-pixel line from (x1, y1) to (x2, y2) using Bresenham's algorithm and the active color.

func (*Canvas) LinePoint

func (c *Canvas) LinePoint(p1, p2 Point)

------------------------------------------------------------------------------------------------ LinePoint draws a line between two Point structures using the active color.

func (*Canvas) Pixels

func (c *Canvas) Pixels() []byte

------------------------------------------------------------------------------------------------ Pixels returns the underlying pixel buffer slice. The slice is shared with the Canvas — writes to it are reflected immediately.

func (*Canvas) PutPixel

func (c *Canvas) PutPixel(x, y int32)

------------------------------------------------------------------------------------------------ PutPixel draws a pixel at (x, y) using the active drawing color.

func (*Canvas) Rectangle

func (c *Canvas) Rectangle(xStart, yStart, width, height, thickness int32)

------------------------------------------------------------------------------------------------ Rectangle draws an outline rectangle starting at (xStart, yStart) with the specified width, height, and border thickness using the active color.

func (*Canvas) Render

func (c *Canvas) Render()

------------------------------------------------------------------------------------------------ Render blits the Go-side pixel buffer into JavaScript and updates the canvas. It is a no-op if the canvas was created with NewOffscreenCanvas.

func (*Canvas) SetColour

func (c *Canvas) SetColour(col colour.Colour)

------------------------------------------------------------------------------------------------ SetColour sets the active drawing color.

func (*Canvas) Triangle

func (c *Canvas) Triangle(p1, p2, p3 Point)

------------------------------------------------------------------------------------------------ Triangle draws a wireframe triangle through the three Points using the active color.

func (*Canvas) Width

func (c *Canvas) Width() int

------------------------------------------------------------------------------------------------ Width returns the canvas width in pixels.

type Point

type Point struct {
	X int32
	Y int32
}

------------------------------------------------------------------------------------------------ Point represents a 2D coordinate vector.

Jump to

Keyboard shortcuts

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