JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://threejs.org/build/three.min.js"></script>
JavaScript
let camera, scene, renderer, clock, spritesheet;
const width = 1536; // total width of the sprite sheet
const size = 256; // size of a single image
const count = 6; // amount of images of sprite sheet
const fps = 12; // how many images are shown per second
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 1;
scene = new THREE.Scene();
clock = new THREE.Clock();
const loader = new THREE.TextureLoader();
spriteSheet = loader.load( 'https://i.imgur.com/yz4peMz.png' );
spriteSheet.minFilter = THREE.LinearFilter;
spriteSheet.repeat.x = 1 / count;
const geometry = new THREE.PlaneBufferGeometry();
const material = new THREE.MeshBasicMaterial( { map: spriteSheet } );
const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
}
function animate() {
requestAnimationFrame( animate );
const t = clock.getElapsedTime();
if ( spriteSheet ) {
spriteSheet.offset.x = Math.floor( ( t * fps ) % count ) * size / width;
}
renderer.render( scene, camera );
}