The easiest way to control FBX animations with Three.js (almost π).
Provides time management, animation control, mesh attachment, and HTML-based controls for FBX animations in Three.js applications.
npm install fbx-animation-controls --saveimport { FBXAnimationControls } from 'fbx-animation-controls';
const controls = new FBXAnimationControls(document.getElementById('viewer'));
// Attach an FBX mesh with animations
controls.attach(mesh, { play: true, atTime: 0.123 });
// In your render loop:
function animate() {
controls.update();
// ... your other render code
requestAnimationFrame(animate);
}import { FBXAnimationControls, IAttachOptions, IControlsConfiguration, OutputTimeFormats } from 'fbx-animation-controls';
const config: IControlsConfiguration = {
outputFormat: OutputTimeFormats.MM_SS_MS,
initHTMLControls: true
};
const controls = new FBXAnimationControls(document.getElementById('viewer')!, config);
const attachOptions: IAttachOptions = {
play: true,
atTime: 2.5
};
controls.attach(mesh, attachOptions);attachedMesh: Mesh | null- Currently attached Three.js meshisPlaying: boolean- Whether animation is currently playingisPaused: boolean- Whether animation is pausedisStopped: boolean- Whether animation is stoppedisHTMLControlsAvailable: boolean- Whether HTML controls are initialized
Attach a Three.js mesh with FBX animations.
Parameters:
mesh- Three.js Mesh object with animationsattachOptions- Optional configuration objectplay?: boolean- Auto-play animation after attachmentatTime?: string | number- Start time for the animation
Detach the current mesh and reset controls.
Start playing the animation.
Pause the animation at current time.
Stop the animation and reset to beginning.
Set the current animation time.
Set animation progress as percentage (0-100).
Get formatted string of current animation time.
Update the animation mixer. Call this in your render loop!
Subscribe to animation events.
interface IControlsConfiguration {
outputFormat?: OutputTimeFormats; // Time display format
initHTMLControls?: boolean; // Whether to create HTML controls
}
enum OutputTimeFormats {
MM_SS_MS = 'MM_SS_MS', // 01:23:45 format
SS_MS = 'SS_MS' // 23:45 format
}Subscribe to events to get notified of animation state changes:
controls.on('PLAY', () => console.log('Animation started'));
controls.on('PAUSE', () => console.log('Animation paused'));
controls.on('STOP', () => console.log('Animation stopped'));
controls.on('MESH_ATTACHED', () => console.log('Mesh attached'));
controls.on('MESH_DETACHED', () => console.log('Mesh detached'));
controls.on('CHANGE_PERCENTAGE', (percentage) => console.log('Progress:', percentage));
controls.on('CHANGE_TIME', (time) => console.log('Time:', time));Available Events:
PLAY- Animation startedPAUSE- Animation pausedSTOP- Animation stoppedMESH_ATTACHED- Mesh attached to controlsMESH_DETACHED- Mesh detached from controlsCHANGE_PERCENTAGE- Animation progress changedCHANGE_TIME- Animation time changed
Add to your HTML file:
<link rel="stylesheet" href="./node_modules/fbx-animation-controls/src/themes/default.css" />Style these CSS selectors according to your design:
General Elements:
.animationSlider- Range input slider.playButton- Play/pause button.currentAnimationTime- Time display text
Slider Track (cross-browser):
.animationSlider::-webkit-slider-runnable-track(WebKit).animationSlider::-moz-range-track(Firefox).animationSlider::-ms-track(IE/Edge)
Slider Thumb (cross-browser):
.animationSlider::-webkit-slider-thumb(WebKit).animationSlider::-moz-range-thumb(Firefox).animationSlider::-ms-thumb(IE/Edge)
# Lint code
npm run lint
# Format code
npm run format
# Check everything
npm run checkMIT
Issues and pull requests are welcome!
