airbnb / lottie-web

Render After Effects animations natively on Web, Android and iOS, and React Native. http://airbnb.io/lottie/
MIT License
29.85k stars 2.85k forks source link

Simple 'playToFrame' or something similar. #3030

Open martinstender opened 10 months ago

martinstender commented 10 months ago

Hi all,

I'm a bit confused about how to best handle a simple scenario, where we have a timeline with, say, 100 frames and then maybe 5 buttons, that in theory would send the playhead to frame 0, 25, 50, 75 and 100, respectively. The idea is simply that if you are at frame 50 and click the first button, the animation plays from frame 50 to frame 0, and if you click button 2 after clicking button 1, it plays from frame 0 to frame 25 and so on. In other words, I'm trying to make the animation play from where ever the playhead currently is, to where ever I want it to go to.

What is the right way to do that?

TIA and best regards

martinstender commented 10 months ago

This seems to do it. (player is the animation object)

So I just add my new playToFrame function to the animation object. I tried to use the currentFrame property, but I guess calling playSegments() changes that property continuously, so renderer.renderedFrame this is more reliable.

player['playToFrame'] = function(targetFrame, forceOrWait){ player.playSegments([player.renderer.renderedFrame,targetFrame], forceOrWait); };

Hope this can be of help to other peeps :-)

samuelOsborne commented 10 months ago

Hey @martinstender I think calling resetSegments() before calling your next playSegments() would fix the issue as playSegments modifies the playback area - if you called playSegments([50,100],true) your frame 0 would actually be equal to 50 relative to the whole animation

Since your solution works well thats great just wanted to give a bit of context ;)

I think it would be possible with something like

click() {
 player.resetSegments();
 player.playSegments([player.currentFrame, 0], true);
}
martinstender commented 10 months ago

Thank you, @samuelOsborne. Yes, that would do it too. Many thanks :-)