Skip to content

Using the Renderer

The renderer module provides a canvas-based animation system for rendering generative line art. It wraps the Canvas 2D API with a modular controller architecture that handles configuration, drawing, animation, and state management.

example source code

import { useRenderer } from 'generative-lines'
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
const renderer = useRenderer(canvas, context)
renderer.initialize({
steps: 10,
colors: 3,
})
// Run a single animation
await renderer.animateIn()
// Or run a continuous loop
renderer.animateLoop()

Factory function that returns a renderer controller.

Parameters:

NameTypeDescription
canvasHTMLCanvasElementCanvas element to draw on
contextCanvasRenderingContext2D2D rendering context
interface Renderer {
// Getters/Setters
config: Config
state: RenderState
// Initialization
initialize(rawConfig: RawConfig, incomingState?: Partial<RenderState>): void
// Configuration Updates
mergeConfig(newConfig: Partial<Config>): void
mergeState(newState: Partial<RenderState>): void
// State Manipulation
rerollLines(): void
rerollColors(): void
// Rendering
drawFull(): void
clearCanvas(): void
captureImage(): string
drawSegment(from: number, to: number): void
getCurrentSegment(): [from: number, to: number]
// Animation Control
replaceTweens(...tweens: Tween[]): void
// Animation
animate(animationConfig: AnimationConfig): Promise<void>
animateIn(): Promise<void>
animateBackOut(): Promise<void>
animateWipeOut(): Promise<void>
animateLoop(
holdAfterIn?: Milliseconds,
holdAfterOut?: Milliseconds,
inConfig?: AnimationConfig,
outConfig?: AnimationConfig
): void
}

Simple animations tween from a start rendering range to an end rendering range:

MethodDescriptionTween Range
animateIn()Draw lines from left to right[0, 0] → [0, 1]
animateBackOut()Erase lines from right to left[0, 1] → [0, 0]
animateWipeOut()Erase lines from left to right[0, 1] → [1, 1]
animateLoop()Continuous loop with hold periods and config[0,0]→[0,1]→[1,1]→[0,0]...
animate(AnimationConfig)Whatever ranges you likeExample:
{ from: [0, 0.5], to: [0.5, 1] }
See AnimationConfig

When updating config via mergeConfig(), the renderer determines what needs to be rebuilt:

Config ChangeAction
background, line-cap, line-joinRestyle lines
thicknessRestyle + rebuild lines
colorsReroll colors + restyle
stepsReroll lines + rebuild
distance, amplitude, paddingX/Y, etc.Rebuild lines only
animationDuration, animationEasingChanges running animation