Context

Library for creating and drawing to canvases.


Source: pkg/lua/lib/context.go | Import: context

Functions


degrees

degrees(radians) -> float

Arguments

  • radians float
Return Values

  • float - Degrees.
radians

radians(degrees) -> float

Arguments

  • degrees float
Return Values

  • float - Radians.
point

point(x, y) -> struct<context.Point>

Arguments

  • x float
  • y float
distance

distance(p1, p2) -> float

Arguments

  • p1 struct<context.Point>
  • p2 struct<context.Point>
Return Values

  • float
interpolate

interpolate(p1, p2, t) -> struct<context.Point>

Arguments

  • p1 struct<context.Point>
  • p2 struct<context.Point>
  • t float
Return Values

  • struct<context.Point>
new

new(width, height) -> int<collection.CONTEXT>

Arguments

  • width int
  • height int
new_image

new_image(id) -> int<collection.CONTEXT>

Arguments

  • id int<collection.IMAGE>
Return Values

  • int<collection.CONTEXT>
new_direct

new_direct(id) -> int<collection.CONTEXT>

Creates a new context directly on the image, this requires the image to use the RGBA color model. If not it will be converted to RGBA. This is also not thread safe, as modifying either the image or the context will affect the other, with no guarantee of the order of operations.

Arguments

  • id int<collection.IMAGE>
Return Values

  • int<collection.CONTEXT>
to_image

to_image(id, name, encoding, model?, copy?) -> int<collection.IMAGE>

Arguments

  • id int<collection.CONTEXT>
  • name string
  • encoding int<image.Encoding>
  • model int<image.Model>
  • copy bool - Set to true to copy the image, otherwise continuing to draw can affect the image.
Return Values

  • int<collection.IMAGE>
to_mask

to_mask(id, name, encoding) -> int<collection.IMAGE>

Arguments

  • id int<collection.CONTEXT>
  • name string
  • encoding int<image.Encoding>
Return Values

  • int<collection.IMAGE> - The returned image will use the 'image.MODEL_ALPHA' color model.
mask

mask(id, img)

The image will be copied and converted to 'image.MODEL_ALPHA'.

Arguments

  • id int<collection.CONTEXT>
  • img int<collection.IMAGE>
size

size(id) -> int, int

@blocking
Arguments

  • id int<collection.CONTEXT>
Return Values

  • int - The width of the context.
  • int - The height of the context.
font_height

font_height(id) -> float

@blocking
Arguments

  • id int<collection.CONTEXT>
Return Values

  • float - The height of text rendered with the current font.
string_measure

string_measure(id, str) -> float, float

@blocking
Arguments

  • id int<collection.CONTEXT>
  • str string
Return Values

  • float - The width of text rendered with the current font.
  • float - The height of text rendered with the current font.
string_measure_multiline

string_measure_multiline(id, str, spacing) -> float, float

@blocking
Arguments

  • id int<collection.CONTEXT>
  • str string
  • spacing float - The space between each line.
Return Values

  • float - The width of text rendered with the current font.
  • float - The height of text rendered with the current font.
current_point

current_point(id) -> float, float, bool

@blocking
Arguments

  • id int<collection.CONTEXT>
Return Values

  • float - The x location of the current point.
  • float - The y location of the current point.
  • bool - If the current point has been set.
transform_point

transform_point(id, x, y) -> float, float

@blocking

Multiplies a point by the current matrix.

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
Return Values

  • float -> New x position.
  • float -> New y position.
clear

clear(id)

Fills the context with the current color.

Arguments

  • id int<collection.CONTEXT>
clip

clip(id, preserve?)

Updates the clipping region by intersecting the current clipping region with the current path as it would be filled by fill().

Arguments

  • id int<collection.CONTEXT>
  • preserve bool - Set in order to keep the current path.
clip_reset

clip_reset(id)

Clears the clipping region.

Arguments

  • id int<collection.CONTEXT>
path_clear

path_clear(id)

Removes all points from the current path.

Arguments

  • id int<collection.CONTEXT>
path_close

path_close(id)

Adds a line segment from the current point to the initial point.

Arguments

  • id int<collection.CONTEXT>
path_to

path_to(id, x, y)

Starts a new subpath starting at the given point.

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
subpath

subpath(id)

Starts a new subpath starting at the current point. No current point will be set after.

Arguments

  • id int<collection.CONTEXT>
draw_cubic

draw_cubic(id, x1, y1, x2, y2, x3, y3)

Draws a cubic bezier curve to the path starting at the current point, if there isn't a current point, it moves to (x1, y1).

