When scrolling, the progress is calculated as a percentage. Depending on which percentage range the scroll is within, a different animation script is executed.
You can set a property, run a continuing animation or run a linear interpolation between two values during a chosen scroll percentage range.
If using linear interpolation, and it is not intended to run across the full 100 percent of the scroll, then you must also scale it so that it starts and ends at the designated scroll percentages. See the code for examples of several different types of animations.
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"/><metaname="viewport"content="width=device-width, initial-scale=1"/><title>
Three.js TypeScript Tutorials by Sean Bradley :
https://sbcode.net/threejs
</title><style>body{overflow-x:hidden;margin:0px;font-family:monospace;color:white;}canvas{position:fixed;top:0;left:0;}main{width:100vw;height:200vw;z-index:99;position:absolute;justify-content:center;text-align:center;pointer-events:none;font-size:5vh;}section{min-height:100vh;padding:20px;font-size:4vh;}#scrollProgress{position:fixed;bottom:10px;left:10px;z-index:99;font-size:3vh;}</style></head><body><spanid="scrollProgress"></span><main><h1>Animate on Scroll</h1><section><h2>Begin scrolling to see things change</h2></section><section><h2>Changing Objects Position</h2><p>The cubes position is now changing</p></section><section><h2>Changing Objects Rotation</h2><p>The cubes rotation is now changing</p></section><section><h2>Changing Camera Position</h2><p>The camera position is now changing</p></section><section><h2>You are at the bottom</h2><p>The cube will now be auto rotating</p><p>
Now you can scroll back to the top to reverse the animation
</p></section></main><scripttype="module"src="bundle.js"></script></body></html>
import*asTHREEfrom'three'importStatsfrom'three/examples/jsm/libs/stats.module'constscene=newTHREE.Scene()constgridHelper=newTHREE.GridHelper(10,10,0xaec6cf,0xaec6cf)scene.add(gridHelper)constcamera=newTHREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,0.1,1000)constrenderer=newTHREE.WebGLRenderer()renderer.setSize(window.innerWidth,window.innerHeight)document.body.appendChild(renderer.domElement)constgeometry=newTHREE.BoxGeometry()constmaterial=newTHREE.MeshBasicMaterial({color:0x00ff00,wireframe:true,})constcube=newTHREE.Mesh(geometry,material)cube.position.set(0,0.5,-10)scene.add(cube)window.addEventListener('resize',onWindowResize,false)functiononWindowResize(){camera.aspect=window.innerWidth/window.innerHeightcamera.updateProjectionMatrix()renderer.setSize(window.innerWidth,window.innerHeight)render()}/* Liner Interpolation * lerp(min, max, ratio) * eg, * lerp(20, 60, .5)) = 40 * lerp(-20, 60, .5)) = 20 * lerp(20, 60, .75)) = 50 * lerp(-20, -10, .1)) = -.19 */functionlerp(x:number,y:number,a:number):number{return(1-a)*x+a*y}// Used to fit the lerps to start and end at specific scrolling percentagesfunctionscalePercent(start:number,end:number){return(scrollPercent-start)/(end-start)}constanimationScripts:{start:number;end:number;func:()=>void}[]=[]//add an animation that flashes the cube through 100 percent of scrollanimationScripts.push({start:0,end:101,func:()=>{letg=material.color.gg-=0.05if(g<=0){g=1.0}material.color.g=g},})//add an animation that moves the cube through first 40 percent of scrollanimationScripts.push({start:0,end:40,func:()=>{camera.lookAt(cube.position)camera.position.set(0,1,2)cube.position.z=lerp(-10,0,scalePercent(0,40))//console.log(cube.position.z)},})//add an animation that rotates the cube between 40-60 percent of scrollanimationScripts.push({start:40,end:60,func:()=>{camera.lookAt(cube.position)camera.position.set(0,1,2)cube.rotation.z=lerp(0,Math.PI,scalePercent(40,60))//console.log(cube.rotation.z)},})//add an animation that moves the camera between 60-80 percent of scrollanimationScripts.push({start:60,end:80,func:()=>{camera.position.x=lerp(0,5,scalePercent(60,80))camera.position.y=lerp(1,5,scalePercent(60,80))camera.lookAt(cube.position)//console.log(camera.position.x + " " + camera.position.y)},})//add an animation that auto rotates the cube from 80 percent of scrollanimationScripts.push({start:80,end:101,func:()=>{//auto rotatecube.rotation.x+=0.01cube.rotation.y+=0.01},})functionplayScrollAnimations(){animationScripts.forEach((a)=>{if(scrollPercent>=a.start&&scrollPercent<a.end){a.func()}})}letscrollPercent=0document.body.onscroll=()=>{//calculate the current scroll progress as a percentagescrollPercent=((document.documentElement.scrollTop||document.body.scrollTop)/((document.documentElement.scrollHeight||document.body.scrollHeight)-document.documentElement.clientHeight))*100;(document.getElementById('scrollProgress')asHTMLDivElement).innerText='Scroll Progress : '+scrollPercent.toFixed(2)}conststats=newStats()document.body.appendChild(stats.dom)functionanimate(){requestAnimationFrame(animate)playScrollAnimations()render()stats.update()}functionrender(){renderer.render(scene,camera)}window.scrollTo({top:0,behavior:'smooth'})animate()