jeremyckahn / shifty

The fastest TypeScript animation engine on the web
https://jeremyckahn.github.io/shifty/doc/
MIT License
1.54k stars 88 forks source link

Suggestion: iterate through array. #45

Closed ghost closed 9 years ago

ghost commented 9 years ago

This would be a great add-on to Shifty: in the "to" property object, the tweened variables could be arrays (of numbers, strings, whatever), and the engine would iterate through the array's items and return the current value as state.

Example: var T = (new Tweenable()).setConfig({ from : { x : 0, y : 0 }, to : { x : [1, 6, 13, 8, 19, 25, 42, 66, 'ALMOST THERE', 'DONE'], y :100 } });

It should work like this: the contents of the array are irrelevant to the engine. What is relevant is the number of items in the array. This is in fact the number of phases in the tween. On each tweening cycle, the state returned is the value of the item that corresponds to current phase (segment). It is, theoretically, a tween from 0 to ARRAY.length() plus/minus offset/range.

The "from".x member should be the starting index of the array (start offset), or a range (array with 2 integer items). For instance, from { x : 2, y : 0 }... should iterate through the "to".x array starting from index 2 and above. For ranges, the tweening will be limited between the specified range of items in the array.

I will try an implementation and see how this goes. I will also come up with a real world use-case of this scenario (I already have one, a 2-dimensional line chart representation).

jeremyckahn commented 9 years ago

Hi @adrianvoica, thanks for your suggestion. This is an interesting idea, but I feel that it's a bit too specific to be added to the core Shifty library. The good news is that you should be able to implement this functionality as an extension to Shifty via the low-level Filter API. The Token extension uses this API to interpolate arbitrarily-formatted strings, so you may want to use that as a guide on how to use the Filters practically (sorry if that code is a little hard to follow).

Is this something that would fit your use case?

ghost commented 9 years ago

Yeah. Totally overlooked that feature. My bad. Filters is what I'm after. They solve my problem. Thanks. ;)

P.S: maybe a "seek" method would fit in with the pause/resume/play/stop methods, besides the "interpolate" feature, for sake of completeness.

I will close this issue.

jeremyckahn commented 9 years ago

maybe a "seek" method would fit in with the pause/resume/play/stop methods, besides the "interpolate" feature, for sake of completeness.

Good idea. I filed an issue to track that task with #46.