A JavaScript-based timeline editor component that provides an interactive interface for creating and editing animation curves. The editor supports both linear and bezier interpolation, with a playback system for previewing animations.
- Interactive curve editing with draggable control points
- Support for both linear and bezier interpolation
- Zoomable and pannable timeline view
- Playback controls (play, pause, stop, seek)
- Marker system for marking specific points in the timeline
- Undo functionality
- Export/Import curve data and markers
- Customizable appearance and duration
-
To use the TimelineEditor, you need to include the timeline.js file in your project.
-
Include the TimelineEditor in your HTML file:
<canvas id="timelineCanvas"></canvas>
<script src="timeline.js"></script>
- Use the TimelineEditor in your JavaScript code:
// Initialize the editor
const timeline = new TimelineEditor('timelineCanvas');
// Add event listener for value changes
timeline.addEventListener('playheadTimeChange', (data) => {
console.log(`Time: ${data.time}s, Value: ${data.value}%`);
});
// Start playback
timeline.play();
You can customize the appearance of the TimelineEditor by passing options to the constructor:
const timeline = new TimelineEditor('timelineCanvas', {
totalDuration: 120, // Total duration of the timeline in seconds
viewportDuration: 30, // Duration of the visible viewport in seconds
minViewportDuration: 10, // Minimum viewport range in seconds
maxViewportDuration: 90, // Maximum viewport range in seconds
lineColor: '#c00' // Color of the lines / curves in the timeline view
})
- Left click: Add a polyline control point
- Right click: Add a bezier control point
- Drag control point: Move the control point
- Double click control point: Delete the control point
- Double click marker: Remove the marker
- Drag empty area: Pan viewport
- Mouse wheel: Zoom in/out
- Click the bottom or top area of the timeline: Jump the playhead to the position
- Drag playhead indicator: Scrub through timeline
totalDuration
: Total duration of the timeline in secondsviewportDuration
: Duration of the visible viewport in secondsminViewportDuration
: Minimum viewport range in secondsmaxViewportDuration
: Maximum viewport range in secondslineColor
: Color of the lines / curves in the timeline viewpoints
: Array of control pointsmarkers
: Array of marker positionsplayhead.time
: Current time position of the playhead in secondsplayhead.isPlaying
: Whether playback is active (read-only)
play()
: Start playbackpause()
: Pause playbackstop()
: Stop and reset playheadseek(time)
: Jump to specific time (in seconds)draw()
: Redraw the timeline view (need to call after modifying properties)
exportData()
: Export curve dataimportData(data)
: Import curve dataundo()
: Revert last changegetValue(time)
: Get interpolated value at time (in seconds)
addMarker(time)
: Add a marker at time (in seconds)clearMarkers()
: Remove all markersexportMarkers()
: Export marker dataimportMarkers(data)
: Import marker data
addEventListener(eventName, callback)
: Add event listenerremoveEventListener(eventName, callback)
: Remove event listener
playheadTimeChange
: Fired when playhead time position changes
timeline.addEventListener('playheadTimeChange', (data) => {
console.log(`Time: ${data.time}s, Value: ${data.value}%`);
});
// Create a simple animation curve
const timeline = new TimelineEditor('timelineCanvas');
// Set timeline properties
timeline.totalDuration = 90;
timeline.viewportDuration = 30;
timeline.lineColor = '#0cf';
// Import predefined curve
timeline.importData({
points: [
{ time: 0, value: 0 }, // default point type is TimelineEditor.PointType.POLYLINE
{ time: 5, value: 0 },
{ time: 15, value: 100, type: TimelineEditor.PointType.BEZIER },
{ time: 30, value: 0 }
]
});
// Add some markers
timeline.addMarker(10); // Add marker at 10s
timeline.addMarker(20); // Add marker at 20s
// Monitor value changes
timeline.addEventListener('playheadTimeChange', (data) => {
console.log(`Time: ${data.time}s, Value: ${data.value}%`);
});
You can also find an example of how to use the TimelineEditor in the index.html and app.js files.
The TimelineEditor can also be used in Qt / QML projects. To use the TimelineEditor in QML, you need to include the timeline.qml file in your Qt project. A simple example of how to use the TimelineEditor in QML is provided in the qt directory.
This project was co-created by AI and me. Much thanks with love to everyone who contributed to the AI development. I am using Trae IDE with Claude LLM to help me write the code and this README file.
This project is licensed under the Apache 2.0 License. See the LICENSE file for details.