Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

animation-timeline-js

ievgennaida1.8kMIT2.3.5TypeScript support: included

animation timeline control based on the canvas.

js, animation, timeline, canvas, html5, no, libs, keyframes, editor

readme

animation-timeline-control

npm

Animation timeline is a TypeScript, no-dependency, canvas component designed to visualize and manipulate animation keyframes.

Features:

  • Fast and customizable, rendered on a canvas.
  • Snap, Zoom, Pan mode, multiple keyframes selection.
  • Keyboard support.
  • Drag multiple keyframes, drag keyframe ranges.
  • Area virtualization - only visible canvas area is rendered.
  • Native browser scrollbars are used.
  • Horizontal scale with the automatically adjusted ticks.

gif preview

gif preview

Live Demo

Usage

npm i animation-timeline-js

HTML/JavaScript

<div id="timeline"></div>
<script type="text/javascript">
   let rows = [
      {
        keyframes: [
          {
            val: 40,
          },
          {
            val: 3000
          }
        ]
      }];

    let timeline = new timelineModule.Timeline({id:'timeline'})

    timeline.setModel({ rows: rows });
</script>

Angular

import {
  Timeline,
  TimelineRow,
  TimelineModel,
  TimelineOptions,
} from "animation-timeline-js";

const model = { rows: [] as Array<TimelineRow> } as TimelineModel;
const options = {
  id: "timeline",
  rowsStyle: {
    height: 35,
  } as TimelineRowStyle,
} as TimelineOptions;

const timeline = new Timeline(options, model);

React

import React, { useEffect, useRef, useState } from "react";
import { Timeline, TimelineModel } from "animation-timeline-js";
type Props = {
  time?: number;
  model: TimelineModel;
};

