wolfhacklua API 1.1 Forum

draw

Immediate-mode 2D drawing: lines, shapes, text, images, gradients, meshes and shaders.

since 1.0no permission needed

draw.* works only inside a draw event handler (screen coordinates), a widget's draw function (widget coordinates, clipped to the widget) and a ui.canvas callback (canvas coordinates, clipped). Anywhere else it raises "can only be called while drawing". Coordinates are pixels with (0, 0) at the top left. Colours are 0xRRGGBBAA.

A script may issue 12 000 draw calls a frame; the rest are dropped. A draw handler that issues a few hundred calls is cheap. See drawing.

Reference#

draw.line#since 1.0

draw.line(x1: number, y1: number, x2: number, y2: number, col: integer, thickness?: number)

A straight line.

ParameterTypeDescription
x1numberStart x.
y1numberStart y.
x2numberEnd x.
y2numberEnd y.
colintegerColour.
thickness optionalnumberLine width in pixels. default 1

draw.rect#since 1.0

draw.rect(x: number, y: number, w: number, h: number, col: integer, rounding?: number)

A filled rectangle.

ParameterTypeDescription
xnumberLeft.
ynumberTop.
wnumberWidth.
hnumberHeight.
colintegerColour.
rounding optionalnumberCorner radius. default 0
lua
draw.rect(20, 20, 200, 40, 0x101418E0, 8)

draw.rect_outline#since 1.0

draw.rect_outline(x: number, y: number, w: number, h: number, col: integer, thickness?: number, rounding?: number)

A rectangle outline.

ParameterTypeDescription
xnumberLeft.
ynumberTop.
wnumberWidth.
hnumberHeight.
colintegerColour.
thickness optionalnumberLine width. default 1
rounding optionalnumberCorner radius. default 0

draw.rect_gradient#since 1.0

draw.rect_gradient(x: number, y: number, w: number, h: number, tl: integer, tr: integer, br: integer, bl: integer)

A rectangle with a colour in each corner, blended across.

ParameterTypeDescription
xnumberLeft.
ynumberTop.
wnumberWidth.
hnumberHeight.
tlintegerTop-left colour.
trintegerTop-right colour.
brintegerBottom-right colour.
blintegerBottom-left colour.
lua
draw.rect_gradient(20, 20, 200, 10, 0xFF0000FF, 0x0000FFFF, 0x0000FFFF, 0xFF0000FF)

draw.circle#since 1.0

draw.circle(x: number, y: number, r: number, col: integer, filled?: boolean, thickness?: number, segments?: integer)

A circle.

ParameterTypeDescription
xnumberCentre x.
ynumberCentre y.
rnumberRadius.
colintegerColour.
filled optionalbooleanFill the circle. default true
thickness optionalnumberOutline width when not filled. default 1
segments optionalintegerSmoothness. default automatic

draw.arc#since 1.0

draw.arc(x: number, y: number, r: number, a0: number, a1: number, col: integer, thickness?: number)

Part of a circle's outline.

ParameterTypeDescription
xnumberCentre x.
ynumberCentre y.
rnumberRadius.
a0numberStart angle in radians.
a1numberEnd angle in radians.
colintegerColour.
thickness optionalnumberLine width. default 1
lua
draw.arc(100, 100, 30, 0, math.pi * 1.5, 0xFFFFFFFF, 3)

draw.triangle#since 1.0

draw.triangle(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, col: integer, filled?: boolean, thickness?: number)

A triangle.

ParameterTypeDescription
x1numberFirst corner x.
y1numberFirst corner y.
x2numberSecond x.
y2numberSecond y.
x3numberThird x.
y3numberThird y.
colintegerColour.
filled optionalbooleanFill it. default true
thickness optionalnumberOutline width. default 1

draw.polyline#since 1.0

draw.polyline(points: number[][] | number[], col: integer, thickness?: number, closed?: boolean)

Connected line segments.

ParameterTypeDescription
pointsnumber[][] | number[]{{x, y}, ...} or a flat {x1, y1, x2, y2, ...}; at most 512 points.
colintegerColour.
thickness optionalnumberLine width. default 1
closed optionalbooleanJoin the last point to the first. default false

draw.polygon#since 1.0