Arguments

  • id int<collection.CONTEXT>
  • x1 float
  • y1 float
  • x2 float
  • y2 float
  • x3 float
  • y3 float
draw_quadratic

draw_quadratic(id, x1, y1, x2, y2)

Draws a quadratic bezier curve to the path starting at the current point, if there isn't a current point, it moves to (x1, y1).

Arguments

  • id int<collection.CONTEXT>
  • x1 float
  • y1 float
  • x2 float
  • y2 float
draw_arc

draw_arc(id, x, y, r, angle1, angle2)

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
  • r float
  • angle1 float
  • angle2 float
draw_circle

draw_circle(id, x, y, r)

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
  • r float
draw_ellipse

draw_ellipse(id, x, y, rx, xy)

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
  • rx float
  • ry float
draw_elliptical_arc

draw_elliptical_arc(id, x, y, rx, ry, angle1, angle2)

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
  • rx float
  • ry float
  • angle1 float
  • angle2 float
draw_image

draw_image(id, img, x, y)

Arguments

  • id int<collection.CONTEXT>
  • img int<collection.IMAGE>
  • x int
  • y int
draw_image_anchor

draw_image_anchor(id, img, x, y, ax, ay)

The anchor is a point between (0,0) and (1,1), so (0.5,0.5) is centered.

Arguments

  • id int<collection.CONTEXT>
  • img int<collection.IMAGE>
  • x int
  • y int
  • ax float
  • ay float
draw_line

draw_line(id, x1, y1, x2, y2)

Arguments

  • id int<collection.CONTEXT>
  • x1 float
  • y1 float
  • x2 float
  • y2 float
draw_line_to

draw_line_to(id, x, y)

Draws a line to the point, starting from the current point. If there is no current point, no line will be drawn and the current point will be set.

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
draw_point

draw_point(id, x, y, r)

Similar to draw_circle but ensures that a circle of the specified size is drawn regardless of the current transformation matrix. The position is still transformed, but not the shape of the point.

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
  • r float
draw_rect

draw_rect(id, x, y, width, height)

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
  • width float
  • height float
draw_rect_round

draw_rect_round(id, x, y, width, height, r)

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
  • width float
  • height float
  • r float
draw_polygon

draw_polygon(id, n, x, y, r, rotation)

Arguments

  • id int<collection.CONTEXT>
  • n int - The number of sides.
  • x float
  • y float
  • r float
  • rotation float
draw_string

draw_string(id, str, x, y)

Arguments

  • id int<collection.CONTEXT>
  • str string
  • x float
  • y float
draw_string_anchor

draw_string_anchor(id, str, x, y, ax, ay)

The anchor is a point between (0,0) and (1,1), so (0.5,0.5) is centered.

Arguments

  • id int<collection.CONTEXT>
  • str string
  • x float
  • y float
  • ax float
  • ay float
draw_string_wrap

draw_string_wrap(id, str, x, y, ax, ay, width, spacing, align)

The anchor is a point between (0,0) and (1,1), so (0.5,0.5) is centered.

Arguments

  • id int<collection.CONTEXT>
  • str string
  • x float
  • y float
  • ax float
  • ay float
  • width float
  • spacing float - The spacing between each line.
  • align int<context.Align>
fill

fill(id, preserve?)

Fills the current path with the current color. Closes open paths.

Arguments

  • id int<collection.CONTEXT>
  • preserve bool - Set in order to keep the current path.
fill_rule

fill_rule(id, rule)

Arguments

  • id int<collection.CONTEXT>
  • rule int<context.FillRule>
stroke

stroke(id, preserve?)

Strokes the current path with the current color.

Arguments

  • id int<collection.CONTEXT>
  • preserve bool - Set in order to keep the current path.
identity

identity(id)

Resets the current transformation matrix to the identity matrix.

Arguments

  • id int<collection.CONTEXT>
mask_invert

mask_invert(id)

Inverts the alpha values of the clipping mask.

Arguments

  • id int<collection.CONTEXT>
invert_y

invert_y(id)

Flips the y axis so that Y=0 is at the bottom of the image.

Arguments

  • id int<collection.CONTEXT>
push

push(id)

Push the current context state to the stack.

Arguments

  • id int<collection.CONTEXT>
pop

pop(id)

Pop the current context state to the stack.

Arguments

  • id int<collection.CONTEXT>
rotate

rotate(id, angle)

Rotates the transformation matrix around the origin.

Arguments

  • id int<collection.CONTEXT>
  • angle float
rotate_about

rotate_about(id, angle, x, y)

Rotates the transformation matrix around the point.

Arguments

  • id int<collection.CONTEXT>
  • angle float
  • x float
  • y float
