gpt4 book ai didi

javascript - 使用 ScrollMagic 和 TimelineMax 控​​制 Lottie 动画

转载 作者:行者123 更新时间:2023-12-02 22:42:12 25 4
gpt4 key购买 nike

我正在尝试使用 ScrollMagic 来控制 TimelineMax 来移动 Lottie 动画的播放头。

因此,当用户滚动时,动画会根据滚动的速度和方向播放。我已经很接近了,需要一点帮助才能将效果带回家。

首先,我包括我的库

<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.5.9/lottie.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/ScrollMagic.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/plugins/animation.gsap.js"></script>

然后...

  // init scrollmagic
var controller = new ScrollMagic.Controller();

现在,动画设置...

  // Manage Animation
var animation = bodymovin.loadAnimation({
container: document.getElementById('lottie'), // Required
path: '/static/animations/animation.json', // Required
renderer: 'svg', // Required
loop: false, // Optional
autoplay: false, // Optional
name: "Welcome to Awesomeness", // Name for future reference. Optional.
});

这就是我正在努力的地方:

  // Setup Timeline
var lottieControl = new TimelineMax({ repeat: -1, yoyo: true }); // <-- don't loop and let the timeline go back and forth
lottieControl.to({ frame:0 }, 1, { // <-- is this right? I'm telling the timeline to start at frame 0
onUpdate:function(){
animation.goToAndStop(Math.round(this.target.frame), true) // <-- move the playback head of the animation to the target frame that has been rounded and use frames not time (true)
},
ease:Linear.easeNone
})

最后,将它们重新组合在一起......

  // Attach to scroll
var lottieScene = new ScrollMagic.Scene({
duration: '80%',
offset: 1
})
.setPin("#header-scroll")
.setTween(lottieControl)
.addTo(controller);

对于我来说,我无法确定我是否正确使用了 goToAndStop 方法。感谢您抽出时间。

最佳答案

经过几个小时的测试,我找到了答案。我看错了东西。我需要将时间线进度与要转到的框架联系起来。在本例中,有 300 帧,因此乘以动画中的帧数。

这是修改后的代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.5.9/lottie.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/ScrollMagic.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.7/plugins/animation.gsap.js"></script>

<script>
// init scrollmagic
var controller = new ScrollMagic.Controller();

// Manage Animation
var animation = bodymovin.loadAnimation({
container: document.getElementById('lottie'), // Required
path: '/static/animations/scroll_animation.json', // Required
renderer: 'svg', // Required
loop: false, // Optional
autoplay: false, // Optional
name: "Welcome to Awesomeness",
});

// Setup Timeline
var tl = new TimelineMax();
tl.to({frame:0}, 1, {
frame: animation.totalFrames-1,
onUpdate:function(){
animation.goToAndStop((Math.round(this.progress() * 300)), true)
},
ease: Linear.easeNone
})


// Attach to scroll
var lottieScene = new ScrollMagic.Scene({
duration: '100%',
offset: 1
})
.setPin("#header-scroll")
.setTween(tl)
.addTo(controller);

</script>

关于javascript - 使用 ScrollMagic 和 TimelineMax 控​​制 Lottie 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58549067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com