- Framework
- Core Concepts
- Timeline System
Timeline System
The @designcombo/timeline
package provides the canvas-based timeline visualization and interaction system for the video editor SDK.
Timeline Class
Constructor
new Timeline(canvas: HTMLCanvasElement, options: TimelineOptions)
Creates a new timeline instance.
Parameters:
canvas
: HTML canvas element to render the timelineoptions
: Configuration options for the timeline
TimelineOptions Interface
interface TimelineOptions {// Required propertieswidth: number;height: number;scale: ITimelineScaleState;duration: number;// Optional propertiesbounding?: {width: number;height: number;};selectionColor?: string;selectionBorderColor?: string;onScroll?: (scrollData: ScrollData) => void;onResizeCanvas?: (size: Size) => void;state?: any;spacing?: {left: number;right: number;};sizesMap?: Record<string, number>;itemTypes?: string[];acceptsMap?: Record<string, string[]>;guideLineColor?: string;}
Methods
purge()
Cleans up resources and event listeners.
timeline.purge(): void
setViewportPos(posX: number, posY: number)
Sets the viewport position.
timeline.setViewportPos(posX: number, posY: number): void
scrollTo(options: ScrollOptions)
Scrolls to a specific position.
interface ScrollOptions {scrollLeft?: number;scrollTop?: number;}timeline.scrollTo(options: ScrollOptions): void
registerItems(items: Record<string, typeof TimelineItem>)
Registers custom timeline items.
Timeline.registerItems({CustomText: CustomTextItem,CustomAudio: CustomAudioItem,// ... more items});
Base Item Classes
Resizable
Base class for items that can be resized horizontally.
import { Resizable, ResizableProps } from "@designcombo/timeline";class CustomItem extends Resizable {static type = "CustomItem";constructor(props: ResizableProps) {super(props);}_render(ctx: CanvasRenderingContext2D) {super._render(ctx);// Custom rendering logic}}
Properties:
width
: Item widthduration
: Media durationplaybackRate
: Playback speeddisplay
: Display properties
Methods:
setCoords()
: Updates item coordinatessetDimensions(width: number, height: number)
: Sets item dimensions
Trimmable
Base class for items that can be trimmed (audio/video).
import { Trimmable, TrimmableProps } from "@designcombo/timeline";class CustomMediaItem extends Trimmable {static type = "CustomMediaItem";constructor(props: TrimmableProps) {super(props);}}
Properties:
trim
: Trim informationduration
: Media durationplaybackRate
: Playback speeddisplay
: Display properties
Methods:
setTrim(trim: ITrim)
: Sets trim valuesgetTrimmedDuration()
: Gets trimmed duration
Control
Base class for interactive controls.
import { Control } from "@designcombo/timeline";const control = new Control({x: 0.5,y: 0,render: (ctx, left, top, styleOverride, fabricObject) => {// Custom control rendering},actionHandler: resize.common,cursorStyleHandler: scaleSkewCursorStyleHandler,actionName: "resizing",sizeX: 20,sizeY: 32,offsetX: 10,});
Constructor Options:
x
: Relative X position (-0.5 to 0.5)y
: Relative Y position (-0.5 to 0.5)render
: Custom rendering functionactionHandler
: Action handler functioncursorStyleHandler
: Cursor style handleractionName
: Action name for eventssizeX
: Control widthsizeY
: Control heightoffsetX
: X offsetoffsetY
: Y offset
Track
Base class for track containers.
import { Track as TrackBase, TrackItemProps } from "@designcombo/timeline";class CustomTrack extends TrackBase {static type = "CustomTrack";constructor(props: TrackItemProps) {super(props);}}
Properties:
items
: Array of track itemsmagnetic
: Whether the track accepts dropped itemsheight
: Track height
Methods:
addItem(item: TimelineItem)
: Adds item to trackremoveItem(item: TimelineItem)
: Removes item from trackgetItems()
: Returns all items in track
Helper
Base class for helper/utility items.
import { Helper as HelperBase, HelperProps } from "@designcombo/timeline";class CustomHelper extends HelperBase {static type = "CustomHelper";constructor(props: HelperProps) {super(props);}}
Control System
Built-in Action Handlers
import { resize } from "@designcombo/timeline";// Common resize handlerresize.common;// Audio-specific resize handlerresize.audio;// Media-specific resize handlerresize.media;// Transition-specific resize handlerresize.transition;
Control Utilities
import { controlsUtils } from "@designcombo/timeline";const { scaleSkewCursorStyleHandler } = controlsUtils;
Creating Custom Controls
export const createCustomControls = () => ({left: new Control({x: -0.5,y: 0,render: drawLeftIcon,actionHandler: resize.common,cursorStyleHandler: scaleSkewCursorStyleHandler,actionName: "resizing",sizeX: 20,sizeY: 32,offsetX: -10,}),right: new Control({x: 0.5,y: 0,render: drawRightIcon,actionHandler: resize.common,cursorStyleHandler: scaleSkewCursorStyleHandler,actionName: "resizing",sizeX: 20,sizeY: 32,offsetX: 10,}),});
Event Constants
Timeline Events
import {TIMELINE_SEEK,TIMELINE_PREFIX,TIMELINE_BOUNDING_CHANGED,} from "@designcombo/timeline";// Timeline seek eventTIMELINE_SEEK;// Timeline event prefixTIMELINE_PREFIX;// Timeline bounding changed eventTIMELINE_BOUNDING_CHANGED;
Drag and Drop Events
import { DRAG_END, DRAG_PREFIX, DRAG_START } from "@designcombo/timeline";// Drag start eventDRAG_START;// Drag end eventDRAG_END;// Drag event prefixDRAG_PREFIX;
Utility Functions
Time Conversion
import { timeMsToUnits, unitsToTimeMs } from "@designcombo/timeline";// Convert milliseconds to timeline unitsfunction timeMsToUnits(timeMs: number, zoom: number = 1): number;// Convert timeline units to millisecondsfunction unitsToTimeMs(units: number, zoom: number = 1): number;
Parameters:
timeMs
: Time in millisecondszoom
: Zoom level (default: 1)
Returns:
- Timeline units or milliseconds
Type Definitions
ITimelineScaleState
interface ITimelineScaleState {zoom: number;offset: number;}
ScrollData
interface ScrollData {scrollTop: number;scrollLeft: number;}
TimelineObject
interface TimelineObject {id: string;type: string;left: number;top: number;width: number;height: number;// ... other properties}
ResizableProps
interface ResizableProps {id: string;left: number;top: number;width: number;height: number;fill?: string;stroke?: string;rx?: number;ry?: number;// ... other properties}
TrimmableProps
interface TrimmableProps extends ResizableProps {trim: ITrim;duration: number;playbackRate?: number;display: IDisplay;}