Documentation
¶
Index ¶
- Constants
- Variables
- func CheckImage(expected []string, actual image.Image) error
- func DrawLine(dc *gg.Context, x0, y0, x1, y1 int)
- func GetFont(name string) (font.Face, error)
- func GetFontList() ([]string, error)
- func MaxFrameCount(widgets []Widget, bounds image.Rectangle) int
- func ModInt(a, m int) int
- func PaintWidget(w Widget, bounds image.Rectangle, frameIdx int) image.Image
- type Animation
- type Arc
- type Box
- type Circle
- type CircularPath
- type Column
- type Emoji
- type Image
- func (p *Image) FrameCount(bounds image.Rectangle) int
- func (p *Image) Init(*starlark.Thread) error
- func (p *Image) InitFromGIF(data []byte) error
- func (p *Image) InitFromImage(data []byte) error
- func (p *Image) InitFromSVG(data []byte) error
- func (p *Image) InitFromWebP(data []byte) error
- func (p *Image) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int)
- func (p *Image) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle
- func (p *Image) Size() (int, int)
- type ImageChecker
- type Insets
- type Line
- type Marquee
- type Padding
- type Path
- type PathPoint
- type PieChart
- type Plot
- type Point
- type PolyLine
- type Polygon
- type Root
- type RootPaintOption
- type Row
- type Sequence
- type Stack
- type Star
- type Starfield
- type Text
- type Tracer
- type Vector
- type Widget
- type WidgetStaticSize
- type WidgetWithInit
- type WrappedText
Constants ¶
const ( // DefaultFrameWidth is the normal width for a frame. DefaultFrameWidth = 64 // DefaultFrameHeight is the normal height for a frame. DefaultFrameHeight = 32 // DefaultMaxFrameCount is the default maximum number of frames to render. DefaultMaxFrameCount = 2000 )
const ( DefaultFontFace = "tb-8" DefaultFontFace2x = "terminus-16" MaxWidth = 1000 )
const ( AlignLeft = "left" AlignCenter = "center" AlignRight = "right" )
Variables ¶
var DefaultFontColor = color.White
var DefaultPalette = map[string]color.RGBA{
"r": {0xff, 0, 0, 0xff},
"g": {0, 0xff, 0, 0xff},
"b": {0, 0, 0xff, 0xff},
"w": {0xff, 0xff, 0xff, 0xff},
".": {0, 0, 0, 0},
"x": {0, 0, 0, 0xff},
}
var DefaultPlotColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
var FillDampFactor uint8 = 0x55
FillDampFactor determines how much surface fill gets line color dampened.
var (
FontCacheTTL = time.Hour
)
Functions ¶
func DrawLine ¶
DrawLine draws a line from (x0, y0) to (x1, y1).
TODO: use PolyLine from path.go instead.
func GetFontList ¶
func MaxFrameCount ¶
MaxFrameCount computes the maximum frame count of a slice of widgets.
Types ¶
type Animation ¶
type Animation struct {
// Children to use as frames in the animation
Children []Widget
}
Animation turns a list of children into an animation, where each child is a frame.
FIXME: Behaviour when children themselves are animated is a bit weird. Think and fix.
Example:
render.Animation(
children=[
render.Box(width=10, height=10, color="#300"),
render.Box(width=12, height=12, color="#500"),
render.Box(width=14, height=14, color="#700"),
render.Box(width=16, height=16, color="#900"),
render.Box(width=18, height=18, color="#b00"),
],
)
type Arc ¶ added in v0.50.0
type Arc struct {
Widget
// The x-coordinate of the center of the arc.
X float64 `starlark:"x,required"`
// The y-coordinate of the center of the arc.
Y float64 `starlark:"y,required"`
// The radius of the arc.
Radius float64 `starlark:"radius,required"`
// The starting angle of the arc, in radians.
StartAngle float64 `starlark:"start_angle,required"`
// The ending angle of the arc, in radians.
EndAngle float64 `starlark:"end_angle,required"`
// The color of the arc.
Color color.Color `starlark:"color,required"`
// The width of the arc.
Width float64 `starlark:"width,required"`
// Enables antialiased stroke rendering.
AntiAlias bool `starlark:"antialias"`
}
Arc draws an arc. The arc is centered at (x, y).
Example:
render.Arc(
x = 10,
y = 10,
radius = 10,
start_angle = 0,
end_angle = 3.14 * 1.5,
width = 3,
color = "#0ff",
)
type Box ¶
type Box struct {
// Child to center inside box
Child Widget
// Limits Box width
Width int
// Limits Box height
Height int
// Padding around the child widget
Padding int
// Background color
Color color.Color
}
A Box is a rectangular widget that can hold a child widget.
Boxes are transparent unless `color` is provided. They expand to fill all available space, unless `width` and/or `height` is provided. Boxes can have a `child`, which will be centered in the box, and the child can be padded (via `padding`).
Example:
render.Box(
color="#00f",
child=render.Box(
width=20,
height=10,
color="#f00",
)
)
type Circle ¶
type Circle struct {
// Widget to place in the center of the circle
Child Widget
// Fill color
Color color.Color `starlark:"color, required"`
// Diameter of the circle
Diameter int `starlark:"diameter,required"`
}
Circle draws a circle with the given `diameter` and `color`. If a `child` widget is provided, it is drawn in the center of the circle.
Example:
render.Circle(
color="#666",
diameter=30,
child=render.Circle(color="#0ff", diameter=10),
)
type CircularPath ¶
type CircularPath struct {
Radius int
// contains filtered or unexported fields
}
CircularPath draws a circle XXX: from where, in what direction?
func (*CircularPath) Length ¶
func (cp *CircularPath) Length() int
func (*CircularPath) Size ¶
func (cp *CircularPath) Size() (int, int)
type Column ¶
type Column struct {
// Child widgets to lay out
Children []Widget `starlark:"children,required"`
// Alignment along vertical main axis
MainAlign string `starlark:"main_align"`
// Alignment along horizontal cross axis
CrossAlign string `starlark:"cross_align"`
// Column should expand to fill all available vertical space
Expanded bool
}
Column lays out and draws its children vertically (in a column).
By default, a Column is as small as possible, while still holding all its children. However, if `expanded` is set, the Column will fill all available space vertically. The width of a Column is always that of its widest child.
Alignment along the vertical main axis is controlled by passing one of the following `main_align` values: - `"start"`: place children at the beginning of the column - `"end"`: place children at the end of the column - `"center"`: place children in the middle of the column - `"space_between"`: place equal space between children - `"space_evenly"`: equal space between children and before/after first/last child - `"space_around"`: equal space between children, and half of that before/after first/last child
Alignment along the horizontal cross axis is controlled by passing one of the following `cross_align` values: - `"start"`: place children at the left - `"end"`: place children at the right - `"center"`: place children in the center
Example:
render.Column(
children=[
render.Box(width=10, height=8, color="#a00"),
render.Box(width=14, height=6, color="#0a0"),
render.Box(width=16, height=4, color="#00a"),
],
)
Example:
render.Column(
expanded=True,
main_align="space_around",
cross_align="center",
children=[
render.Box(width=10, height=8, color="#a00"),
render.Box(width=14, height=6, color="#0a0"),
render.Box(width=16, height=4, color="#00a"),
],
)
type Emoji ¶
type Emoji struct {
// The Unicode emoji sequence to render
EmojiStr string `starlark:"emoji,required"`
// Scale emoji to this width
Width int
// Scale emoji to this height
Height int
// contains filtered or unexported fields
}
Emoji renders a single emoji at a specified height, maintaining aspect ratio. This allows for rendering emojis much larger than the standard 10x10 pixel size used in text rendering.
Example:
render.Emoji(emoji="😀", height=32) # Large smiley face
func (*Emoji) PaintBounds ¶
type Image ¶
type Image struct {
// Binary image data or SVG text
Src string `starlark:"src,required"`
// Scale image to this width
Width int
// Scale image to this height
Height int
// (Read-only) Frame delay in ms, for animated GIFs
Delay int `starlark:"delay,readonly"`
// Number of render frames to hold each animation frame, default is 1.
HoldFrames int `starlark:"hold_frames"`
// contains filtered or unexported fields
}
Image renders the binary image data passed via `src`. Supported formats include PNG, JPEG, GIF, and SVG.
If `width` or `height` are set, the image will be scaled accordingly, with nearest neighbor interpolation. Otherwise the image's original dimensions are used.
If the image data encodes an animated GIF, the Image instance will also be animated. Frame delay (in milliseconds) can be read from the `delay` attribute.
func (*Image) InitFromGIF ¶
func (*Image) InitFromImage ¶
func (*Image) InitFromSVG ¶
func (*Image) InitFromWebP ¶
func (*Image) PaintBounds ¶
type ImageChecker ¶
func (ImageChecker) Check ¶
func (ic ImageChecker) Check(expected []string, actual image.Image) error
func (ImageChecker) PrintDiff ¶
func (ic ImageChecker) PrintDiff(expected []string, actual image.Image)
func (ImageChecker) PrintImage ¶
func (ic ImageChecker) PrintImage(im image.Image)
type Line ¶ added in v0.50.0
type Line struct {
Widget
// The x-coordinate of the starting point.
X1 float64 `starlark:"x1,required"`
// The y-coordinate of the starting point.
Y1 float64 `starlark:"y1,required"`
// The x-coordinate of the ending point.
X2 float64 `starlark:"x2,required"`
// The y-coordinate of the ending point.
Y2 float64 `starlark:"y2,required"`
// The color of the line.
Color color.Color `starlark:"color,required"`
// The width of the line.
Width float64 `starlark:"width,required"`
// Enables antialiased stroke rendering.
AntiAlias bool `starlark:"antialias"`
}
Line draws a line from (x1, y1) to (x2, y2).
Example:
render.Line(
x1 = 0,
y1 = 0,
x2 = 63,
y2 = 31,
width = 1,
color = "#fff",
)
type Marquee ¶
type Marquee struct {
// Widget to potentially scroll
Child Widget `starlark:"child,required"`
// Width of the Marquee, required for horizontal
Width int `starlark:"width"`
// Height of the Marquee, required for vertical
Height int `starlark:"height"`
// Position of child at beginning of animation
OffsetStart int `starlark:"offset_start"`
// Position of child at end of animation
OffsetEnd int `starlark:"offset_end"`
// Direction to scroll, 'vertical' or 'horizontal', default is horizontal
ScrollDirection string `starlark:"scroll_direction"`
// Alignment when contents fit on screen, 'start', 'center' or 'end', default is start
Align string `starlark:"align"`
// Delay the scroll of the animation by a certain number of frames, default is 0
Delay int `starlark:"delay"`
}
Marquee scrolls its child horizontally or vertically.
The `scroll_direction` will be 'horizontal' and will scroll from right to left if left empty, if specified as 'vertical' the Marquee will scroll from bottom to top.
In horizontal mode the height of the Marquee will be that of its child, but its `width` must be specified explicitly. In vertical mode the width will be that of its child but the `height` must be specified explicitly.
If the child's width fits fully, it will not scroll.
The `offset_start` and `offset_end` parameters control the position of the child in the beginning and the end of the animation.
Alignment for a child that fits fully along the horizontal/vertical axis is controlled by passing one of the following `align` values: - `"start"`: place child at the left/top - `"end"`: place child at the right/bottom - `"center"`: place child at the center
Example:
render.Marquee(
width=64,
child=render.Text("this won't fit in 64 pixels"),
offset_start=5,
offset_end=32,
)
type Padding ¶
type Padding struct {
// The Widget to place padding around
Child Widget `starlark:"child,required"`
// Padding around the child
Pad Insets
// This is a confusing parameter
Expanded bool
// Background color.
Color color.Color
}
Padding places padding around its child.
If the `pad` attribute is a single integer, that amount of padding will be placed on all sides of the child. If it's a 4-tuple `(left, top, right, bottom)`, then padding will be placed on the sides accordingly.
type PieChart ¶
type PieChart struct {
// List of color hex codes
Colors []color.Color `starlark:"colors, required"`
// List of numbers corresponding to the relative size of each color
Weights []float64 `starlark:"weights, required"`
// Diameter of the circle
Diameter int `starlark:"diameter,required"`
}
PieChart draws a circular pie chart of size `diameter`. It takes two arguments for the data: parallel lists `colors` and `weights` representing the shading and relative sizes of each data entry.
Example:
render.PieChart(
colors = [ "#fff", "#0f0", "#00f" ],
weights = [ 180, 135, 45 ],
diameter = 30,
)
type Plot ¶
type Plot struct {
// A list of 2-tuples of numbers
Data [][2]float64 `starlark:"data,required"`
// Limits Plot width
Width int `starlark:"width,required"`
// Limits Plot height
Height int `starlark:"height,required"`
// Line color, default is '#fff'
Color color.Color `starlark:"color"`
// Line color for Y-values below 0
ColorInverted color.Color `starlark:"color_inverted"`
// Limit X-axis to a range
XLim [2]float64 `starlark:"x_lim"`
// Limit Y-axis to a range
YLim [2]float64 `starlark:"y_lim"`
// Paint surface between line and X-axis
Fill bool `starlark:"fill"`
// Specifies the type of chart to render, "scatter" or "line", default is "line"
ChartType string `starlark:"chart_type"`
// Fill color for Y-values above 0
FillColor color.Color `starlark:"fill_color"`
// Fill color for Y-values below 0
FillColorInverted color.Color `starlark:"fill_color_inverted"`
// contains filtered or unexported fields
}
Plot is a widget that draws a data series.
Example:
render.Plot(
data = [
(0, 3.35),
(1, 2.15),
(2, 2.37),
(3, -0.31),
(4, -3.53),
(5, 1.31),
(6, -1.3),
(7, 4.60),
(8, 3.33),
(9, 5.92),
],
width = 64,
height = 32,
color = "#0f0",
color_inverted = "#f00",
x_lim = (0, 9),
y_lim = (-5, 7),
fill = True,
)
type PolyLine ¶
type PolyLine struct {
Vertices []PathPoint
// contains filtered or unexported fields
}
PolyLine draws straight lines passing through a list of vertices.
type Polygon ¶ added in v0.50.0
type Polygon struct {
Widget
// A list of (x, y) tuples representing the vertices of the polygon.
Vertices []Point `starlark:"vertices,required"`
// The color used to fill the polygon.
FillColor color.Color `starlark:"fill_color"`
// The color used to draw the polygon's stroke.
StrokeColor color.Color `starlark:"stroke_color"`
// The width of the polygon's stroke.
StrokeWidth float64 `starlark:"stroke_width"`
// Enables antialiased stroke rendering.
AntiAlias bool `starlark:"antialias"`
}
Polygon draws a polygon.
Example:
render.Polygon(
vertices = [(0, 0), (20, 0), (20, 10), (0, 10)],
fill_color = "#00f",
stroke_color = "#fff",
stroke_width = 1,
)
func (Polygon) FrameCount ¶ added in v0.50.0
type Root ¶
type Root struct {
// Widget to render
Child Widget `starlark:"child,required"`
// Frame delay in milliseconds
Delay int32 `starlark:"delay"`
// Expiration time in seconds
MaxAge int32 `starlark:"max_age"`
// Request animation is shown in full, regardless of app cycle speed.
ShowFullAnimation bool `starlark:"show_full_animation"`
// contains filtered or unexported fields
}
Root is the top level of every Widget tree.
The child widget, and all its descendants, will be drawn on a 64x32 canvas. Root places its child in the upper left corner of the canvas.
If the tree contains animated widgets, the resulting animation will run with _delay_ milliseconds per frame.
If the tree holds time sensitive information which must never be displayed past a certain point in time, pass _MaxAge_ to specify an expiration time in seconds. Display devices use this to avoid displaying stale data in the event of e.g. connectivity issues.
type RootPaintOption ¶
type RootPaintOption func(*Root)
func WithMaxFrameCount ¶
func WithMaxFrameCount(maxFrames int) RootPaintOption
WithMaxFrameCount sets the maximum number of frames that will be rendered. If a widget tree has more frames than this, the number of frames will be capped.
func WithMaxParallelFrames ¶
func WithMaxParallelFrames(maxFrames int) RootPaintOption
WithMaxParallelFrames sets the maximum number of frames rendered concurrently. If <=0, Paint uses runtime.NumCPU().
type Row ¶
type Row struct {
// Child widgets to lay out
Children []Widget `starlark:"children,required"`
// Alignment along horizontal main axis
MainAlign string `starlark:"main_align"`
// Alignment along vertical cross axis
CrossAlign string `starlark:"cross_align"`
// Row should expand to fill all available horizontal space
Expanded bool
}
Row lays out and draws its children horizontally (in a row).
By default, a Row is as small as possible, while still holding all its children. However, if `expanded` is set, the Row will fill all available space horizontally. The height of a Row is always that of its tallest child.
Alignment along the horizontal main axis is controlled by passing one of the following `main_align` values: - `"start"`: place children at the beginning of the row - `"end"`: place children at the end of the row - `"center"`: place children in the middle of the row - `"space_between"`: place equal space between children - `"space_evenly"`: equal space between children and before/after first/last child - `"space_around"`: equal space between children, and half of that before/after first/last child
Alignment along the vertical cross axis is controlled by passing one of the following `cross_align` values: - `"start"`: place children at the top - `"end"`: place children at the bottom - `"center"`: place children at the center
Example:
render.Row(
children=[
render.Box(width=10, height=8, color="#a00"),
render.Box(width=14, height=6, color="#0a0"),
render.Box(width=16, height=4, color="#00a"),
],
)
Example:
render.Row(
expanded=True,
main_align="space_between",
cross_align="end",
children=[
render.Box(width=10, height=8, color="#a00"),
render.Box(width=14, height=6, color="#0a0"),
render.Box(width=16, height=4, color="#00a"),
],
)
type Sequence ¶
type Sequence struct {
// List of child widgets
Children []Widget `starlark:"children,required"`
}
Sequence renders a list of child widgets in sequence.
Each child widget is rendered for the duration of its frame count, then the next child wiget in the list will be rendered and so on.
It comes in quite useful when chaining animations. If you want to know more about that, go check out the [animation](animation.md) documentation.
Example:
render.Sequence(
children = [
render.Box(width=10, height=10, color="#f00"),
render.Box(width=10, height=10, color="#0f0"),
render.Box(width=10, height=10, color="#00f"),
],
)
type Stack ¶
type Stack struct {
// Widgets to stack
Children []Widget `starlark:"children,required"`
}
Stack draws its children on top of each other.
Just like a stack of pancakes, except with Widgets instead of pancakes. The Stack will be given a width and height sufficient to fit all its children.
Example:
render.Stack(
children=[
render.Box(width=50, height=25, color="#911"),
render.Text("hello there"),
render.Box(width=4, height=32, color="#119"),
],
)
type Starfield ¶
type Starfield struct {
Child Widget
Color color.Color
Width int
Height int
// contains filtered or unexported fields
}
type Text ¶
type Text struct {
// The text string to draw
Content string `starlark:"content,required"`
// Desired font face
Font string
// Limits height of the area on which text is drawn
Height int
// Shifts position of text vertically.
Offset int
// Desired font color
Color color.Color
// contains filtered or unexported fields
}
Text draws a string of text on a single line.
By default, the text will use the "tb-8" font, but other fonts can be chosen via the `font` attribute. The `height` and `offset` parameters allow fine tuning of the vertical layout of the string. Take a look at the [font documentation](fonts.md) for more information.
Example:
render.Text(content="Tidbyt!", color="#099")
func (*Text) PaintBounds ¶
type Vector ¶
type Vector struct {
Children []Widget
MainAlign string `starlark:"main_align"`
CrossAlign string `starlark:"cross_align"`
Expanded bool
Vertical bool
}
Vector draws its children either vertically or horizontally (like a row or a column).
A vector has a main axis along which children are draw. The main axis is either horizontal or vertical (i.e. a row or a column). MainAlign controls how children are placed along this axis. CrossAlign controls placement orthogonally to the main axis.
type Widget ¶
type Widget interface {
// PaintBounds Returns the bounds of the area that will actually be drawn to when Paint() is called
PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle
Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int)
FrameCount(bounds image.Rectangle) int
}
A Widget is a self-contained object that can render itself as an image.
type WidgetStaticSize ¶
WidgetStaticSize has inherent size and width known before painting.
type WidgetWithInit ¶
WidgetWithInit is an interface for widgets that require initialization.
type WrappedText ¶
type WrappedText struct {
// The text string to draw
Content string `starlark:"content,required"`
// Desired font face
Font string
// Limits height of the area on which text may be drawn
Height int
// Limits width of the area on which text may be drawn
Width int
// Controls spacing between lines
LineSpacing int
// Desired font color
Color color.Color
// Text alignment
Align string
// If true, long words that exceed the width will be broken to fit
WordBreak bool
// contains filtered or unexported fields
}
WrappedText draws multi-line text.
The optional `width` and `height` parameters limit the drawing area. If not set, WrappedText will use as much vertical and horizontal space as possible to fit the text.
Alignment of the text is controlled by passing one of the following `align` values: - `"left"`: align text to the left - `"center"`: align text in the center - `"right"`: align text to the right
Example:
render.WrappedText(
content="this is a multi-line text string",
width=50,
color="#fa0",
)
func (*WrappedText) FrameCount ¶
func (tw *WrappedText) FrameCount(bounds image.Rectangle) int