Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 983 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

GIF Loop Coder - Animating with Arrays

5:28 JavaScript lesson by

In this lesson, we discuss animating using arrays, and how different data types are interpolated while animating.


egghead.io comment guidelines

Avatar
egghead.io

In this lesson, we discuss animating using arrays, and how different data types are interpolated while animating.

Since GIF Loop Coder's focus is on animation, it makes sense to dive in a bit on how to animate objects. Again, animation is produced by changing something over time. The simplest method of animating in GLC is to supply a two-value array to a property.

Here, I'll create a rectangle centered on the canvas. The width will be 100, and the height I'll give an array of 50, 200. You can see that the height of the box is animating.

Now, you can animate almost any property of an object, and you can animate as many properties as you want on the same object. I'll change the width to an array of 200, 50. Note that the numbers are reversed compared to the height, so that when the width is 200, the height will be 50 and vice versa. This gives an organic squishy-looking effect.

You could continue to animate other properties on the same object such as the X and Y location, etc. All those animations will compile into the final product.

Animating between two numbers is pretty straightforward, but what about other types of properties? In the previous video, we also animated colors. Again, this is as simple as passing in an array of two color values to a color style property. Here, I'll set fillStyle to red, yellow. Now, the rectangle will smoothly move between those two colors.

I mentioned before that color management is one of the strong points of GLC. We'll see a bit of that here.

In addition to named colors like red and yellow, you can use an animate with RGB-formatted strings in both three and six digits. You can change the array to something like #0F0 for green and #0000FF for blue, or you can use RGB and RGBA strings like RGB(255, 255, 0) and RGBA(0, 255, 0, 0.75).

You can mix and match with all of these different formats like pale violet, red, and #F38702. It's all good. If that's not enough, there's a color module that I talk about in another video that'll give you even more options.

Now, I said that most properties are animatable, but what about Boolean properties like stroke and fill? Obviously, you can't smoothly interpolate between true and false, so what GLC does is choose one of those values based on the current animation time.

I've removed all the other animation properties on this rectangle and set the fill style to yellow. I'm going to set the stroke to an array of false, true and set the line width up to around 20 so we can see it clearly. When we run this, we set the stroke blinking on and off.

Let's pause the animation and scrub through it to see what's going on. At zero, we get the first value, false, so there's no stroke. At 0.5, we get true, so we have a stroke. Then back to false, no stroke, at one. If we go slower, we can see that there's no stroke right up until 0.25, which is halfway to 0.5. Then, the stroke switches on and stays on until 0.75, which is halfway from 05 to 1.

Admittedly, this isn't quite as useful as interpolating between numbers or colors, but it's there if you need it.

I'll quickly mention the string data type, as only one object uses it -- text. I'll add some text and set its text property to an array containing the strings "hello" and "world." Now, there are different strategies to interpolate between two strings.

In practice, in an animation, it looks like a chaotic jumble of random letters that only makes sense for the briefest of moments at the start and end of the animation, so strings take the same strategy as Booleans. Here, you'll see "hello" for the first part of the animation and "world" for the next part.

The last data type we'll mention is arrays. Now, there are a couple of properties that take arrays as their base values. One of these is the path property of a path object, which holds an array of point positions. The other is line-dash, which is an array of numbers used to define a dotted or dashed line.

I've created a simple path here so you can see how that works. Each number is an X- or Y-coordinate of a point in the path. Here, we have eight numbers forming four points. You can see the path they create.

Now, since we use arrays for holding animated properties and we use an array for paths, we need to pass an array of two arrays to animate a path. I'll copy this path and put both copies inside of another array. Then, I'll just randomly change some of the numbers.

When we run this, GLC will go through each element for the array and interpolate between them on each frame, forming a new path and rendering that. You can see how this path animates.

Next up, you might ask, "What if you pass an array with more than two parameters?" It would be nice if GLC smoothly interpolated between all values, but that's not what happens, at least not at this point. What happens is more like the Boolean example. If you pass an array of three elements, it will choose one of those three elements depending on the current animation progress.

Let's make a new sketch and add a polygon with addpoly. I'll increase the radius and give fillStyle an array of red, green, and blue. Running it, we can see those three colors flashing.

Scrubbing through it, we can see that it gets red for the first third of the animation between zero and five, green for the second third, and then blue for the last, and then back through blue, green, and red for the second half of the animation.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?