scale

scale(id, x, y)

Scales the transformation matrix by a factor.

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
scale_about

scale_about(id, sx, sy, x, y)

Scales the transformation matrix by a factor (sx,sy) starting at the point (x,y).

Arguments

  • id int<collection.CONTEXT>
  • sx float
  • sy float
  • x float
  • y float
color_hex

color_hex(id, hex)

Supports hex colors in the follow formats: #RGB #RRGGBB #RRGGBBAA. The leading # is optional.

Arguments

  • id int<collection.CONTEXT>
  • hex string
color

color(id, color)

Arguments

  • id int<collection.CONTEXT>
  • color struct<image.Color>
color_rgb

color_rgb(id, r, g, b)

Float values for r,g,b between 0 and 1.

Arguments

  • id int<collection.CONTEXT>
  • r float
  • g float
  • b float
color_rgb255

color_rgb255(id, r, g, b)

Interger values for r,g,b between 0 and 255.

Arguments

  • id int<collection.CONTEXT>
  • r int
  • g int
  • b int
color_rgba

color_rgba(id, r, g, b, a)

Float values for r,g,b,a between 0 and 1.

Arguments

  • id int<collection.CONTEXT>
  • r float
  • g float
  • b float
  • a float
color_rgba255

color_rgba255(id, r, g, b, a)

Interger values for r,g,b,a between 0 and 255.

Arguments

  • id int<collection.CONTEXT>
  • r int
  • g int
  • b int
  • a int
dash_set

dash_set(id, pattern)

Sets the dash pattern to use. Call with empty array to disable dashes.

Arguments

  • id int<collection.CONTEXT>
  • pattern []float - Each float value is a dash with a length of the value.
dash_set_offset

dash_set_offset(id, offset)

The initial offset for the dash pattern.

Arguments

  • id int<collection.CONTEXT>
  • offset float
line_cap

line_cap(id, cap)

Arguments

  • id int<collection.CONTEXT>
  • cap int<context.LineCap>
line_join

line_join(id, join)

Arguments

  • id int<collection.CONTEXT>
  • join int<context.LineJoin>
line_width

line_width(id, width)

Arguments

  • id int<collection.CONTEXT>
  • width float
pixel_set

pixel_set(id, x, y)

Sets a pixel to the current color.

Arguments

  • id int<collection.CONTEXT>
  • x int
  • y int
shear

shear(id, x, y)

Updates the current matrix with a shearing angle, at the origin.

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
shear_about

shear_about(id, sx, sy, x, y)

Updates the current matrix with a shearing angle (sx,sy), at the given point (x,y).

Arguments

  • id int<collection.CONTEXT>
  • sx float
  • sy float
  • x float
  • y float
translate

translate(id, x, y)

Updates the current matrix with a translation.

Arguments

  • id int<collection.CONTEXT>
  • x float
  • y float
word_wrap

word_wrap(id, str, width) -> []string

@blocking
Arguments

  • id int<collection.CONTEXT>
  • str string
  • width float
Return Values

  • []string - The original string split into line breaks at the set width.
matrix_new

matrix_new(xx, yx, xy, yy, x0, y0) -> struct<context.Matrix>

Arguments

  • xx float
  • yx float
  • xy float
  • yy float
  • x0 float
  • y0 float
Return Values

  • struct<context.Matrix>
matrix_identity

matrix_identity() -> struct<context.Matrix>

Return Values

  • struct<context.Matrix>
matrix_rotate

matrix_rotate(angle) -> struct<context.Matrix>

Arguments

  • angle float
Return Values

  • struct<context.Matrix>
matrix_scale

matrix_scale(x, y) -> struct<context.Matrix>

Arguments

  • x float
  • y float
Return Values

  • struct<context.Matrix>
matrix_shear

matrix_shear(x, y) -> struct<context.Matrix>

Arguments

  • x float
  • y float
Return Values

  • struct<context.Matrix>
matrix_translate

matrix_translate(x, y) -> struct<context.Matrix>

Arguments

  • x float
  • y float
Return Values

  • struct<context.Matrix>
point_cubic_bezier

point_cubic_bezier(x0, y0, x1, y1, x2, y2, x3, y3) -> []struct<context.Point>

Arguments

  • x0 float
  • y0 float
  • x1 float
  • y1 float
  • x2 float
  • y2 float
  • x3 float
  • y3 float
Return Values

  • []struct<context.Point>
point_quadratic_bezier

point_quadratic_bezier(x0, y0, x1, y1, x2, y2) -> []struct<context.Point>