function TimelineComponent(props: Props) {
  const { model, time } = props;
  const timelineElRef = useRef<HTMLDivElement>(null);
  const [timeline, setTimeline] = useState<Timeline>();

  useEffect(() => {
    let newTimeline: Timeline | null = null;
    // On component init
    if (timelineElRef.current) {
      newTimeline = new Timeline({ id: timelineElRef.current });
      // Here you can subscribe on timeline component events
      setTimeline(newTimeline);
    }

    // cleanup on component unmounted.
    return () => newTimeline?.dispose();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [timelineElRef.current]);

  // Example to subscribe and pass model or time update:
  useEffect(() => {
    timeline?.setModel(model);
  }, [model, timeline]);

  // Example to subscribe and pass model or time update:
  useEffect(() => {
    if (time || time === 0) {
      timeline?.setTime(time);
    }
  }, [time, timeline]);

  return <div style={{ width: "100%", minHeight: 300 }} ref={timelineElRef} />;
}
export default TimelineComponent;


// Usage: 
<TimelineComponent
  time={0}
  model={{
    rows: [
      {
        keyframes: [
          {
            val: 40,
          },
          {
            val: 3000,
          },
        ],
      },
    ],
  }}
></TimelineComponent>

Svelte

<script lang="ts">
  import { Timeline } from "animation-timeline-js";
  import type { TimelineModel } from "animation-timeline-js";
  import { onMount } from "svelte";

  export let time: number = 0;
  export let model: TimelineModel;

  let timelineEl!: HTMLDivElement;
  let timeline: Timeline | null = null;

  // Equivalent to componentDidMount and componentWillUnmount
  onMount(() => {
    let newTimeline: Timeline | null = null;
    if (timelineEl) {
      newTimeline = new Timeline({ id: timelineEl });
    }
    timeline = newTimeline;
    // Cleanup
    return () => {
      if (newTimeline) {
        newTimeline.dispose();
      }
    };
  });
  $: if (model) {
    // Reactive statements for model and time
    timeline?.setModel(model);
  }
  $: if (time || time === 0) {
    timeline?.setTime(time);
  }
</script>

<div bind:this={timelineEl} class="editor"></div>

<style>
  .editor {
    width: 100%;
    min-height: 300px;
  }
</style>



// Usage: 
<TimelineComponent
  time={0}
  model={{
    rows: [
      {
        keyframes: [
          {
            val: 40,
          },
          {
            val: 3000,
          },
        ],
      },
    ],
  }}
></TimelineComponent>

FAQ

  • Can I have a tree view or list on the left/right? Answer: The outline/tree view has to be implemented separately. This gives additional control over the styling.
  • Can I have separately connected groups of keyframes per row? Answer: Yes, multiple groups can be created per row. See <demo/demo.js> for details.
  • Can I override the format of the gauge? Answer: Yes, the text can be changed. Just override the method _formatUnitsText.
  • Can I have an individual style per row/keyframe/group? Answer: Yes, every instance can be styled separately if needed.
  • Can I style individual keyframes? Answer: Yes, you have to override _renderKeyframe. See <demo/demo.js> for details.

Outline list

Outline list\tree can implemented as a separate HTML component and scrolling needs to be synchronized with the timeline. See the live demo

gif preview

Tree View implementation

Next steps has to be followed to synchronize custom tree view html with the timeline component:

  • Tree node that should have the same height as row model for the animation timeline.
  • When some of the tree node entries are collapsed than corresponding animation timeline rows should become hidden.
  • scrolling position has to be synchronized, this can be found here: live demo

Model

Keyframes model is used to pass keyframes and rows to be visualized. Component is using passed model for the visualization purpose and has no method to manage tracks or keyframes. It also means that any attached metadata can be passed and it will be preserved (Use case: you can attach additional data for each keyframe).

Read only and defined by the interfaces:

  • TimelineModel
  • TimelineRow
  • TimelineKeyframe

Example on how to add a keyframe to existing model:

    const existingModel = timeline.getModel();
    existingModel.rows[0].keyframes.append({ val: 20 });
    timeline.setModel(existingModel);

Events/Methods and options

Event name description
timeChanged time changed. source can be used to check event sender. args type: TimelineTimeChangedEvent
selected keyframe is selected. args type: TimelineSelectedEvent
scroll On scroll. args type: TimelineScrollEvent
scrollFinished On scroll finished. args type: TimelineScrollEvent
dragStarted emitted on drag started. args type: TimelineDragEvent
drag emitted when dragging. args type: TimelineDragEvent
dragFinished emitted when drag finished. args type: TimelineDragEvent
keyframeChanged emitted when drag finished. args type: TimelineKeyframeChangedEvent
onContextMenu emitted on context menu displayed. args type: TimelineKeyframeChangedEvent

Events can be prevented by calling args.preventDefault()

Events subscription is performed in the JavaScript (not a DOM events):

this.timeline.onDragStarted((args: TimelineDragEvent) => {
    if (args) {
    }
});

Methods

Method name description
setTime set current active time. Returns bool to indicate whether time was set. Ex: cannot be changed when dragged. Also timeline interactions can be disabled.
getTime get current position of the timeline.
dispose Call to unsubscribe from all the events. Important when UI component is unmounted or page is closed.
setOptions Set timeline properties
getOptions Get current options of the timeline.
getAllKeyframes Get array of all keyframes from the current active model.

Options

Options can be passed when timeline is created or by calling setOptions method. See all options in the TimelineOptions interface.

Main options: | Property | description | | --------------- | ------------------------------------------------------------------------------------------------------------------------- | | groupsDraggable | keyframes group is draggable. Default: true | | keyframesDraggable | keyframes group is draggable. Default: true | | timelineDraggable | Timeline can be dragged or position can be changed by user interaction. Default True |

Keyboard shortcuts

Selection Mode

  • Click - select single keyframe.
  • Ctrl + Click - add new keyframe, toggle existing keyframe.

Keyframes can be marked as selectable = false to prevent interaction.

Zoom Mode

  • Ctrl - reverse zoom in/zoom out.
  • Ctrl + Mouse wheel - zoom to the current active cursor. (Same logic for the pan mode)

Interaction Modes

Selection - allow to select one or group of the keyframes.

  • selection - Keyframe selection tool selecting single or group of keyframes.
  • pan - Pan tool with the possibility to select keyframes.
  • nonInteractivePan - Allow only pan without any keyframes interaction. Timeline still can be moved and controlled by option 'timelineDraggable'.
  • zoom - zoom tool
  • none - No iteraction, except moving a timeline. Timeline still can be moved and controlled by option 'timelineDraggable'.

Example:

  timeline.setInteractionMode('none');

For the TypeScript TimelineInteractionMode enum is used.

Timeline units and position

Expected that you have a component or engine that can execute playing a timeline. Ex: SVG has events to run the animations and report current time position. This component is meant only to visualize the position.

Time indicator position can be changed by a method call:

timeline.setTime(1000);

Current time can be fetched by a method call or by an event:

let value = timeline.getTime();

timeline.onTimeChanged((event: TimelineTimeChangedEvent) => {
  if(event.source !== TimelineEventSource.User) {
    value = event.val;
  }
});

Displayed units text can be changed by overriding a method:

timeline._formatUnitsText = (val)=> { return val + ' ms'; };

Styling

The timeline component is rendered as a canvas and has no HTML elements for CSS styling. Styles are applied as a part of the keyframes model and can be applied in a cascade order from bottom to the top:

  • Global control setting (See TypeScript interface TimelineStyle)
  • row styles (See TypeScript interface TimelineRowStyle)
  • Keyframe group styles with the underlying keyframe styles. (TimelineGroupStyle)
  • keyframe styles (See TypeScript interface TimelineKeyframeStyle)

Separate global styles for the timeline indicator are used:

  • TimelineOptions - global component properties and styles.
    • TimelineStyle timeline indicator styles
      • TimelineCapStyle - cap of the timeline style.

Changes

See Changelog here

Development of the component

Build

run once to install development references:

  npm install

Run next command to pack JavaScript as a bundle:

  npm run build

Debug

VSCode is used as IDE and configuration is included to the project sources.

To debug project you should run command once files are changed:

npm run build

Then navigate to the VsCode debug window and click 'Launch Debug File'. Put breakpoint in any typescript file and trigger function from the browser.

Recommended extensions:

  • markdownlint
  • ESLint
  • esbenp.prettier-vscode

Dev Dependencies

Component has no production dependencies when built. TypeScript Babel + Webpack is used to pack and transpile the library. Mocha and chai are used as test & assertion library.

Run Tests

npm run test

License

MIT

changelog

animation-timeline-control Changelog

Changes

[2.3.5] - 21.07.2024

Changed

  • Allow to change gauge denominators.
  • Dev packages updates.

[2.3.4] - 10.04.2024

Changed

  • Added context menu event.
  • Extended demo to show different styling options.
  • Updated dev packages to the latest versions.
  • Added groups stroke and border radius support.
  • BUG FIX: group is rendered for one keyframe.
  • BUG FIX: missing timelineGroupStyle export.
  • Added demo for the custom keyframe rendering (image rendering).

[2.3.2] - 10.04.2024

Changed

  • Fixed casing of the lib files.

[2.3.1] - 24.03.2024

Changed

  • Fixed dispose function. Added svelte example
  • Added time pad for the timeline format.

[2.3.0] - 11.06.2022 (Breakings changes)

Added

  • Allow to style groups/connection of the keyframes separately. (see live demo - Live demo)

Changed

  • Breaking change! Style names are aligned with the HTML namings.
  • Breaking change! Events are sending objects with the change metadata.
  • Breaking change! timelineInteractive is renamed as timelineDraggable
  • Breaking change! keyframe styles are moved to the 'style' property for each keyframe.
  • Internal defaults.ts consts are moved to the default folder into the separate files.
  • Dev packages are updated.
  • timelinePoint -> timelineValues
  • TimelineClickEvent-> added point and prevPoint
  • TimelineDragEvent-> added point and prevPoint
  • Function getScrollLeft/setScrollLeft is changed to the property scrollLeft
  • Function getScrollTop/setScrollTop is changed to the property scrollTop
  • scrollLeft() -> scrollToRightBounds()
  • Renamed private function 'height' as 'canvasClientHeight'
  • Renamed private function 'width' as 'canvasClientWidth'
  • removed getMin and getMax for the ranged properties favor of min, max property. (In a case of calculated values getters can be used).
  • mergeOptions are moved to TimelineUtils
  • Strict TypeScript mode is enabled.
  • Refactoring of the event arguments.
  • models are moved from root src to src\models folders.

[2.2.3]

Added

  • Added scrollFinished event. TODO: group styles, refactoring of the styling system.

Fixed

  • Fixed Dispose method is not removing all scroll container event handlers.
  • Fixed demo nonInteractivePan.
  • Fixed timeline player demo.
  • Fixed rectangular keyframes mouse over detection.
  • Fixed cloning of the HTML element when options are set.

[2.2.2]

Added

  • Added new option timelineInteractive = true/false to control possibility for user to move timeline position.
  • Added 'nonInteractivePan' interaction mode that is allowing only to pan and change position of the timeline without changing the keyframes position.
  • Added 'none' interaction mode where no interactions are allowed.
  • Added 'play' demo to the index.html

Changed

  • Private property findDraggable is renamed tofindDraggableElement
  • Options are appended to the current active options, not to default.
  • Fixed order of the build (definitions and tests only after the definitions.)
  • updated build packages.

[2.2.1]

TypeScript fixes, updated build packages.

> [2.0]

  • Migrated to TypeScript, Webpack, Babel.
  • API is refined.

< [2.0]

Vanilla js implementation.