Three.js - Scroll Based Animation: Unterschied zwischen den Versionen
Aus Wikizone
(Die Seite wurde neu angelegt: „== Snippets == == Complete Example (three.js journey) == <syntaxhighlight lang="javascript"> </syntaxhighlight> == Magnetic Sections - Todo == <syntaxhighligh…“) |
|||
| Zeile 1: | Zeile 1: | ||
== Snippets == | == Snippets == | ||
| + | Check Scroll Position | ||
| + | <syntaxhighlight lang="javascript"> | ||
| + | let scrollY = window.scrollY | ||
| + | window.addEventListener('scroll', () => | ||
| + | { | ||
| + | scrollY = window.scrollY | ||
| + | |||
| + | console.log(scrollY) | ||
| + | }) | ||
| + | </syntaxhighlight> | ||
| + | Scroll objects in sync to html scroll | ||
| + | <syntaxhighlight lang="javascript"> | ||
| + | // position objects | ||
| + | const objectsDistance = 4 | ||
| + | mesh1.position.y = - objectsDistance * 0 | ||
| + | mesh2.position.y = - objectsDistance * 1 | ||
| + | mesh3.position.y = - objectsDistance * 2 | ||
| + | //... | ||
| + | // Calc sizes (HINT-no resize event in this example) | ||
| + | const sizes = { | ||
| + | width: window.innerWidth, | ||
| + | height: window.innerHeight | ||
| + | } | ||
| + | |||
| + | //... | ||
| + | // Base camera | ||
| + | const camera = new THREE.PerspectiveCamera(35, sizes.width / sizes.height, 0.1, 100) | ||
| + | camera.position.z = 6 | ||
| + | scene.add(camera) | ||
| + | |||
| + | // Control Scrolling | ||
| + | let scrollY = window.scrollY | ||
| + | window.addEventListener('scroll', () => { scrollY = window.scrollY }) | ||
| + | |||
| + | // ... | ||
| + | |||
| + | // tick function | ||
| + | const tick = () => | ||
| + | { | ||
| + | // ... | ||
| + | // Animate camera | ||
| + | camera.position.y = - scrollY / sizes.height * objectsDistance // scroll objects in sync to html | ||
| + | // ... | ||
| + | } | ||
| + | |||
| + | tick() | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | <syntaxhighlight lang="javascript"> | ||
| + | </syntaxhighlight> | ||
| + | <syntaxhighlight lang="javascript"> | ||
| + | </syntaxhighlight> | ||
| + | <syntaxhighlight lang="javascript"> | ||
| + | </syntaxhighlight> | ||
| + | <syntaxhighlight lang="javascript"> | ||
| + | </syntaxhighlight> | ||
| + | <syntaxhighlight lang="javascript"> | ||
| + | </syntaxhighlight> | ||
| + | <syntaxhighlight lang="javascript"> | ||
| + | </syntaxhighlight> | ||
| + | <syntaxhighlight lang="javascript"> | ||
| + | </syntaxhighlight> | ||
| + | |||
== Complete Example (three.js journey) == | == Complete Example (three.js journey) == | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
Version vom 11. Februar 2022, 19:36 Uhr
Snippets
Check Scroll Position
let scrollY = window.scrollY
window.addEventListener('scroll', () =>
{
scrollY = window.scrollY
console.log(scrollY)
})
Scroll objects in sync to html scroll
// position objects
const objectsDistance = 4
mesh1.position.y = - objectsDistance * 0
mesh2.position.y = - objectsDistance * 1
mesh3.position.y = - objectsDistance * 2
//...
// Calc sizes (HINT-no resize event in this example)
const sizes = {
width: window.innerWidth,
height: window.innerHeight
}
//...
// Base camera
const camera = new THREE.PerspectiveCamera(35, sizes.width / sizes.height, 0.1, 100)
camera.position.z = 6
scene.add(camera)
// Control Scrolling
let scrollY = window.scrollY
window.addEventListener('scroll', () => { scrollY = window.scrollY })
// ...
// tick function
const tick = () =>
{
// ...
// Animate camera
camera.position.y = - scrollY / sizes.height * objectsDistance // scroll objects in sync to html
// ...
}
tick()