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 PaintRoots(width, height int, solidBackground bool, roots ...Root) []image.Image
- 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.
func ModInt ¶
ModInt computes a (mod m). Useful for handling frameIdx > num available frames in Widget.Paint().
func PaintRoots ¶
PaintRoots draws >=1 Roots which must all have the same dimensions.
Types ¶
type Animation ¶
type Animation struct {
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.
DOC(Children): Children to use as frames in the animation
EXAMPLE BEGIN
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"),
],
)
EXAMPLE END.
type Arc ¶ added in v0.50.0
type Arc struct {
Widget
X float64 `starlark:"x,required"`
Y float64 `starlark:"y,required"`
Radius float64 `starlark:"radius,required"`
StartAngle float64 `starlark:"start_angle,required"`
EndAngle float64 `starlark:"end_angle,required"`
Color color.Color `starlark:"color,required"`
Width float64 `starlark:"width,required"`
}
Arc draws an arc. The arc is centered at (x, y).
DOC(X): The x-coordinate of the center of the arc. DOC(Y): The y-coordinate of the center of the arc. DOC(Radius): The radius of the arc. DOC(StartAngle): The starting angle of the arc, in radians. DOC(EndAngle): The ending angle of the arc, in radians. DOC(Color): The color of the arc. DOC(Width): The width of the arc.
EXAMPLE BEGIN render.Arc(
x = 10, y = 10, radius = 10, start_angle = 0, end_angle = 3.14 * 1.5, width = 3, color = "#0ff",
) EXAMPLE END.
type Box ¶
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`).
DOC(Child): Child to center inside box DOC(Width): Limits Box width DOC(Height): Limits Box height DOC(Padding): Padding around the child widget DOC(Color): Background color
EXAMPLE BEGIN
render.Box(
color="#00f",
child=render.Box(
width=20,
height=10,
color="#f00",
)
)
EXAMPLE END.
type Circle ¶
type Circle struct {
Child Widget
Color color.Color `starlark:"color, required"`
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.
DOC(Child): Widget to place in the center of the circle DOC(Color): Fill color DOC(Diameter): Diameter of the circle
EXAMPLE BEGIN
render.Circle(
color="#666",
diameter=30,
child=render.Circle(color="#0ff", diameter=10),
)
EXAMPLE END.
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 {
Children []Widget `starlark:"children,required"`
MainAlign string `starlark:"main_align"`
CrossAlign string `starlark:"cross_align"`
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
DOC(Children): Child widgets to lay out DOC(Expanded): Column should expand to fill all available vertical space DOC(MainAlign): Alignment along vertical main axis DOC(CrossAlign): Alignment along horizontal cross axis
EXAMPLE BEGIN
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 END ¶
EXAMPLE BEGIN
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"),
],
)
EXAMPLE END.
type Emoji ¶
type Emoji struct {
EmojiStr string `starlark:"emoji,required"`
Width, 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.
DOC(Emoji): The Unicode emoji sequence to render DOC(Width): Scale emoji to this width DOC(Height): Scale emoji to this height
EXAMPLE BEGIN
render.Emoji(emoji="😀", height=32) # Large smiley face
EXAMPLE END.
func (*Emoji) PaintBounds ¶
type Image ¶
type Image struct {
Src string `starlark:"src,required"`
Width, Height int
Delay int `starlark:"delay,readonly"`
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.
DOC(Src): Binary image data or SVG text DOC(Width): Scale image to this width DOC(Height): Scale image to this height DOC(Delay): (Read-only) Frame delay in ms, for animated GIFs DOC(HoldFrames): Number of render frames to hold each animation frame, default is 1.
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
X1 float64 `starlark:"x1,required"`
Y1 float64 `starlark:"y1,required"`
X2 float64 `starlark:"x2,required"`
Y2 float64 `starlark:"y2,required"`
Color color.Color `starlark:"color,required"`
Width float64 `starlark:"width,required"`
}
Line draws a line from (x1, y1) to (x2, y2).
DOC(X1): The x-coordinate of the starting point. DOC(Y1): The y-coordinate of the starting point. DOC(X2): The x-coordinate of the ending point. DOC(Y2): The y-coordinate of the ending point. DOC(Color): The color of the line. DOC(Width): The width of the line.
EXAMPLE BEGIN render.Line(
x1 = 0, y1 = 0, x2 = 63, y2 = 31, width = 1, color = "#fff",
) EXAMPLE END.
type Marquee ¶
type Marquee struct {
Child Widget `starlark:"child,required"`
Width int `starlark:"width"`
Height int `starlark:"height"`
OffsetStart int `starlark:"offset_start"`
OffsetEnd int `starlark:"offset_end"`
ScrollDirection string `starlark:"scroll_direction"`
Align string `starlark:"align"`
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
DOC(Child): Widget to potentially scroll DOC(Width): Width of the Marquee, required for horizontal DOC(Height): Height of the Marquee, required for vertical DOC(OffsetStart): Position of child at beginning of animation DOC(OffsetEnd): Position of child at end of animation DOC(ScrollDirection): Direction to scroll, 'vertical' or 'horizontal', default is horizontal DOC(Align): Alignment when contents fit on screen, 'start', 'center' or 'end', default is start DOC(Delay): Delay the scroll of the animation by a certain number of frames, default is 0
EXAMPLE BEGIN
render.Marquee(
width=64,
child=render.Text("this won't fit in 64 pixels"),
offset_start=5,
offset_end=32,
)
EXAMPLE END.
type Padding ¶
type Padding struct {
Child Widget `starlark:"child,required"`
Pad Insets
Expanded bool
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.
DOC(Child): The Widget to place padding around DOC(Expanded): This is a confusing parameter DOC(Pad): Padding around the child DOC(Color): Background color.
type PieChart ¶
type PieChart struct {
Colors []color.Color `starlark:"colors, required"`
Weights []float64 `starlark:"weights, required"`
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.
DOC(Colors): List of color hex codes DOC(Weights): List of numbers corresponding to the relative size of each color DOC(Diameter): Diameter of the circle
EXAMPLE BEGIN
render.PieChart(
colors = [ "#fff", "#0f0", "#00f" ],
weights = [ 180, 135, 45 ],
diameter = 30,
)
EXAMPLE END.
type Plot ¶
type Plot struct {
// Coordinates of points to plot
Data [][2]float64 `starlark:"data,required"`
// Overall size of the plot
Width int `starlark:"width,required"`
Height int `starlark:"height,required"`
// Primary line color
Color color.Color `starlark:"color"`
// Optional line color for Y-values below 0
ColorInverted color.Color `starlark:"color_inverted"`
// Optional limit on X and Y axis
XLim [2]float64 `starlark:"x_lim"`
YLim [2]float64 `starlark:"y_lim"`
// If true, also paint surface between line and X-axis
Fill bool `starlark:"fill"`
// Optional, default "line". If set to "scatter", the line connecting dots will not be drawn
ChartType string `starlark:"chart_type"`
// Optional fill color for Y-values above 0
FillColor color.Color `starlark:"fill_color"`
// Optional 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.
DOC(Data): A list of 2-tuples of numbers DOC(Width): Limits Plot width DOC(Height): Limits Plot height DOC(Color): Line color, default is '#fff' DOC(ColorInverted): Line color for Y-values below 0 DOC(XLim): Limit X-axis to a range DOC(YLim): Limit Y-axis to a range DOC(Fill): Paint surface between line and X-axis DOC(FillColor): Fill color for Y-values above 0 DOC(FillColorInverted): Fill color for Y-values below 0 DOC(ChartType): Specifies the type of chart to render, "scatter" or "line", default is "line"
EXAMPLE BEGIN
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,
)
EXAMPLE END.
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
Vertices []Point `starlark:"vertices,required"`
FillColor color.Color `starlark:"fill_color"`
StrokeColor color.Color `starlark:"stroke_color"`
StrokeWidth float64 `starlark:"stroke_width"`
}
Polygon draws a polygon.
DOC(Vertices): A list of (x, y) tuples representing the vertices of the polygon. DOC(FillColor): The color used to fill the polygon. DOC(StrokeColor): The color used to draw the polygon's stroke. DOC(StrokeWidth): The width of the polygon's stroke.
EXAMPLE BEGIN render.Polygon(
vertices = [(0, 0), (20, 0), (20, 10), (0, 10)], fill_color = "#00f", stroke_color = "#fff", stroke_width = 1,
) EXAMPLE END.
func (Polygon) FrameCount ¶ added in v0.50.0
type Root ¶
type Root struct {
Child Widget `starlark:"child,required"`
Delay int32 `starlark:"delay"`
MaxAge int32 `starlark:"max_age"`
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.
DOC(Child): Widget to render DOC(Delay): Frame delay in milliseconds DOC(MaxAge): Expiration time in seconds DOC(ShowFullAnimation): Request animation is shown in full, regardless of app cycle speed.
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
type Row ¶
type Row struct {
Children []Widget `starlark:"children,required"`
MainAlign string `starlark:"main_align"`
CrossAlign string `starlark:"cross_align"`
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
DOC(Children): Child widgets to lay out DOC(Expanded): Row should expand to fill all available horizontal space DOC(MainAlign): Alignment along horizontal main axis DOC(CrossAlign): Alignment along vertical cross axis
EXAMPLE BEGIN
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 END
EXAMPLE BEGIN
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"),
],
)
EXAMPLE END.
type Sequence ¶
type Sequence struct {
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.
DOC(Children): List of child widgets
EXAMPLE BEGIN
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"),
],
)
EXAMPLE END.
type Stack ¶
type Stack struct {
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.
DOC(Children): Widgets to stack
EXAMPLE BEGIN
render.Stack(
children=[
render.Box(width=50, height=25, color="#911"),
render.Text("hello there"),
render.Box(width=4, height=32, color="#119"),
],
)
EXAMPLE END.
type Starfield ¶
type Starfield struct {
Child Widget
Color color.Color
Width int
Height int
// contains filtered or unexported fields
}
type Text ¶
type Text struct {
Content string `starlark:"content,required"`
Font string
Height int
Offset int
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.
DOC(Content): The text string to draw DOC(Font): Desired font face DOC(Height): Limits height of the area on which text is drawn DOC(Offset): Shifts position of text vertically. DOC(Color): Desired font color
EXAMPLE BEGIN
render.Text(content="Tidbyt!", color="#099")
EXAMPLE END.
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 {
Content string `starlark:"content,required"`
Font string
Height int
Width int
LineSpacing int
Color color.Color
Align string
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
DOC(Content): The text string to draw DOC(Font): Desired font face DOC(Height): Limits height of the area on which text may be drawn DOC(Width): Limits width of the area on which text may be drawn DOC(LineSpacing): Controls spacing between lines DOC(Color): Desired font color DOC(Align): Text Alignment DOC(WordBreak): If true, long words that exceed the width will be broken to fit EXAMPLE BEGIN
render.WrappedText(
content="this is a multi-line text string",
width=50,
color="#fa0",
)
EXAMPLE END.
func (*WrappedText) FrameCount ¶
func (tw *WrappedText) FrameCount(bounds image.Rectangle) int