canvas

package
v1.0.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: 7 Imported by: 0

Documentation

Overview

Package canvas provides a small software drawing surface for menus, text overlays and 2D scenes, so a front-end can compose them the same way a software renderer composes frames: as raw RGBA pixels.

New returns a Canvas sized to the window. Callers compose a screen with Canvas.Fill or Canvas.DimFrom, Canvas.Rect, Canvas.Text, and Canvas.TextCentered, then pass Canvas.Pixels to the WritePixels method of an Ebiten image. Canvas.Line, Canvas.Circle and Canvas.Polygon add anti-aliased vector shapes, enough for a 2D visualizer to draw a whole frame without a display.

The package does not import Ebiten itself, so it needs no display: the same drawing code can render a live window or, through a headless front-end, the project's documentation media.

Index

Examples

Constants

View Source
const GlyphWidth = 7

GlyphWidth is the advance of the built-in 7x13 face, used to centre text.

Variables

This section is empty.

Functions

func MeasureFace added in v0.15.0

func MeasureFace(s string, face font.Face) int

MeasureFace returns the pixel advance s would take in face, so a caller can centre or wrap text before drawing it. A nil face measures the built-in one.

Types

type Canvas

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

Canvas is a fixed-size RGBA pixel surface.

Example

ExampleCanvas composes a small title screen the way the family's menus do: fill, highlight bar, centred text, then hand the pixels to Ebiten's WritePixels.

package main

import (
	"fmt"
	"image/color"

	"github.com/danielriddell21/crucible/canvas"
)

func main() {
	c := canvas.New(320, 200)
	c.Fill(color.RGBA{R: 10, G: 12, B: 16, A: 255})
	c.Rect(0, 87, 320, 18, color.RGBA{R: 24, G: 40, B: 34, A: 255})
	c.TextCentered(100, "> DESCEND <", color.RGBA{R: 90, G: 220, B: 160, A: 255})

	w, h := c.Size()
	fmt.Println(w, h, len(c.Pixels()) == w*h*4)
}
Output:
320 200 true

func New

func New(w, h int) *Canvas

New returns a canvas of the given size in pixels.

func (*Canvas) Blend added in v0.15.0

func (c *Canvas) Blend(x, y, w, h int, col color.RGBA)

Blend fills a rectangle, compositing col over what is already there. Unlike Canvas.Rect, which paints opaquely, it reads col.A as straight (not premultiplied) opacity — so a tint highlighting a board square or a band dimming the scene behind a banner lets the picture show through.

func (*Canvas) Circle added in v0.15.0

func (c *Canvas) Circle(x, y, radius float64, col color.RGBA)

Circle fills a disc of the given radius centred on (x, y), anti-aliased. It approximates the outline with enough straight segments that the edge reads as smooth at the radius drawn.

func (*Canvas) DimFrom

func (c *Canvas) DimFrom(fb []byte, k float64)

DimFrom copies another framebuffer scaled toward black by factor k in [0, 1], so a paused game shows faintly behind its menu.

func (*Canvas) Fill

func (c *Canvas) Fill(col color.RGBA)

Fill paints the whole canvas with a single opaque colour.

func (*Canvas) Line added in v0.15.0

func (c *Canvas) Line(x1, y1, x2, y2, width float64, col color.RGBA)

Line strokes a line from (x1, y1) to (x2, y2) with the given width, anti-aliased. A width below one pixel is drawn as one pixel.

func (*Canvas) Pixels

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

Pixels returns the underlying RGBA pixel buffer, suitable for (*ebiten.Image).WritePixels.

func (*Canvas) Polygon added in v0.15.0

func (c *Canvas) Polygon(pts [][2]float64, col color.RGBA)

Polygon fills the closed polygon through pts with col, anti-aliased. Fewer than three points draws nothing. Points are in canvas pixel coordinates.

func (*Canvas) Rect

func (c *Canvas) Rect(x, y, w, h int, col color.RGBA)

Rect fills an axis-aligned rectangle, clipped to the canvas.

func (*Canvas) Ring added in v0.15.0

func (c *Canvas) Ring(x, y, radius, width float64, col color.RGBA)

Ring strokes a circle's outline of the given width, anti-aliased — the halo round a highlighted entity, a radius marker on a map. A width at or beyond the radius fills the disc.

func (*Canvas) Size

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

Size returns the canvas width and height in pixels.

func (*Canvas) Text

func (c *Canvas) Text(x, y int, s string, col color.RGBA)

Text draws s with its baseline at (x, y) in the built-in 7x13 face.

func (*Canvas) TextCentered

func (c *Canvas) TextCentered(y int, s string, col color.RGBA)

TextCentered draws s horizontally centred at baseline y, with a one-pixel drop shadow.

func (*Canvas) TextFace added in v0.15.0

func (c *Canvas) TextFace(x, y int, s string, col color.RGBA, face font.Face) int

TextFace draws s with its baseline at (x, y) in a caller-supplied face, returning the advance in pixels. It is the escape hatch from the built-in 7x13 face for front-ends with their own typography — a chess board's piece glyphs, a title screen's display face — so they can compose headlessly without giving up their look.

A nil face falls back to the built-in one.

Jump to

Keyboard shortcuts

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