This guide will help you get up and running with Vivid for creating mathematical visualizations.
npm install vivid-animationsyarn add vivid-animations<script type="module">
import { Scene, Circle, Create } from 'https://unpkg.com/vivid-animations/dist/index.js'
// Your code here
</script>Let's create a simple animation with a circle:
<!DOCTYPE html>
<html>
<head>
<title>My First Vivid Animation</title>
<style>
canvas {
border: 1px solid #ccc;
display: block;
margin: 20px auto;
}
</style>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
<script type="module" src="main.js"></script>
</body>
</html>// main.js
import { Scene, Circle, Create } from 'vivid-animations'
// Get the canvas element
const canvas = document.getElementById('canvas') as HTMLCanvasElement
const scene = new Scene(canvas)
// Create a circle
const circle = new Circle({
radius: 1,
color: { r: 0.2, g: 0.6, b: 1, a: 1 }
})
// Animate the circle appearing
scene.play(new Create(circle))Open the HTML file in a web browser and you'll see a blue circle animate into view!
The Scene is the main container that manages all objects and handles rendering:
const scene = new Scene(canvas)
// Add objects to the scene
scene.add(circle)
// Play animations
scene.play(animation)
// Play multiple animations
scene.play([animation1, animation2])Objects are things that can be drawn on screen. All objects implement the VividObject interface:
// Basic shapes
const circle = new Circle({ radius: 1 })
const rect = new Rectangle({ width: 2, height: 1 })
const line = new Line({ start: { x: -1, y: 0 }, end: { x: 1, y: 0 } })
// Text
const text = new Text('Hello Vivid!')
// Mathematical objects
const axes = new Axes({ xRange: [-5, 5], yRange: [-3, 3] })
const func = new FunctionPlot({ func: x => x * x })Animations modify objects over time:
// Basic animations
new Create(object) // Scale from 0 to full size
new FadeIn(object) // Fade in opacity
new FadeOut(object) // Fade out opacity
// Transform animations
new Transform(object, {
transform: {
translate: { x: 2, y: 1 },
scale: { x: 1.5, y: 1.5 },
rotate: Math.PI / 4
}
})
// Text animations
new DrawText(text) // Draw text character by character
new EraseText(text) // Erase text character by characterObjects can be positioned using several methods:
// Absolute positioning
circle.moveTo({ x: 2, y: 1 })
// Relative positioning
circle.shift({ x: 1, y: 0 }) // Move right by 1 unit
// Chaining transformations
circle.moveTo({ x: 0, y: 0 }).scale(1.5).rotate(Math.PI / 4)// 2D axes
const axes = new Axes({
xRange: [-5, 5],
yRange: [-3, 3],
showGrid: true,
gridColor: { r: 0.8, g: 0.8, b: 0.8, a: 0.5 }
})
// 3D axes
const axes3d = new Axes3D({
xRange: [-3, 3],
yRange: [-3, 3],
zRange: [-3, 3]
})// Basic function
const parabola = new FunctionPlot({
func: x => x * x,
xRange: [-3, 3],
color: { r: 1, g: 0.5, b: 0, a: 1 }
})
// Trigonometric function
const sine = new FunctionPlot({
func: Math.sin,
xRange: [-2 * Math.PI, 2 * Math.PI],
resolution: 100
})
// Custom function
const custom = new FunctionPlot({
func: x => Math.exp(-x*x) * Math.cos(5*x),
xRange: [-3, 3]
})// Basic math notation
const formula = new MathText('f(x) = x^2 + 2x + 1')
// Greek letters and symbols
const equation = new MathText('\\alpha + \\beta = \\gamma')
// Fractions and complex expressions
const complex = new MathText('\\frac{d}{dx} \\int_0^x f(t) dt = f(x)')// 3D vector
const vector = new Vector3D({ x: 1, y: 2, z: 1 })
// 3D surface
const surface = new Surface3D({
func: (x, y) => Math.sin(x) * Math.cos(y),
xRange: [-Math.PI, Math.PI],
yRange: [-Math.PI, Math.PI],
resolution: 50
})// Enable interactive controls (mouse/touch)
scene.camera3D.enableControls(true)
// Programmatic camera control
scene.camera3D.setPosition(5, 5, 5)
scene.camera3D.lookAt(0, 0, 0)
// Animate camera
scene.play(new Transform(scene.camera3D, {
position: { x: 10, y: 10, z: 10 }
}))import { Sequence } from 'vivid-animations'
scene.play(new Sequence([
new Create(circle), // First: create circle
new FadeIn(text), // Then: fade in text
new Transform(circle, { // Finally: move circle
transform: { translate: { x: 2, y: 0 } }
})
]))import { AnimationGroup } from 'vivid-animations'
scene.play(new AnimationGroup([
new Create(circle), // All animations
new FadeIn(text), // happen at the
new Create(axes) // same time
]))// Set custom duration
new Create(circle, { duration: 2 }) // 2 seconds
// Set custom easing
new FadeIn(text, {
duration: 1.5,
easing: t => t * t // Quadratic ease-in
})Create objects that update continuously:
import { Updater, ValueTracker } from 'vivid-animations'
const tracker = new ValueTracker(0)
const circle = new Circle({ radius: 1 })
// Circle follows tracker value
scene.addUpdater(new Updater(() => {
circle.moveTo({ x: tracker.getValue(), y: 0 })
}))
// Animate the tracker
scene.play(new Transform(tracker, { value: 5 }))canvas.addEventListener('click', (event) => {
const rect = canvas.getBoundingClientRect()
const x = (event.clientX - rect.left - canvas.width / 2) / 80
const y = -(event.clientY - rect.top - canvas.height / 2) / 80
const dot = new Circle({ radius: 0.1 })
dot.moveTo({ x, y })
scene.play(new Create(dot))
})- Limit object count: Keep the number of objects reasonable
- Use object pooling: Reuse objects instead of creating new ones
- Optimize update loops: Avoid expensive calculations in update methods
- Use appropriate resolution: Don't use more detail than necessary
// Clean up when done
scene.clear() // Remove all objects
scene.stop() // Stop animation loop- Explore the API Reference for complete documentation
- Check out Examples for more complex use cases
- Learn about 3D Graphics for advanced visualizations
- Read about Custom Animations for creating your own
// Make sure canvas exists before creating scene
const canvas = document.getElementById('canvas') as HTMLCanvasElement
if (!canvas) {
console.error('Canvas element not found!')
return
}// Use exact import names
import { Scene, Circle, Create } from 'vivid-animations'
// Not: import Scene from 'vivid-animations'// Make sure to call scene.play()
scene.add(circle) // Adds object to scene
scene.play(new Create(circle)) // Starts animationReady to create amazing mathematical visualizations? Let's dive in!