Rigged 3D Model Animation.
Play a skeletal animation clip on a skinned glTF model.
View code
Run the JavaScript as a module. Keep the import map from the HTML panel.
<script type="importmap">{"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.180.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.180.0/examples/jsm/"}}</script>
<canvas aria-label="Animation demo"></canvas>:root { color-scheme: dark; font: 15px system-ui, sans-serif; color: #dbff55; }
* { box-sizing: border-box; }
body { margin: 0; height: 100dvh; display: grid; place-items: center; overflow: hidden; background: transparent; }
.tile { display: grid; place-items: center; width: 68px; aspect-ratio: 1; border-radius: 18px; background: currentColor; }
.tile { background: #dbff55; color: #202415; font-size: 30px; }
canvas { display: block; width: 100%; height: 100%; min-width: 0; min-height: 0; }
button { font: inherit; color: #f3f1e9; background: #191919; border: 1px solid #ffffff66; padding: 8px 14px; border-radius: 24px; cursor: pointer; }
:focus-visible { outline: 2px solid #dbff55; outline-offset: -3px; }
.notice { max-width: 34ch; padding: 16px; color: #fff; }
@media (prefers-reduced-motion: reduce) { .sprite { animation-play-state: paused; } }import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
// The gallery pauses this loop when the preview leaves the viewport.
let active = !matchMedia('(prefers-reduced-motion: reduce)').matches;
if (new URLSearchParams(location.search).has('embed')) active = false;
addEventListener('message', event => {
if (event.source === parent && event.origin === location.origin && event.data?.type === 'motion-play') {
active = event.data.active;
}
});
function loop(draw) {
let time = 0;
let previous;
function frame(now) {
const dt = previous === undefined ? 0 : Math.min((now - previous) / 1000, .05);
previous = now;
if (active && !document.hidden) {
time += dt;
draw(time, dt);
}
requestAnimationFrame(frame);
}
draw(0, 0);
requestAnimationFrame(frame);
}
const canvas = document.querySelector('canvas');
let renderer;
try {
renderer = new THREE.WebGLRenderer({canvas, antialias:true, alpha:true});
} catch {
canvas.replaceWith(Object.assign(document.createElement('p'), {
className: 'notice', textContent: 'This demo needs WebGL 2. Try a browser with hardware acceleration enabled.'
}));
throw new Error('WebGL 2 unavailable');
}
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(42, 1, .1, 120);
camera.position.z = 5;
function resize() {
renderer.setPixelRatio(Math.min(devicePixelRatio || 1, 2));
const width = Math.max(1, canvas.clientWidth);
const height = Math.max(1, canvas.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}
resize();
addEventListener('resize', resize);
const source = 'https://cdn.jsdelivr.net/gh/mrdoob/three.js@r180/examples/models/gltf/RobotExpressive/RobotExpressive.glb';
scene.add(new THREE.HemisphereLight(0xffffff, 0x534d71, 2.6));
const light = new THREE.DirectionalLight(0xffffff, 3);
light.position.set(3, 5, 4);
scene.add(light);
const gltf = await new GLTFLoader().loadAsync(source);
scene.add(gltf.scene);
const bounds = new THREE.Box3().setFromObject(gltf.scene);
const center = bounds.getCenter(new THREE.Vector3());
const size = bounds.getSize(new THREE.Vector3()).length();
camera.position.copy(center).add(new THREE.Vector3(size * .58, size * .15, size * 1.26));
camera.lookAt(center);
const mixer = new THREE.AnimationMixer(gltf.scene);
const clip = THREE.AnimationClip.findByName(gltf.animations, 'Walking') ?? gltf.animations[0];
mixer.clipAction(clip).play();
loop((_, dt) => {
mixer.update(dt);
renderer.render(scene, camera);
});