Skip to content

Graphics

const graphics = require("@vectarine/graphics")

Functions to draw shapes, images, and canvases to the screen.

Uses Vec or Coord types for position and directions.

graphics.clear(color: Vec4.Vec4): ()

Clear the canvas

graphics.drawRect(pos: Pos, size: Direction, color: Vec4.Vec4?): ()

Draws a filled rectangle at the position pos with the size size If color is not provided, it defaults to black

Graphics.drawRect(Vec.V2(-0.05, -0.05), Vec.V2(0.1, 0.1), Vec4.RED)
graphics.drawPolygon(points: { Pos }, color: Vec4.Vec4?): ()

Draws a convex filled polygon

graphics.drawLine(p1: Pos, p2: Pos, color: Vec4.Vec4?, thickness: number?): ()

Draws a line from p1 to p2 If color is not provided, it defaults to black If thickness is not provided, it defaults to 0.005

-- Draw a line from the center of the screen to the top right
Graphics.drawLine(Coord.CENTER, Coord.TOP_RIGHT)
graphics.drawArrow(pos: Pos, direction: Direction, color: Vec4.Vec4?, size: number?): ()

Draws an arrow starting at pos, and towards direction You can use it to show the speed of an entity for example

graphics.drawCircle(center: Pos, radius: number, color: Vec4.Vec4?): ()

Draws a filled circle

graphics.drawEllipse(center: Pos, size: Direction, color: Vec4.Vec4?): ()

Draws an ellipse contained in the rectangle defined by center and size * 2

graphics.drawWithMask(drawFunction: () -> (), maskFunction: () -> ()): ()

Runs drawFunction using the result of the maskFunction as a mask. drawFunction will only be able to draw in places where maskFunction drew. The output of maskFunction is not visible directly and the colors used by the mask do not matter.

This can be useful to crop images or scenes to specific shapes like a circle.

graphics.drawSplashScreen(loadingText: string?, progress: number?): ()

Show a splash screen over the whole screen. This is useful to show a loading animation while the game is loading. You can configure your splash screen using the loading_animation field in the .vecta manifest. Optionally, you can pass some text to show during loading and a number between 0 and 1 to show a progress bar.

graphics.drawSplashScreenIfNeeded(
resourcesToWaitFor: { Resource.Resource },
loadingText: string?,
): boolean

Show a splash screen if any of the resources are not ready. A progress bar is automatically shown based on the number of resources needed. Optionally, you can pass some text to show during loading. Returns true if the splash screen is shown. You can use it like this:

function Update()
if module.showSplashScreenIfNeeded({
resource1, resource2, resource3
}, "Starting...") then
return
end
-- Your game code requiring the resources here
end
graphics.withTransformation(
transformation: { translation: Vec.Vec2?, scale: Vec.Vec2?, rotation: number? },
drawFunction: () -> (),
): ()

All drawing function called inside withTransformation will be transformed by the given translation, scale and rotation. You can use this to rotate or scale things without changing your drawing code. The translation is applied first, then the scale, and finally the rotation. You can nest multiple calls to withTransformation and the transformations will be combined. The transformations are only applied for 2d drawing functions.

This is used internally to implement the UI system.

graphics.withoutTransformation(drawFunction: () -> ()): ()

Reset the transformation to the default inside the drawFunction. You can still call withTransformation inside the drawFunction, this just resets the transformation to the default for the duration of the drawFunction.

graphics.transform(pos: Vec.Vec2): Vec.Vec2
graphics.inverseTransform(pos: Vec.Vec2): Vec.Vec2
graphics.getCurrentTransform(): (Vec.Vec2, Vec.Vec2, number)

Return the current translation, scale and rotation of the current transformation. If you have nested transformations, it returns the combined transformation.

type Pos = ...
type Direction = ...