Nick | Coutsos |
---|---|
GitHub | github.com/nickcoutsos |
null | |
null |
Presenting now! Follow along remotely:
https://nickcoutsos.github.io/threejs-animation-lunch-and-learn/
By drawing it more frequently in more positions the movement is smoother and the animation nicer to watch.
See: requestAnimationFrame()
for (let i = 0; i <= steps; i++) {
box.position
.copy(source)
.lerp(dest, i / steps)
}
Tweening allows us to define simple animations using interpolation to fill in the blanks.
So what else can we tween?
for (let i = 0; i <= steps; i++) {
box.scale
.copy(source)
.lerp(dest, i / steps)
}
for (let i = 0; i <= steps; i++) {
box.rotation.x = (
source.rotation.y +
(i / steps) * (
dest.rotation.y -
source.rotation.y
)
)
}
for (let i = 0; i <= steps; i++) {
box.material.opacity = i / steps
}
for (let i = 0; i <= steps; i++) {
box.material.color
.copy(source)
.lerp(dest, i / steps)
}
for (let i = 0; i <= steps; i++) {
box.material.color
.copy(source)
.lerpHSL(dest, i / steps)
}
Thus far we've interpolated values linearly with a fixed number of steps.
Pretend we have an infinite number of steps
Pretend that this set of steps is time. Because it is.
const start = performance.now()
const duration = 5000
function renderFrame(now) {
const delta = now - start
const t = delta / duration
myAnimation(t)
if (delta <= duration) {
requestAnimationFrame(renderFrame)
}
}
Now our animated properties are functions of time.
A timing function:
[0,1]
That input represents time, or, completed percentage of an animation a known duration.
Scaling property changes with respect to elapsed time means they are animated independently of the framerate.
We've covered linear timing:
const linear = t => t
How about a quadratic scale?
const easeInQuad = t => Math.pow(t, 2)
Use whatever expressions you like
const easeInOutSine = t => ( Math.sin(2*Math.PI*t) )
As long as you take a number and return a number...
const bounce = t => {
if (t < 1/2.75) {
return 7.5625*t*t
} else if (t < (2/2.75)) {
return 7.5625*(t-=(1.5/2.75))*t + .75;
} else if (t < (2.5/2.75)) {
return 7.5625*(t-=(2.25/2.75))*t + .9375
} else {
return 7.5625*(t-=(2.625/2.75))*t + .984375
}
}
... you can take it as far as you need to.
npm install easing-utils
import { bounce } from 'easing-utils'
What is it?
Object3D
s all the way downThe scene graph is a hierarchy of objects
Scene
objectan object may have zero or more child objects