Oh fiddlesticks
Something's gone wrong here. Try again and see if it works, if it doesn't you're allowed to shout at me.

Flying reindeer animation (“Iapetus”)

Posted: August 14th, 2015

Greetings one and all, today we’ll be taking a look at another bit of concepting/messing around from the start of 2015. This follows on from the settling snowfall effect (“Mimas”) I’ve covered previously. This project was started directly after Mimas, but never reached the same level of completion. Following the years theme of moons of Saturn, this project was called Iapetus.

Flying reindeer animation lapetus

The idea was simple: animate a sleigh flying through the sky and dropping presents on to houses. Another neat use of HTML5 canvas to create a little background effect.

But that wasn’t enough! I wanted to animate the reindeer’s limbs and head so they actually looked like they were moving through the sky. I initially considered recreating a spritesheet system like the one I used to use in Haxe games – I ported this to Javascript for a concept piece way back in 2013 and the performance wasn’t bad, and that was using DOM. But hey, the canvas gives us all the tools we need to make this work, so I went to work implementing something.

Bored of all this talking already? Go look at it running then.

Incidentally this is yet another project where I found a new Javascript OOP pattern I liked and had a play with it. I actually really like this OOP shim created by John Resig, it provides inheritance in all the ways you’d expect, function overriding with parent access, and consistent this values – it’s the closest I’ve felt to something like C# or Java while writing Javascript. Those 25 magical lines of code allow you to do brilliant things…

 

 

Above we create a “class” called AnimClip by using the extend method of the basic Class object. Constructors in John Resig’s system are provided by way of an init method, and in there we use jQuery’s $.extend method to merge default options with anything passed in arguments. Below that, we extend AnimClip to create BezierPosAnimClip – which provides the same animation system, but along a bezier curve – and in that constructor we again use $.extend to merge a new, additional option in to any arguments, and then send that object to _super, which is the AnimClip constructor.

Moving right along, how does this unholy mess of ragdolling reindeer work?

Simple really. The canvas – like many similar graphical constructs – is manipulated by way of a transformation matrix. Look these up if I’ve lost you already: Matrix math is a bit weird at first but it’s good to have an understanding of how 2D graphics applications achieve some of the things they do. Luckily in this case you don’t have to dive in to the maths (well, unless you want to…) because Javascript provides simple methods for transforming the canvas.

Suppose we have an image, and we want to draw it at (100,100), and rotate it 90 degrees? To achieve this we must move the entire canvas by (-100,-100), then rotate it -90degs, draw the image, rotate +90degs, and move (+100,+100) to get back to where we started. You can work out the state of the matrix after each transformation as a homework assignment.

So that’s great, but what if we have an image that has a “sub image” which needs to be affected by the parents position and rotation, but also has it’s own properties to add to that? We need to burrow down through the object chain, applying each parent object’s transformations on the way down and reversing them on the way up so everything is properly nested. In practise, this isn’t so hard.

My code has a class IGameObject which holds all the data for a given object: position, rotation, center of rotation, scale etc, as well as an array of children. Each frame we enter the update method and if we have children we use a pair of methods applyGlobalRotate and endGlobalRotate, which transforms the canvas for the current object so we can call in to the child objects to do the same. An extension of this, IAnimObject, adds an array of IAnimClips and code to utilize them so we can animate our different body parts. This little animation system reads an array of AnimClips each frame, which each manipulate a single property between a min and max value, optionally with one of jQuery’s easing functions.

 

flying reindeer animation lapetus

 

To animate our reindeer we split him in to several parts: the body comprises our main object, and the two pairs of legs and head are children of the body. The initialization of a reindeer looks like this:

 

 

Pretty neat, I thought. In an ideal world of course the scale parameter from the parent object would also affect the children, but prototypes will be prototypes…

 

The only other part of the initial idea implemented was a very basic flyover animation using a linear Bézier curve as the route. No looping or reset occurs, the animation runs once. The last thing I remember working on in this code was making the individual reindeer and sleigh rotate to match the curve so they aren’t just a flat plane, but I clearly never got this done.

The Bézier code in question is actually ported again from some old Haxe code (in particular the Pinball project, which featured entire arenas designed with linked Béziers), and is quite neat and simple. Here’s everything you need:

 

 

The Bézier object takes three vertices as parameters: the start, middle and end point, which influence the path of your line. A call to getPoint() with 0 < t < 1 will return the position along the Bézier path denoted by t.

Integration with jQuery’s easing functions is super simple, too…

 

 

jQuery’s easings look like the above, with five parameters. In order they are: unused, current time, start value, end value and total duration. Sneakily, and very usefully, we can just send our current percentage completion and we’ll get the same animation effect as doing it any other way.

 

 

That’s probably about all there is to this code – as I say, a prototype that was cut short. I had a basic background scene ready to start dropping presents on but never got that far.

Go and take a look at Iapetus, the flying reindeer animation, and view source if you’d like. You have a little selection of buttons to view the sleigh “up close” or the flyover animation, set the easing for all animations to anything in the jQuery library, as well as a debug toggle to show the points of rotation for each object. FYI the “default” animation easing’s as designed are mostly linear, the only exception being $.easeInOutQuad on the reindeer heads.

 

– B