Skip to content

Using the LinesCanvas Element

The lines-canvas element is a Web Component that renders generative line art animations.

import { LinesCanvas } from 'generative-lines'
customElements.define('lines-canvas', LinesCanvas)
<lines-canvas
steps="12"
colors="3"
distance="12"
amplitude="32"
thickness="2"
lines="12"
padding-x="20"
padding-y="20"
perspective="-0.02"
easing="Cubic.InOut"
background="transparent"
animation-duration="0"
animation-easing="Cubic.InOut"
line-cap="round"
line-join="round"
autoplay="true"
/>
AttributeTypeDefaultDescription
stepsint12Number of steps in the line
colorsint3Number of gradient colors
distanceint12Vertical distance between lines
amplitudeint32Maximum Y variation
thicknessint2Line stroke width
linesint12Number of parallel lines
padding-xint20Horizontal padding
padding-yint20Vertical padding
perspectivefloat-0.02Perspective scaling factor
easingtween.js easingCubic.InOutEasing function (tween.js ref)
backgroundstringtransparentBackground color (CSSColor, MDN ref)
animation-durationint1000Animation duration in ms
animation-easingtween.js easingCubic.InOut Animation easing function (tween.js ref)
line-capbutt, round, squareroundLine end cap style (MDN ref)
line-joinround, bevel, miterroundLine join style (MDN ref)
autoplaytrue, loop, falsefalseAnimation mode
tween-groupstringAutoplayTweenGroupTween group for animation control (ref)

The tween-group attribute allows you to control which tween group manages the animation:

<lines-canvas
tween-group="custom-group"
autoplay="loop"
/>

Multiple elements can share the same tween group to synchronize animations.

Plays a single animation from left to right on mount. Dispatches animated-in event when complete.

<lines-canvas
autoplay="true"
id="myCanvas"
></lines-canvas>
<script>
document.getElementById('myCanvas').addEventListener('animated-in', () => {
console.log('Animation finished')
})
</script>

Continuously loops the animation with hold periods.

<lines-canvas autoplay="loop"></lines-canvas>

No animation. Lines are drawn statically.

<lines-canvas autoplay="false"></lines-canvas>
const element = document.querySelector('lines-canvas')
PropertyTypeDescription
canvasHTMLCanvasElementThe underlying canvas element
contextCanvasRenderingContext2DThe 2D rendering context
rendererRendererThe renderer controller (ref)
isMountedbooleanWhether the element is connected to DOM

Batch multiple attribute changes into a single reconfiguration. Multiple attribute changes will be combined into one configuration update:

element.startBatchUpdate()
element.setAttribute('steps', '20')
element.setAttribute('colors', '5')
element.setAttribute('distance', '10')
element.endBatchUpdate()

Access the current configuration and state via getters:

const config = element.renderer.config
const state = element.renderer.state

Replace running animation tweens with a new configuration:

// Replace tweens with a custom configuration
renderer.replaceTweens(/* new tweens */)

Note: Changing animationDuration or animationEasing config values automatically updates running tweens.

Reroll random state for lines or colors:

renderer.rerollLines() // Generate new line positions
renderer.rerollColors() // Generate new colors

Capture the current canvas state as a data URL:

const imageData = renderer.captureImage() // returns PNG data URL

The renderer provides additional control:

const renderer = element.renderer
// Animate
await renderer.animateIn()
await renderer.animateBackOut()
await renderer.animateWipeOut()
renderer.animateLoop()
// Update configuration
renderer.mergeConfig({ steps: 20, colors: 5 })
// Reroll random state
renderer.rerollLines()
renderer.rerollColors()
// Capture image
const imageData = renderer.captureImage() // returns data URL

Changing attributes after mount automatically updates the rendering:

const element = document.querySelector('lines-canvas')
// These will trigger automatic updates
element.setAttribute('colors', '5')
element.setAttribute('thickness', '4')
element.setAttribute('easing', 'Quad.InOut')

The canvas automatically fills its container, unless specified otherwise:

lines-canvas {
display: block;
width: 500px;
height: 500px;
}
<lines-canvas
autoplay="false"
lines="8"
thickness="3"
colors="4"
background="#1a1a2e"
>
</lines-canvas>
<lines-canvas
autoplay="loop"
animation-duration="2000"
animation-easing="Quad.InOut"
steps="16"
amplitude="48"
>
</lines-canvas>
<lines-canvas id="example" autoplay="true"></lines-canvas>
<script>
const linesCanvas = document.getElementById('example')
linesCanvas.addEventListener(
'animated-in',
() => {
linesCanvas.renderer.animateWipeOut()
},
{ once: true }
)
</script>