I've been looking around and I can't seem to find a clear way in which I can animate a shape to change from a circle to a triangle or to a rectangle or the other way around. I would assume that I could somehow store the shape and change its attributes in order to convert it.
Basically what I am asking is, how can I draw a circle and then animate it to a triangle on the click of a button? Is that possible with canvas shapes?
Thanks!
It is hard to understand your exact aim and what kind of animation you need between shapes as you don't specify animation steps/states. But you could take a look at CSS3 transitions.
I made a few in the following demo that illustrate how you can simulate "shape morphing" (is this the corect term?) if that is what you are trying to do :
DEMO (click on the circles to animate them)
For the circle to rectangle, the transition is pretty easy to achieve, you just need to animate border-radius :
CSS :
For the circle to triangle, the shape transition is a bit harder to achieve, the shape morphing isn't perfect and needs improvement but you can get the idea.
The circle and the triangle are set with pseudo elements and animations display one or the other with 2 different transitions :
CSS :
You might want to use Raphael (https://dmitrybaranovskiy.github.io/raphael/) or a similar library to take care of this for you. The raphael site contains many examples similar to the case you describe.
( I will award @rje because he got me in the right direction and after all the effort I realized that my question wasn't detailed enough with the specifics of my issue ).
First I will explain what I was actually looking for: Basically when creating a "shape" in a canvas context it's pretty easy to use that as a mask, by mask I mean, let's say you draw a circle in the middle of the canvas which remains transparent but the canvas is filled with a certain color. Using raphael js directly is kind of strange to do this until you get the hang of it because it's actually svg and there are different rules. You have to draw a outer path which will match the paper ( the raphael "canvas" ) and an inner path which will remain transparent. The challenge for me was to make it also responsive But I managed to do it after all using something like this
The great thing about this is that using Raphael you can animate them and also use for example
innerpath
for creating another shape and use another color to fill that. Amazing.Thanks everybody.
Here's one approach you can use to animate a circle to-and-from any regular polygon.
Create your triangle (or any regular polygon) using quadratic curves for each side.
That way you can animate the quadratic curve's control point to control the "roundness" of your polygon.
If the control point is on the polygon line, then the curve has zero roundness and the result is your polygon.
If the control point is appropriately outside the polygon line, then the curve approximates the roundness of a circle and the result is your polygon looks like a circle.
To "morph" between circle-polygon, you just animate the control point of the curve.
I use "approximates" to describe the circle because quadratic curves can only approximate a circle. If you want an even better circle, you can refactor my code to use cubic Bezier curves instead of quadratic curves.
Here's example code and a Demo: http://jsfiddle.net/m1erickson/NMJ58/