Archive for the 'Animated Emails' Category :

SVG animation

Tuesday, February 4th, 2014 by Anna Yeaman

Going from a static SVG to an animated SVG is a big jump, unless you want to float a balloon up the screen (even that takes a bijillion lines of code). There’s three ways to animate an SVG: CSS, native SVG animation and JavaScript. As email devs we can’t use JavaScript, so we started with the more familiar CSS.

 

Processing power

Our first test translated the fox across the screen using CSS animation. It was jerky in the iOS email client as mobile devices don’t have the processing power of a desktop computer. It wasn’t the file size which was pretty low, but how long it takes to redraw all that data each frame.

SVG in email animation

It needs to calculate all those positions each time, and then all the gradients to fill the shapes. Scalable vectors are generated on the fly which takes processing power, and unlike video or JPEG compression there’s no hardware assistance in rendering the SVG.

The fox artwork could have been more lightweight. The shape on the left will draw quicker than the more detailed shape on the right, as polygons = data = performance.

SVG in email

 

SVG animation vs. CSS animation

After that first test we switched to native SVG animation.¬†We didn’t benchmark it, but just by eyeballing it SVG animation appeared to have the edge. While basic transformations are covered with either technique, they do have different capabilities. Also the way you’d code them with the CSS in the head has implications for email designers.

We didn’t base64 the demos below, so you can take a look at the source code. Though you can base64 SVG animation and send via email, just like we did for the static tests.

 

Ease in, ease out?

Without being able to ease in and out you’ll end up with really crude animation. Luckily we can do easing and faring with SVG animation, as you can see with Graeme’s bouncing ball.

SVG animation is time-based, you set the duration of the animation, e.g 10 seconds from start to finish. All keytimes are in the range of 0-1, so if you had 10 keyframes in our 10 sec animation you could set keyframe 1 at 0.1 sec, keyframe 2 at 0.3 and keyframe 3 at 0.4 ect.

You can also set keysplines, this allows you to control the animation more precisely. It sets the rate of change between key values, and is governed by keytimes allowing for easing and faring. Its a bit tricky as there’s no graphical representation of the spline, so you just guesstimate.

SVG in email animation

 

Animate polygon points?

If we can manipulate the points of a polygon we can create slick animations. Here we created an SVG with 10 points which morphs into various shapes, such as a star into a square. Each keyframe has to have the same number of vertices, in the same order. As it’s tweening we get a smooth transition, because its drawing all the in-between states. We are using keysplines here also, to control the motion so its not just linear.

SVG in email animation

It’s fairly efficient (5K), though you do have to specify the new point positions for each keyframe so it’s not without overhead. Unfortunately we could only get vertex animation to work under Firefox, which rules it out.

 

Paperdoll

We can’t animate the points of a polygon, only the polygons themselves. So the type of animation we can create is what I think of as paperdoll animation, whereby each limb is on a hinge.

SVG in email

In order to get the fox tail to thump (every 16sec) we created two groups for the tail, back and front. We then set the anchor point for each group - like the silver pins on the mermaid - which you pivot around. The tail animation consists of a rotate and translate, with the back and front synced. There’s no IK skeleton, no ‘parent group’ so you just have to wing it.

SVG animation

To get the fox to blink (every 6sec), Graeme duplicated the eye polygons to make eyelids and then animated them. It’s a simple transformation in X and Y, again governed by keysplines to get the snap. The eyelid travels from behind the brow, along the edge of the nose. The face polygons had to be reordered to hide the eyelids behind the brow, but in front of the eye.

Graeme did some similar tweaking with the tail, as there were no hind leg or paws behind it just dead space. Obviously when planning this type of animation, you’d take overlapping into consideration. Here we just used artwork that was on-hand for testing, and it needed reworking.

Implementing something more complicated like a walk-cycle might be difficult without an interface of some sort. Though it’s very efficient as you are translating just one set of points (the tail is not changing shape just moving around).

Here it went from 18K when static, to just 22K with the blink, and 24K with both the blink and tail thump. As base64 code it rises to 31K, you might have to do this conversion to send via your ESP. SVG animation is supported under the iOS and Android email client (and looked nice and smooth btw). There’s a more detailed SVG email support chart here.

 

Isolated groups or whole SVGs

The most accessible type of SVG animation is whereby you transform a whole group that moves independently. Such as a balloon moving upwards or a spinning wheel. You can layer on translate, rotate, scale, show, hide, color changes to nice effect like on madebyfieldwork.

 

What’s possible…

I’d seen the Playground SVG animations, and wondered if we could do anything approaching it in email. With these tests, we were just trying to figure out the level of sophistication we could achieve. While the lack of JavaScript is a big disadvantage, we’re not totally out of the game.

The ‘paperdoll’ technique has promise, its efficient and with the right artists you could create some lovely work. It’s a shame about vertex animation, but there’s one other avenue open to us which I’ll look at next.

Update here’s part one Basics of SVG in Email and part three 3D to SVG