veltman / flubber

Tools for smoother shape animations.
MIT License
6.61k stars 167 forks source link

Morphing between multiple shapes and multiple shapes #6

Closed jarben closed 7 years ago

jarben commented 7 years ago

I wonder whether it would be possible to morph a set of shapes to another set of shapes? :)

veltman commented 7 years ago

It's certainly possible - the easy case is one where you have 3 shapes to 3 shapes, or something like that, and I'll probably introduce a flubber.all() method for that at some point. But I'm still unsure of the most intuitive way to handle the case where you have 3 shapes turning into 7 shapes, and how those get matched up. In practice, the results look pretty chaotic and performance isn't great.

In the meantime, to handle the case where you have the same number of from and to shapes, you can do something like:

To get back multiple functions:

var interpolators = startShapes.map(function(shape, i){
  return flubber.interpolate(shape, endShapes[i], options);
});

To get back a single function:

var interpolators = startShapes.map(function(shape, i){
  return flubber.interpolate(shape, endShapes[i], options);
});

var combinedInterpolator = function(t){
  return interpolators.map(function(fn){
    return fn(t);
  }).join(" ");
};
jarben commented 7 years ago

Thanks for your response and code snippets Noah! Yeah, it seems like a challenge, regarding 3->7 scenarios, maybe if 2 items morph into closest 4 and the 3rd into remaining 3? I guess this could be solved even now with your combined interpolator with a bit of a logic around it..

Anyway, love your library, awesome job!

veltman commented 7 years ago

Added .interpolateAll() in v0.3.0. Will probably leave it up to the user if they want to bucket their shapes to do something like 3 shapes -> 7 shapes, rather than simply guessing and matching them up based on their locations.

jarben commented 7 years ago

Awesome, will give it try!

alexjlockwood commented 7 years ago

Does flubber support fading/shrinking shapes out of view as opposed to transforming them into other nearby shapes?

For example, in order to morph 2 shapes into 1 shape, it sometimes looks nicer to have the extra shape shrink out of view instead of attempting to cleverly merge the 2 shapes back into one shape.

Just wondering.

veltman commented 7 years ago

@alexjlockwood I don't think it will ever support fading, but v1 will probably support shrinking with a strategy similar to this demo:

https://bl.ocks.org/veltman/33dbad5aa12d92b977fd29128eb5358e

alexjlockwood commented 7 years ago

@veltman that would be awesome :D. the shrinking might be a good way to deal with holes in some situations as well.

pkhodaveissi commented 5 years ago

How to use interpolateAll to morph a two shapes path to more shapes path? "M7.393 0h7.666l-4.196 16.086c-0.509 1.971-2.287 3.349-4.323 3.349h-4.207l5.060-19.435zM4.724 22.551c2.608 0 4.725 2.117 4.725 4.725s-2.117 4.725-4.725 4.725c-2.607 0-4.724-2.117-4.724-4.725s2.117-4.725 4.724-4.725z" to "M51.231 16.246h-2.7l3.242-12.394c0.002-0.014 0.017-0.024 0.032-0.024h4.849c0.021 0 0.038 0.020 0.032 0.041l-2.681 10.26c-0.327 1.246-1.467 2.117-2.773 2.117zM47.106 20.653c-0.002-1.616 1.115-2.976 3.040-2.979s3.046 1.354 3.049 2.969c0.004 1.667-1.112 2.951-3.038 2.954s-3.046-1.277-3.051-2.944zM9.305 8.496v15.101h-5.108v-15.101h-4.197v-4.264h13.5v4.264h-4.195zM13.213 10.422h4.717v2.157c0.503-0.787 1.121-1.391 1.849-1.811 0.73-0.419 1.582-0.629 2.556-0.629h0.403c0.146 0 0.316 0.017 0.507 0.051v4.443c-0.624-0.308-1.303-0.463-2.032-0.463-1.095 0-1.917 0.321-2.463 0.963-0.548 0.642-0.822 1.579-0.822 2.812v5.65h-4.717v-13.175zM29.031 23.597h-4.717v-13.175h4.717v13.175zM24.002 5.363c0-0.36 0.070-0.697 0.208-1.015 0.14-0.316 0.331-0.595 0.573-0.835 0.244-0.239 0.526-0.428 0.847-0.565 0.323-0.137 0.664-0.206 1.030-0.206s0.709 0.069 1.030 0.206c0.321 0.137 0.603 0.325 0.847 0.565 0.242 0.24 0.433 0.518 0.573 0.835 0.138 0.317 0.208 0.655 0.208 1.015s-0.070 0.698-0.208 1.014c-0.14 0.317-0.331 0.595-0.573 0.835-0.244 0.24-0.526 0.428-0.847 0.565s-0.664 0.205-1.030 0.205-0.707-0.068-1.030-0.205c-0.321-0.137-0.603-0.326-0.847-0.565-0.242-0.24-0.433-0.518-0.573-0.835-0.138-0.316-0.208-0.655-0.208-1.014zM41.357 17.048c0-0.411-0.074-0.8-0.221-1.169-0.149-0.368-0.361-0.689-0.639-0.963s-0.603-0.492-0.977-0.655-0.779-0.244-1.212-0.244c-0.416 0-0.807 0.077-1.172 0.231-0.365 0.154-0.684 0.368-0.951 0.641-0.27 0.274-0.488 0.595-0.652 0.963-0.166 0.368-0.248 0.757-0.248 1.168s0.079 0.796 0.236 1.155c0.155 0.36 0.374 0.673 0.652 0.938 0.276 0.265 0.603 0.479 0.977 0.642s0.769 0.244 1.185 0.244c0.418 0 0.809-0.077 1.174-0.231s0.681-0.368 0.951-0.642c0.27-0.274 0.486-0.591 0.652-0.95s0.246-0.735 0.246-1.13zM35.417 30.171h-4.717v-19.749h4.717v1.463c1.006-1.25 2.38-1.875 4.116-1.875 0.955 0 1.847 0.185 2.671 0.552 0.826 0.369 1.543 0.87 2.151 1.502 0.607 0.634 1.081 1.379 1.42 2.235s0.51 1.772 0.51 2.748c0 0.976-0.17 1.884-0.51 2.722s-0.809 1.575-1.407 2.209c-0.599 0.634-1.308 1.13-2.123 1.49-0.817 0.359-1.703 0.539-2.658 0.539-1.669 0-3.059-0.565-4.169-1.694v7.858z"

Sorry I tried but no results, maybe I'm missing something with options object, thanks