#timeline #sequencer #web-control #component #javascript #animation #ui #web-ui
|
пре 2 година | |
---|---|---|
.vscode | пре 2 година | |
demo | пре 5 година | |
lib | пре 2 година | |
src | пре 2 година | |
tests | пре 2 година | |
.babelrc | пре 5 година | |
.eslintignore | пре 2 година | |
.eslintrc.js | пре 2 година | |
.gitignore | пре 5 година | |
.npmignore | пре 5 година | |
.prettierrc.js | пре 2 година | |
LICENSE | пре 6 година | |
README.md | пре 2 година | |
_config.yml | пре 5 година | |
cspell.json | пре 2 година | |
index.html | пре 2 година | |
karma.conf.js | пре 2 година | |
package-lock.json | пре 2 година | |
package.json | пре 2 година | |
tsconfig.json | пре 5 година | |
tsconfig.tests.json | пре 5 година | |
webpack.config.js | пре 2 година |
Animation timeline is a TypeScript, no-dependency, canvas component designed to visualize and manipulate animation keyframes.
Features:
<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>
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);
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 () => {
timeline?.dispose();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Example to subscribe and pass model or time update:
useEffect(() => {
if (timeline) {
timeline.setModel(model);
}
}, [model, timeline]);
// Example to subscribe and pass model or time update:
useEffect(() => {
if (timeline) {
timeline.setTime(time);
}
}, [time, timeline]);
return <div ref={timelineElRef} />;
}
export default TimelineComponent;
Outline list\tree can implemented as a separate HTML component and synchronized with the timeline. See the live demo
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:
Example on how to add a keyframe to existing model:
const existingModel = timeline.getModel();
existingModel.rows[0].keyframes.append({ val: 20 });
timeline.setModel(existingModel);
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 |
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 |
Events can be prevented by calling args.preventDefault()
Example of the type strict event subscription:
this.timeline.onDragStarted((args: TimelineDragEvent) => {
if (args) {
}
});
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 units = timeline.getTime();
timeline.onTimeChanged((event: TimelineTimeChangedEvent) => {
if(event.source !== TimelineEventSource.User) {
units = event.var;
}
});
Displayed units text can be changed by overriding a method:
timeline._formatUnitsText = (val)=> { return val + ' ms'; };
Timeline is rendered as a canvas, so has no HTML elements for the css styling. Styles can be applied on a few levels:
Styles are applied by a global settings and can be overridden by a row or keyframe style.
TypeScript fixes, updated build packages.
Vanilla js implementation.
run once to install development references:
npm install
Run next command to pack JavaScript as a bundle:
npm run build
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 debug window and click 'Launch Debug File'. Put breakpoint in any typescript file and trigger function from the browser.
Recommended extensions:
Component has no production dependencies when built. To pack and transpile TypeScript Babel + Webpack is used. For the testing mocha and chai, as the assertion library are used.
To build TypeScript unittests command should be executed:
npm run build-tests
Tests execution can be started by opening tests/unittests.html. Mocha test libs are hosted on the internet, so connection is required.
MIT