draw.polygon(points: number[][] | number[], col: integer)

A filled convex polygon.

ParameterTypeDescription
pointsnumber[][] | number[]The corners, at most 512. The shape must be convex.
colintegerColour.

draw.bezier#since 1.0

draw.bezier(x1: number, y1: number, c1x: number, c1y: number, c2x: number, c2y: number, x2: number, y2: number, col: integer, thickness?: number)

A cubic Bézier curve.

ParameterTypeDescription
x1numberStart x.
y1numberStart y.
c1xnumberFirst control point x.
c1ynumberFirst control point y.
c2xnumberSecond control point x.
c2ynumberSecond control point y.
x2numberEnd x.
y2numberEnd y.
colintegerColour.
thickness optionalnumberLine width. default 1

draw.text#since 1.0

draw.text(x: number, y: number, col: integer, text: string, options?: table)

Draws text. Note the argument order: the colour comes before the text.

ParameterTypeDescription
xnumberAnchor x.
ynumberTop y.
colintegerColour.
textstringThe text.
options optionaltable{font, size, shadow, outline, align}. font is a Font; size is in pixels; shadow and outline are booleans; align is "left", "center" or "right", relative to x.
lua
draw.text(100, 50, 0xFFFFFFFF, "hello", { size = 18, shadow = true })

draw.text_size#since 1.0

draw.text_size(text: string, options?: table): number, number

Measures text so you can size a background around it.

ParameterTypeDescription
textstringThe text.
options optionaltableThe same options as draw.text.
ReturnsDescription
numberWidth.
numberHeight.
lua
local w, h = draw.text_size("hello", { size = 16 })

draw.clip#since 1.0

draw.clip(x: number, y: number, w: number, h: number)

Limits drawing to a rectangle until draw.unclip.

ParameterTypeDescription
xnumberLeft.
ynumberTop.
wnumberWidth.
hnumberHeight.

draw.unclip#since 1.0

draw.unclip()

Removes the clip made by draw.clip.

draw.alpha#since 1.0

draw.alpha(a: number)

Multiplies the opacity of everything drawn after it. Use it to fade a whole panel.

ParameterTypeDescription
anumberOpacity multiplier, 0 to 1.

draw.size#since 1.0

draw.size(): number, number

The size of what you are drawing into: the screen in a draw handler, the widget in a widget, the canvas in a canvas.

ReturnsDescription
numberWidth of the drawing area.
numberHeight.

draw.image#since 1.0

draw.image(tex: Texture, x: number, y: number, w: number, h: number, options?: table)

Draws a texture. A texture that has not finished loading draws nothing; check tex:ready() or task.await it.

ParameterTypeDescription
texTextureA texture.
xnumberLeft.
ynumberTop.
wnumberWidth.
hnumberHeight.
options optionaltable{tint, rounding, rotation, uv}: a colour tint, a corner radius, a rotation in radians, and uv = {u0, v0, u1, v1} to draw part of the picture.

draw.model#since 1.0

draw.model(x: number, y: number, w: number, h: number, options?: table)

Draws the built-in player model. It renders on a worker thread and the pictures are cached.

ParameterTypeDescription
xnumberLeft.
ynumberTop.
wnumberWidth.
hnumberHeight.
options optionaltable{yaw = degrees, team = "ct" | "t", tint}.

draw.mesh#since 1.0

draw.mesh(vertices: number[], indices: integer[], options?: table)

Draws a small 3D mesh, turned, perspective projected and flat shaded in software.

ParameterTypeDescription
verticesnumber[]Flat {x, y, z, x, y, z, ...}.
indicesinteger[]1-based vertex index triples; at most 4096 triangles.
options optionaltable{x, y, scale, yaw, pitch, roll, distance, color, light, wire}.

draw.shader_rect#since 1.0

draw.shader_rect(shader: Shader, x: number, y: number, w: number, h: number, params?: number[])

Fills a rectangle with a pixel shader. A shader that is not ready yet draws nothing.

ParameterTypeDescription
shaderShaderA shader made with assets.shader.
xnumberLeft.
ynumberTop.
wnumberWidth.
hnumberHeight.
params optionalnumber[]Up to 16 numbers, available in the shader as u_p[0..3].
API 1.1 · source docs-src/api/draw.json