Canvas
const canvas = require("@vectarine/canvas")Functions to create canvas and shaders A canvas is a drawing surface and a shader is a program that changes how a canvas is drawn to another drawing surface.
Functions
Section titled “Functions”canvas.createCanvas
Section titled “canvas.createCanvas”canvas.createCanvas(width: number, height: number): CanvasCreate a new canvas of given width and height The canvas is empty and fully transparent by default Creating a canvas is an expensive operation! For performance reasons, you shouldn’t create canvases every frame!
ShaderResource
Section titled “ShaderResource”export type ShaderResource = ...Canvas
Section titled “Canvas”export type Canvas = ...Canvas:paint
Section titled “Canvas:paint”Canvas:paint(draw_function: () -> ()): ()Paint on the canvas.
All function from graphics will draw to this canvas instead of the screen until this function returns.
You can nest calls to paint, in which case the innermost paint is the one that receives the draw calls.
The canvas uses the same coordinate system as the screen with (-1,-1) being the bottom-left, (0,0) the center and (1,1) the top-right.
Once you have finished painting the canvas, you can use graphics.drawCanvas to draw it on the screen or on another canvas.
Canvas:setShader
Section titled “Canvas:setShader”Canvas:setShader(shader: ShaderResource?): ()When the canvas is drawn, instead of being drawn as-is, it will be used as a texture input to the shader that
you provide. The output of the shader is what will be drawn. This can be used for post-processing effects, outlines, recoloring, etc.
To disable the shader, call setShader(nil).
You can see examples of shaders on ShaderToy
Canvas:setUniform
Section titled “Canvas:setUniform”Canvas:setUniform(name: string, value: number | vec.Vec2 | vec4.Vec4): ()Sets a uniform float value for the shader used when drawing this canvas.
Canvas:getSize
Section titled “Canvas:getSize”Canvas:getSize(): vec.Vec2Gets the size of the canvas or image in pixels
Canvas:draw
Section titled “Canvas:draw”Canvas:draw(pos: vec.Vec2, size: Direction): ()Draws a canvas at given position and size
Canvas:drawPart
Section titled “Canvas:drawPart”Canvas:drawPart( dest_p1: Pos, dest_p2: Pos, dest_p3: Pos, dest_p4: Pos, src_pos: vec.Vec2, src_size: vec.Vec2,): ()Draws a part canvas stretched to fit the quadrilateral delimited by the 4 destination points
This is the same as drawImagePart but for canvases
type Pos = ...Direction
Section titled “Direction”type Direction = ...