In this recipe, we will learn how to use the powerful timeline features of Cinder to create sequences of animations. We will draw a circle and animate the radius and color in a sequenced manner.
Include the necessary files to use the timeline, draw in OpenGL, and generate random numbers.
#include "cinder/gl/gl.h" #include "cinder/Timeline.h" #include "cinder/Rand.h"
Also, add the following useful using
statements:
using namespace ci; using namespace ci::app; using namespace std;
We will animate several parameters sequentially using the timeline. Perform the following steps to do so:
Anim<float> mRadius; Anim<Color> mColor; Vec2f mPos;
setup
method, initialize the members. Set the position to be at the center of the window, the radius as 30, and a random color using the HSV color mode.mPos = (Vec2f)getWindowCenter(); mRadius = 30.0f; mColor = Color( CM_HSV, randFloat(), 1.0f, 1.0f );
draw
method, we will clear the background with black and draw the circle using the previously defined members.gl::clear( Color::black() ); gl::color( mColor.value() ); gl::drawSolidCircle( mPos, mRadius.value() );
mouseDown
event handler.void mouseDown( MouseEvent event );
mouseDown
, we will apply the animations to the main timeline.We will first animate mRadius
from 30 to 200 and append another animation to mRadius
from 200 to 30.
Add the following code snippet to the mouseDown
method:
timeline().apply( &mRadius, 30.0f, 200.0f, 2.0f, EaseInOutCubic() ); timeline().appendTo( &mRadius, 200.0f, 30.0f, 1.0f, EaseInOutCubic() );
mColor
and then append this animation to mRadius
.Add the following code snippet inside the mouseDown
method:
Color targetColor = Color( CM_HSV, randFloat(), 1.0f, 1.0f ); timeline().apply( &mColor, targetColor, 1.0f, EaseInQuad() ).appendTo( &mRadius );
Appending animations is a powerful and easy way to create complex animation sequences.
In step 5 we append an animation to mRadius
using the following line of code:
timeline().appendTo( &mRadius, 200.0f, 30.0f, 1.0f, EaseInOutCubic() );
This means this animation will only occur after the previous mRadius
animation has finished.
In step 6 we append the mColor
animation to mRadius
using the following line of code:
timeline().apply( &mColor, targetColor, 1.0f, EaseInQuad() ).appendTo( &mRadius );
This means the mColor
animation will only occur when the previous mRadius
animation has finished.
When appending two different animations, it is possible to offset the start time by defining the offset seconds as a second parameter.
So, for example, change the line in step 6 to read the following:
timeline().apply( &mColor, targetColor, 1.0f, EaseInQuad() ).appendTo( &mRadius, -0.5f );
This would mean that the mColor
animation would begin 0.5 seconds before mRadius
has finished.
3.12.76.164