veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

ui/canvas

lib/ui/canvas.vk

lib/ui/canvas.vk Pure-Veko 100% safe RGBA32 2D software rasterizer, built-in 5x7 ASCII bitmap font, alpha compositor, and zero-dependency BMP / PPM image encoders. Zero capabilities required; works identically on VM, Wasm 3.0, and Native.

Color struct
signature
Color = struct { r i64, g i64, b i64, a i64 = ... }

8-bit RGBA color value.

rgba fn
signature
rgba = fn [r i64, g i64, b i64, a i64] Color

Constructs an RGBA Color with components clamped to 0..255.

Executable Example
veko
use [cv = ui/canvas]
c = cv/rgba 34 211 238 255
if c.r != 34 or c.g != 211 or c.b != 238 or c.a != 255 [ err 'rgba' ] else [ .ok ]
rgb fn
signature
rgb = fn [r i64, g i64, b i64] Color

Constructs an opaque RGB Color (a = 255).

signature
color-bg-dark = fn [] Color

Built-in modern UI palette constructors.

signature
color-surface = fn [] Color
signature
color-border = fn [] Color
signature
color-text-primary = fn [] Color
signature
color-text-muted = fn [] Color
signature
color-cyan = fn [] Color
signature
color-emerald = fn [] Color
signature
color-amber = fn [] Color
signature
color-rose = fn [] Color
Canvas struct
signature
Canvas = struct { width i64, height i64, pixels bytes, clip_on bool = ..., clip_x i64 = ..., clip_y i64 = ..., clip_w i64 = ..., clip_h i64 = ... }

Offscreen RGBA32 pixel surface (width * height * 4 bytes, top-down row-major) with optional clip rect.

signature
set-clip = fn [mut cv Canvas, x i64, y i64, w i64, h i64]

Sets an active clipping rectangle (x, y, w, h) on cv (clamped to canvas bounds).

signature
reset-clip = fn [mut cv Canvas]

Clears any active clipping rectangle on cv, restoring full-canvas drawing.

signature
new-canvas = fn [w i64, h i64] Canvas

Allocates a new w x h RGBA32 Canvas initialized to transparent black (0, 0, 0, 0).

Executable Example
veko
use [cv = ui/canvas]
mut c = cv/new-canvas 32 16
cv/clear mut c (cv/rgb 10 20 30)
p = cv/get-pixel c 5 5
if p.r != 10 or p.g != 20 or p.b != 30 or p.a != 255 [ err 'clear/get-pixel' ] else [ .ok ]
signature
clear = fn [mut cv Canvas, c Color]

Fills the entire canvas with c in place (zero per-frame heap allocation).

signature
get-pixel = fn [cv Canvas, x i64, y i64] Color

Reads the RGBA Color at (x, y) (returns transparent black if out of bounds).

signature
set-pixel = fn [mut cv Canvas, x i64, y i64, c Color]

Sets or Porter-Duff SRC_OVER alpha-blends pixel (x, y) with c (honoring any active clip rect).

signature
fill-rect = fn [mut cv Canvas, x i64, y i64, w i64, h i64, c Color]

Fills a clipped rectangle (x, y, w, h) with c (supports alpha blending and active clip rect).

signature
stroke-rect = fn [mut cv Canvas, x i64, y i64, w i64, h i64, thickness i64, c Color]

Draws a rectangle outline of thickness pixels.

signature
fill-gradient-v = fn [mut cv Canvas, x i64, y i64, w i64, h i64, top Color, bottom Color]

Fills a vertical linear gradient across (x, y, w, h) from top to bottom.

signature
fill-rounded-rect = fn [mut cv Canvas, x i64, y i64, w i64, h i64, radius i64, c Color]

Fills a rounded rectangle with corner radius.

signature
stroke-rounded-rect = fn [mut cv Canvas, x i64, y i64, w i64, h i64, radius i64, c Color]

Draws a crisp 1px rounded rectangle border matching fill-rounded-rect.

signature
fill-circle = fn [mut cv Canvas, cx i64, cy i64, r i64, c Color]

Draws a filled circle centered at (cx, cy) with radius r.

signature
stroke-circle = fn [mut cv Canvas, cx i64, cy i64, r i64, c Color]

Draws a 1px circle outline centered at (cx, cy) with radius r.

signature
draw-line-smooth = fn [mut cv Canvas, x0 i64, y0 i64, x1 i64, y1 i64, c Color]

Draws an anti-aliased multi-pixel line from (x0, y0) to (x1, y1) with soft fringe.

signature
draw-line = fn [mut cv Canvas, x0 i64, y0 i64, x1 i64, y1 i64, c Color]

Draws an integer Bresenham line from (x0, y0) to (x1, y1).

signature
draw-char = fn [mut cv Canvas, x i64, y i64, ch i64, scale i64, c Color]

Draws a single 5x7 glyph scaled by scale at (x, y).

signature
measure-text-width = fn [text str, scale i64] i64

Measures the pixel width of text rendered at integer scale.

signature
draw-text = fn [mut cv Canvas, x i64, y i64, text str, scale i64, c Color]

Draws ASCII text at (x, y) using the built-in 5x7 bitmap font scaled by scale.

signature
encode-bmp = fn [cv Canvas] bytes

Encodes cv as an uncompressed 24-bit BMP image ('BM' header + bottom-up BGR rows).

Executable Example
veko
use [cv = ui/canvas]
mut c = cv/new-canvas 8 4
cv/clear mut c (cv/color-cyan)
bmp = cv/encode-bmp c
if bmp[0] != 66 or bmp[1] != 77 or (len bmp) != 150 [ err 'bad bmp' ] else [ .ok ]
signature
encode-ppm = fn [cv Canvas] bytes

Encodes cv as a binary Netpbm PPM (P6) RGB24 image.

Executable Example
veko
use [cv = ui/canvas]
mut c = cv/new-canvas 4 2
cv/clear mut c (cv/rgb 255 0 128)
ppm = cv/encode-ppm c
if ppm[0] != 80 or ppm[1] != 54 [ err 'bad ppm' ] else [ .ok ]