JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.js"></script>
JavaScript
let camera, scene, renderer;
const count = 100;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.z = 20;
scene = new THREE.Scene();
const geometry = new THREE.BoxBufferGeometry();
const objects = [];
for ( let i = 0; i < count; i ++ ) {
const box = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0xffffff * Math.random() } ) );
box.position.x = ( ( i % 10 ) - 4.5 ) * 2;
box.position.y = ( 4.5 - Math.floor( i / 10 ) ) * 2;
scene.add( box );
objects.push( box.scale );
}
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// animation
gsap.to( objects, {
duration: 1,
x: 0,
y: 0,
z: 0,
ease: "power1.inOut",
repeat: -1,
yoyo: true,
stagger: {
each: 0.1,
from: "center",
grid: [ 10, 10 ]
}
});
}
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}