Arguments

  • x0 float
  • y0 float
  • x1 float
  • y1 float
  • x2 float
  • y2 float
Return Values

  • []struct<context.Point>
pattern_solid

pattern_solid(color) -> struct<context.PatternSolid>

Arguments

  • color struct<image.Color>
Return Values

  • struct<context.PatternSolid>
pattern_surface

pattern_surface(id, repeat_op) -> struct<context.PatternSurface>

Images are only grabbed when the pattern is set, not when this is called.

Arguments

  • id int<collection.IMAGE>
  • repeat_op int<context.RepeatOp>
Return Values

  • struct<context.PatternSurface>
pattern_surface_sync

pattern_surface_sync(id, repeat_op) -> struct<context.PatternSurfaceSync>

This does not wait for the image to be ready or idle, if the image is not loaded it will grab an empy image. This also means it is not thread safe, it is unknown what state the image will be in when grabbed. Images are also only grabbed when the pattern is set, not when this is called.

Arguments

  • id int<collection.IMAGE>
  • repeat_op int<context.RepeatOp>
Return Values

  • struct<context.PatternSurfaceSync>
pattern_custom

pattern_custom(fn) -> struct<context.PatternCustom>

Arguments

  • fn function(x int, y int) -> struct<image.Color>
Return Values

  • struct<context.PatternCustom>
gradient_linear

gradient_linear(x0, y0, x1, y1) -> struct<context.PatternGradientLinear>

Arguments

  • x0 float
  • y0 float
  • x1 float
  • y1 float
Return Values

  • struct<context.PatternGradientLinear>
gradient_radial

gradient_radial(x0, y0, r0, x1, y1, r1) -> struct<context.PatternGradientRadial>

Arguments

  • x0 float
  • y0 float
  • r0 float
  • x1 float
  • y1 float
  • r1 float
Return Values

  • struct<context.PatternGradientRadial>
fill_style

fill_style(id, pattern)

Arguments

  • id int<collection.CONTEXT>
  • pattern struct<context.Pattern>
stroke_style

stroke_style(id, pattern)

Arguments

  • id int<collection.CONTEXT>
  • pattern struct<context.Pattern>
font_load

font_load(path, points)

Arguments

  • path string
  • points float

Constants


FillRule
  • FILLRULE_WINDING
  • FILLRULE_EVENODD
LineCap
  • LINECAP_ROUND
  • LINECAP_BUTT
  • LINECAP_SQUARE
LineJoin
  • LINEJOIN_ROUND
  • LINEJOIN_BEVEL
RepeatOp
  • REPEAT_BOTH
  • REPEAT_X
  • REPEAT_Y
  • REPEAT_NONE
Align
  • ALIGN_LEFT
  • ALIGN_CENTER
  • ALIGN_RIGHT
PatternType
  • PATTERN_SOLID
  • PATTERN_SURFACE
  • PATTERN_SURFACE_SYNC
  • PATTERN_GRADIENT_LINEAR
  • PATTERN_GRADIENT_RADIAL
  • PATTERN_CUSTOM

Structs


Point

Properties

  • x float
  • y float
Matrix

Properties

  • xx float
  • yx float
  • xy float
  • yy float
  • x0 float
  • y0 float
Methods

multiply

multiply(self, struct<context.Matrix>) -> self

rotate

rotate(self, angle float) -> self

scale

scale(self, x float, y float) -> self

shear

shear(self, x float, y float) -> self

translate

translate(self, x float, y float) -> self

transform_point

transform_point(x float, y float) -> float, float

transform_vector

transform_vector(x float, y float) -> float, float

PatternSolid

Properties

  • type string<context.PatternType>
  • color struct<image.Color>
PatternSurface

Properties

  • type string<context.PatternType>
  • id int<collection.IMAGE>
  • repeatOp int<context.RepeatOp>
PatternSurfaceSync

Properties

  • type string<context.PatternType>
  • id int<collection.IMAGE>
  • repeatOp int<context.RepeatOp>
PatternGradientLinear

Properties

  • type string<context.PatternType>
  • x0 float
  • y0 float
  • x1 float
  • y1 float
Methods

color_stop

color_stop(offset float, struct<image.Color>)

PatternGradientRadial

Properties

  • type string<context.PatternType>
  • x0 float
  • y0 float
  • r0 float
  • x1 float
  • y1 float
  • r1 float
Methods

color_stop

color_stop(offset float, struct<image.Color>)

PatternCustom

Properties

  • type string<context.PatternType>
  • fn function(x int, y int) -> struct<image.ColorRGBA>

Interfaces


Pattern

Properties

  • type string<context.PatternType>