mediamonks / fast-image-sequence

The fast-image-sequence-renderer is a powerful package that allows you to display a sequence of images at a high frame rate on your website. It can be used to create smooth animations, 360 product views or video-like sequences from a series of images. Zero dependencies.
https://www.npmjs.com/package/@mediamonks/fast-image-sequence
16 stars 1 forks source link

Added method addMoreFrames #39

Open kimhongyeon opened 3 months ago

kimhongyeon commented 3 months ago

Issue: https://github.com/mediamonks/fast-image-sequence/issues/38

Fast Image Sequence Renderer, as it stands, is difficult to add frames to once initialized. It requires reinitialization to add more frames, which is inefficient and complicates the code since new images keep coming in over a short period, characteristic of streaming.

So, I have added theaddMoreFrames method.

When addMoreFrames is called while the image sequence is running after the instance has been created, the frames and sources are restructured, allowing images to be added seamlessly.

To implement this, I moved some of the initialization logic from the constructor into a struct method. By calling struct in the constructor, it retains the same logic as before.

In addMoreFrames, after modifying this.options.frames, the struct method is called, followed by the destruct method to temporarily release resources like the canvas. Then, the original logic is executed with a few branching points. For example, when calling drawingLoop, it resumes from the previous playback point.

As a result, when the image sequence needs to grow continuously like streaming, calling the addMoreFrames method allows it to function like video streaming.

kimhongyeon commented 2 months ago

Why do you need an addMoreFrames function? Is this because you don't know exactly how many frames the sequence will have in advance? Or is this because you want to preload additional frames later? Or is there any other reason? In your current pull request, you recreate all frames and input sources without destructing the old ones. This could cause memory leaks but is also, as far as I can estimate, no faster than an entire destruct/re-init of the sequence renderer.

Here are the reasons for needing the addMoreFrames method:

Implementation method:

As you suggested, destructing/re-initializing the FastImageSequence from the usage part is also a method, but the usage code would become ugly and hard to understand. More importantly, the video played with the image sequence may not be smooth whenever new frames are added.

As you can see below, in situations where frames are added, it is handled to play continuously and smoothly. Additionally, it branches to moreFrame to help with performance as much as possible.

https://github.com/kimhongyeon/fast-image-sequence/blob/5eb35966d080c18bcb962c04aca7b3a97060cb35/src/lib/FastImageSequence.ts#L373-L377

스크린샷 2024-07-09 오전 7 31 50

On the other hand, if the new image sequences being received from the server in real-time keep coming in at intervals smaller than a second, the addMoreFrames method will be called repeatedly. Considering this, it might be a good idea to handle such situations using throttling or debouncing.