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 ¶
- Constants
- func MeasureFace(s string, face font.Face) int
- type Canvas
- func (c *Canvas) Blend(x, y, w, h int, col color.RGBA)
- func (c *Canvas) Circle(x, y, radius float64, col color.RGBA)
- func (c *Canvas) DimFrom(fb []byte, k float64)
- func (c *Canvas) Fill(col color.RGBA)
- func (c *Canvas) Line(x1, y1, x2, y2, width float64, col color.RGBA)
- func (c *Canvas) Pixels() []byte
- func (c *Canvas) Polygon(pts [][2]float64, col color.RGBA)
- func (c *Canvas) Rect(x, y, w, h int, col color.RGBA)
- func (c *Canvas) Ring(x, y, radius, width float64, col color.RGBA)
- func (c *Canvas) Size() (w, h int)
- func (c *Canvas) Text(x, y int, s string, col color.RGBA)
- func (c *Canvas) TextCentered(y int, s string, col color.RGBA)
- func (c *Canvas) TextFace(x, y int, s string, col color.RGBA, face font.Face) int
Examples ¶
Constants ¶
const GlyphWidth = 7
GlyphWidth is the advance of the built-in 7x13 face, used to centre text.
Variables ¶
This section is empty.
Functions ¶
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 (*Canvas) Blend ¶ added in v0.15.0
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
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 ¶
DimFrom copies another framebuffer scaled toward black by factor k in [0, 1], so a paused game shows faintly behind its menu.
func (*Canvas) Line ¶ added in v0.15.0
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 ¶
Pixels returns the underlying RGBA pixel buffer, suitable for (*ebiten.Image).WritePixels.
func (*Canvas) Polygon ¶ added in v0.15.0
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) Ring ¶ added in v0.15.0
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) TextCentered ¶
TextCentered draws s horizontally centred at baseline y, with a one-pixel drop shadow.
func (*Canvas) TextFace ¶ added in v0.15.0
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.