This course was updated in 2024. It no longer supports Tweenjs. Please upgrade to JEasings
Video Lecture
Description
Tweenjs is a JavaScript tweening engine.
A tween (from in-between) is a concept that allows you to change the values of the properties of an object smoothly.
We can decide how long it should take, and if there should be a delay, and what to do each time the tween is updated, whether it should repeat and other things.
Since Three r151, tween.module.min.js is no longer supplied by the Threejs library as shown in the video. Instead, we can install it from its official repository.
npminstall@tweenjs/tween.js
It comes with its own type declarations, so it is also no longer necessary to manually set up a type definition for the library as shown in the video.
The import syntax has also changed. Use,
importTWEENfrom'@tweenjs/tween.js'
Tween Easing Options
TWEEN.Easing._equation_._direction_
Start Scripts
./src/client/client.ts
Warning
Tween is now installed from the official repository rather than using it directly from the local Threejs libs folder as demonstrated in the video. Don't forget to Install Tween
import*asTHREEfrom'three'import{OrbitControls}from'three/examples/jsm/controls/OrbitControls'import{GLTFLoader}from'three/examples/jsm/loaders/GLTFLoader'importStatsfrom'three/examples/jsm/libs/stats.module'importTWEENfrom'@tweenjs/tween.js'constscene=newTHREE.Scene()scene.add(newTHREE.AxesHelper(5))constcamera=newTHREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,0.1,1000)camera.position.z=2constrenderer=newTHREE.WebGLRenderer()//renderer.physicallyCorrectLights = true //deprecatedrenderer.useLegacyLights=false//use this instead of setting physicallyCorrectLights=true propertyrenderer.shadowMap.enabled=truerenderer.setSize(window.innerWidth,window.innerHeight)document.body.appendChild(renderer.domElement)constcontrols=newOrbitControls(camera,renderer.domElement)controls.enableDamping=trueconstsceneMeshes:THREE.Mesh[]=[]letmonkey:THREE.Meshconstloader=newGLTFLoader()loader.load('models/monkey_textured.glb',function(gltf){gltf.scene.traverse(function(child){if((childasTHREE.Mesh).isMesh){constm=childasTHREE.Meshm.receiveShadow=truem.castShadow=trueif(child.name==='Plane'){sceneMeshes.push(m)}elseif(child.name==='Suzanne'){monkey=m}}if((childasTHREE.Light).isLight){constl=childasTHREE.SpotLightl.castShadow=truel.shadow.bias=-0.003l.shadow.mapSize.width=2048l.shadow.mapSize.height=2048}})scene.add(gltf.scene)},(xhr)=>{console.log((xhr.loaded/xhr.total)*100+'% loaded')},(error)=>{console.log(error)})window.addEventListener('resize',onWindowResize,false)functiononWindowResize(){camera.aspect=window.innerWidth/window.innerHeightcamera.updateProjectionMatrix()renderer.setSize(window.innerWidth,window.innerHeight)render()}constraycaster=newTHREE.Raycaster()constmouse=newTHREE.Vector2()functiononDoubleClick(event:MouseEvent){mouse.set((event.clientX/renderer.domElement.clientWidth)*2-1,-(event.clientY/renderer.domElement.clientHeight)*2+1)raycaster.setFromCamera(mouse,camera)constintersects=raycaster.intersectObjects(sceneMeshes,false)if(intersects.length>0){// const p = intersects[0].point// controls.target.set(p.x, p.y, p.z)// new TWEEN.Tween(controls.target)// .to({// x: p.x,// y: p.y,// z: p.z// }, 500)// //.delay (1000)// .easing(TWEEN.Easing.Cubic.Out)// //.onUpdate(() => render())// .start()}}renderer.domElement.addEventListener('dblclick',onDoubleClick,false)conststats=newStats()document.body.appendChild(stats.dom)functionanimate(){requestAnimationFrame(animate)controls.update()TWEEN.update()render()stats.update()}functionrender(){renderer.render(scene,camera)}animate()
./src/typings/tween.js/index.d.ts
Warning
It is no longer necessary to manually set up a type definition for Tween as shown in the video. A type definition is now included with the official Tween install. Don't forget to Install Tween
Create a new folder called ./src/typings/tween.js/ and add the TypeScript definition file for tween.js and name it index.d.ts
// It is no longer necessary to add this type definition. See warning above.exportclasstween{Tween:typeofTweenupdate(time?:number):booleangetAll():Tween[]removeAll():voidadd(tween:Tween):voidremove(tween:Tween):voidEasing:{Linear:{None:(amount:number)=>number}Quadratic:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}Cubic:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}Quartic:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}Quintic:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}Sinusoidal:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}Exponential:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}Circular:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}Elastic:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}Back:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}Bounce:{In:(amount:number)=>numberOut:(amount:number)=>numberInOut:(amount:number)=>number}}}declaretypeEasingFunction=(amount:number)=>numberdeclareclassTween{constructor(object:any)isPlaying():booleanisPaused():booleanto(properties:{},duration?:number):thisduration(value:number):thisstart(time?:number|string):thisstop():thisend():thispause(time:number):thisresume(time:number):thisdelay(amount:number):thisrepeat(times:number):thisrepeatDelay(amount:number):thisonStart(callback:(object:any)=>void):thisonUpdate(callback:(object:any,elapsed:number)=>void):thisonRepeat(callback:(object:any)=>void):thisonComplete(callback:(object:any)=>void):thisonStop(callback:(object:any)=>void):thiseasing(easing:EasingFunction):thischain(tween:Tween):this}declareconstTWEEN:tween
./src/client/tsconfig.json
Warning
It is no longer necessary to modify the tsconfig.json since Tween is now installed from the official repository rather than using it directly from the local Threejs lib folder as demonstrated in the video. A type definition is now included in the official Tween install and is already correctly linked. Don't forget to Install Tween
Update the compiler options paths to point to the new tween.js typescript definition file.
1 2 3 4 5 6 7 8 9101112
// It is no longer necessary to modify the tsconfig.json. See warning above.{"compilerOptions":{"target":"ES6","moduleResolution":"node","strict":true,"paths":{"three/examples/jsm/libs/tween.module.min":["../typings/tween.js"]}},"include":["**/*.ts"]}