Renderer
The renderer is responsible for rendering shapes and text on the screen. It has a very simple API:
TypeScript
export interface Renderer {
rectangle(
color: Vec4,
position: Vec2,
size: Vec2,
corners: Vec4,
borderWidth: Vec4,
borderColor: Vec4,
clipStart: Vec2,
clipSize: Vec2,
clipCorners: Vec4,
): void;
render(commandEncoder: GPUCommandEncoder): void;
text(
text: string,
position: Vec2,
fontName: string,
fontSize: number,
color: Vec4,
textAlignment: TextAlign,
clipStart: Vec2,
clipSize: Vec2,
options?: { lineHeight?: number; maxWidth?: number },
): void;
}
Overview
For each rectangle on the screen, an instance of a full-screen quad is renderered, which is then assigned to proper positions based on data stored in storage buffer. Fragment shader uses SDF to calculate border radius.
API
interface
Renderer
/renderer/Renderer.ts ↗
class
CanvasRenderer
/renderer/CanvasRenderer.ts ↗
method
rectangle
Type declaration
TypeScript
(color: Vec4, position: Vec2, size: Vec2, corners: Vec4, borderWidth: Vec4, borderColor: Vec4, clipStart: Vec2, clipSize: Vec2, clipCorners: Vec4) => void
method
text
Type declaration
TypeScript
(text: string, position: Vec2, fontName: string, fontSize: number, color: Vec4, textAlignment: TextAlign, clipStart: Vec2, clipSize: Vec2, options?: { ...; }) => void
class
WebGLRenderer
/renderer/WebGLRenderer.ts ↗
method
rectangle
Type declaration
TypeScript
(color: Vec4, position: Vec2, size: Vec2, corners: Vec4, borderWidth: Vec4, borderColor: Vec4, clipStart: Vec2, clipSize: Vec2, clipCorners: Vec4) => void
method
text
Type declaration
TypeScript
(text: string, position: Vec2, fontName: string, fontSize: number, color: Vec4, textAlign: TextAlign, clipStart: Vec2, clipSize: Vec2, options?: { ...; }) => void
class
WebGPURenderer
/renderer/WebGPURenderer.ts ↗
method
rectangle
Type declaration
TypeScript
(color: Vec4, position: Vec2, size: Vec2, corners: Vec4, borderWidth: Vec4, borderColor: Vec4, clipStart: Vec2, clipSize: Vec2, clipCorners: Vec4) => void
method
text
Type declaration
TypeScript
(text: string, position: Vec2, fontName: string, fontSize: number, color: Vec4, textAlign: TextAlign, clipStart: Vec2, clipSize: Vec2, options?: { ...; }) => void
Text rendering
Text uses SDF font rendering to display glyphs from a font atlas. See Text Rendering page for more information.
I wrote a blogpost that goes more in depth – Drawing Text in WebGPU Using Just the Font File.