Creating Beautiful Drawings Using Sencha Ext JS - Part 2

In Part One of this series, we learned about sprites and their attributes, how they differ from configs, and how to change them. All of the changes we made to our examples were applied immediately. In this article, we’ll look at animating our sprites.

Sprite Animations

In the Draw package, we animate sprites using animation modifiers that apply changes to sprite attributes.

The animation modifier of a sprite can be accessed via its getAnimation accessor method. The two main configs of an animation modifier are duration and easing. Let’s use those to animate a sample circle sprite:

  1. circleSprite.setAnimation({
  2. duration: 1000,
  3. easing: 'elasticOut'
  4. });
  5. // Now this change to circle radius will animate automatically.
  6. circleSprite.setAttributes({
  7. r: 100
  8. });

Open in a fiddle

elasticOut is a built-in easing function. You can see all of these functions with previews in this Charts KitchenSink example.

Additionally, multiple attributes can be animated at once. For example, let’s change our initial three values to the following:

  1. cx: 50,
  2. cy: 50,
  3. r: 30

Let’s also modify the easing:

  1. easing: 'bounceOut'

Finally, let’s animate both horizontal and vertical centers of the circle:

  1. circleSprite.setAttributes({
  2. cx: 200,
  3. cy: 200
  4. });

image alt text

Open in a fiddle

Notice how both attributes animate in sync, as both attributes use the same easing and animation duration.

But what if we want to have different animation durations for different attributes? We can do this using the customDurations config of the animation modifier:

  1. circleSprite.setAnimation({
  2. duration: 1000,
  3. easing: 'bounceOut',
  4. customDurations: {
  5. cy: 3000
  6. }
  7. });

image alt text

Open in a fiddle

This will make the vertical center attribute (cy) animate 3 times more slowly than other sprite attributes (in this case, horizontal center, cx).

Finally, we can have custom easings assigned to specific attributes as well:

  1. customEasings: {
  2. cx: 'easeOut'
  3. }

image alt text

Open in a fiddle

It’s important to note that animation is not limited to the simple number attributes we’ve examined in this guide. You can also animate colors, arrays, and even text!

For example, try animating color and array changes:

image alt text

Open in a fiddle

Conclusion

As you can see, animating sprites is incredibly easy and flexible. The animation API is powerful enough to create beautiful and realistic animations right out of the box!

Have fun experimenting!