|
| 1 | +<!-- shows what happens when we call tween.end() --> |
| 2 | + |
| 3 | +<style> |
| 4 | + #box { |
| 5 | + background-color: deeppink; |
| 6 | + width: 100px; |
| 7 | + height: 100px; |
| 8 | + } |
| 9 | +</style> |
| 10 | + |
| 11 | +<div id="box"></div> |
| 12 | + |
| 13 | +<button id="end">tween.end()</button> |
| 14 | + |
| 15 | +<script type="module"> |
| 16 | + import {Tween, Easing} from '../dist/tween.esm.js' |
| 17 | + |
| 18 | + const box = document.getElementById('box') // Get the element we want to animate. |
| 19 | + |
| 20 | + const coords = {x: 0, y: 0} // Start at (0, 0) |
| 21 | + |
| 22 | + const tween = new Tween(coords, false) // Create a new tween that modifies 'coords'. |
| 23 | + .to({x: 300, y: 200}, 5000) // Move to (300, 200) in 1 second. |
| 24 | + .easing(Easing.Quadratic.InOut) // Use an easing function to make the animation smooth. |
| 25 | + .onUpdate(() => { |
| 26 | + // Called after tween.js updates 'coords'. |
| 27 | + // Move 'box' to the position described by 'coords' with a CSS translation. |
| 28 | + box.style.setProperty('transform', 'translate(' + coords.x + 'px, ' + coords.y + 'px)') |
| 29 | + |
| 30 | + // |
| 31 | + console.log(coords, tween.isPlaying()) |
| 32 | + }) |
| 33 | + .onComplete(() => { |
| 34 | + console.log('onComplete') |
| 35 | + }) |
| 36 | + .start() // Start the tween immediately. |
| 37 | + |
| 38 | + // Setup the animation loop. |
| 39 | + function animate(time) { |
| 40 | + tween.update(time) |
| 41 | + requestAnimationFrame(animate) |
| 42 | + } |
| 43 | + |
| 44 | + requestAnimationFrame(animate) |
| 45 | + |
| 46 | + document.querySelector('#end').addEventListener('click', () => { |
| 47 | + console.log('tween.end') |
| 48 | + tween.end() |
| 49 | + console.log(tween) |
| 50 | + }) |
| 51 | +</script> |
0 commit comments