ui/canvas
lib/ui/canvas.vklib/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.
rgba = fn [r i64, g i64, b i64, a i64] Color
Constructs an RGBA Color with components clamped to 0..255.
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 ]
color-surface = fn [] Color
color-border = fn [] Color
color-text-primary = fn [] Color
color-text-muted = fn [] Color
color-cyan = fn [] Color
color-emerald = fn [] Color
color-amber = fn [] Color
color-rose = fn [] Color
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.
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).
reset-clip = fn [mut cv Canvas]
Clears any active clipping rectangle on cv, restoring full-canvas drawing.
new-canvas = fn [w i64, h i64] Canvas
Allocates a new w x h RGBA32 Canvas initialized to transparent black (0, 0, 0, 0).
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 ]
clear = fn [mut cv Canvas, c Color]
Fills the entire canvas with c in place (zero per-frame heap allocation).
get-pixel = fn [cv Canvas, x i64, y i64] Color
Reads the RGBA Color at (x, y) (returns transparent black if out of bounds).
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).
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).
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.
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.
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.
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.
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.
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.
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.
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).
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).
measure-text-width = fn [text str, scale i64] i64
Measures the pixel width of text rendered at integer scale.
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.
encode-bmp = fn [cv Canvas] bytes
Encodes cv as an uncompressed 24-bit BMP image ('BM' header + bottom-up BGR rows).
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 ]
encode-ppm = fn [cv Canvas] bytes
Encodes cv as a binary Netpbm PPM (P6) RGB24 image.
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 ]