Animating WatzThis With SVG
Now that you understand how to use SVG in HTML, let's use that knowledge to add a cool animation to WatzThis.com!
Since this is our first experiment with animation in WatzThis, let's start small and simple. How about a circle that just moves around a bit between the WatzThis header and the body content?
There are three different ways to animate SVG:
- JavaScript
- CSS
- Synchronized Multimedia Integration Language (SMIL)
SMIL is the language that's specifically built for animating SVG, and it's perhaps the simplest way. So we'll use that for our example. If you're interested in finding out the other ways of animating SVG, I've added a link to a good article on the topic in the Supplementary Material.
SMIL animates SVG graphics by changing the value of attributes over time using the <animate> element. Let's take a quick look at <animate> in action. Here's the SVG and SMIL code for creating and moving a circle.
Take a good look at this code. Care to take a guess as to what it'll do?
If you guessed that the circle will transform into a pterodactyl, you guessed wrong—but you get points for creativity.
If you guessed that the circle will move from its starting point along the X axis (horizontally) until the cx attribute has a value of 0, that it will take three seconds to do this, and that it will then repeat this same movement 10 times, you guessed exactly right! How did you know all that?
To try it out in your browser, open the circle.svg file that you just created and add the <animate> element between the beginning and ending tags of the <circle> element.
Adding the <animate> element to the <circle>
The <animate> element can manipulate other attributes as well. Remember that SVG graphics are essentially just math. So you can manipulate any of their properties to create unlimited types of animated effects.
If you wanted to make a circle that starts small and slowly gets larger, you could do that using the following code:
Now, to put an animation into the WatzThis app, open your latest version of index.html in Komodo Edit and insert an object element right above the div containing the directions. So find the part that currently reads:
Change it to:
Save the file.
If the circle.svg file is in the same directory as index.html, and if you typed everything correctly, you can preview the app in your browser and see the animation. Here's a still picture of how it looks on an iPhone:
WatzThis app with an animated circle
Not bad, right? But it's hardly the sort of thing that people are going to forward to their entire address book. What would be super-cool would be if you had some sort of character that was moving across the screen. A quick search of a free clip art website (OpenClipArt.org) turns up just the thing.
Copy the following code into a new file in Komodo Edit, and save it as animated_ghost.svg in the same folder as the watzthis.com index.html.
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg">
<g>
<title>Ghost</title>
<svg x="-100">
<g>
<path id="path1928" fill="#DCE2E6" stroke="#000000" stroke-width="0.6828" d="m61.9289,50.94758c-0.1623,9.87308 6.14767,18.35825 6.81532,28.1152c-0.4594,3.60356 4.28075,9.08076 0.44525,11.07582c-4.0636,-2.49697 -5.21735,-7.93875 -8.25023,-11.52047c-3.76081,-5.90585 -6.77587,-15.18637 -11.89385,-17.31176c-3.3587,-1.39478 -1.75969,6.9068 -0.5946,10.06939c1.5126,5.57819 2.83635,11.23289 3.6307,16.96872c-0.0917,3.14579 -2.2539,11.66516 -6.29502,7.40224c-2.1521,-6.28045 -2.70217,-12.96525 -4.54059,-19.33556c-1.10762,-2.63072 -1.37718,-8.6926 -3.81768,-8.9211c-3.87983,3.37206 -2.20068,9.13503 -3.93305,13.52342c-0.94734,4.60616 -1.37111,9.33416 -2.83837,13.82274c-3.02578,-4.96761 -2.63397,-11.04386 -4.42889,-16.43405c-1.76009,-8.48392 -1.49659,-25.50752 -4.62679,-25.57714c-3.62948,-0.08034 -1.22863,19.29771 -5.97283,27.99073c-1.22035,2.06507 -2.91164,8.45481 -5.24569,6.98109c-2.01449,-4.41856 1.58647,-9.3888 -0.81336,-13.7351c-2.13408,-7.53034 -4.03626,-15.22704 -3.74463,-23.11802c-0.07225,-14.64216 4.28397,-30.24847 15.42943,-40.34114c6.83051,-5.84816 17.60137,-7.2966 25.0983,-1.84672c8.63189,5.62818 13.80269,15.41243 15.90016,25.28693c0.90079,5.60348 0.40031,11.31281 -0.3236,16.90478l0.00002,0z"/>
<path id="path2823" stroke="#000000" stroke-width="0.6828" d="m46.14694,20.93468c0,1.32295 -1.00928,2.39536 -2.25431,2.39536c-1.24524,0 -2.25472,-1.0724 -2.25472,-2.39536s1.00927,-2.39536 2.25472,-2.39536c1.24503,0 2.25431,1.0722 2.25431,2.39536z"/>
<path id="path2825" stroke="#000000" stroke-width="0.6828" d="m29.28912,20.93468c0,1.32295 -1.00927,2.39536 -2.25451,2.39536c-1.24503,0 -2.2545,-1.0724 -2.2545,-2.39536s1.00926,-2.39536 2.2545,-2.39536c1.24524,0 2.25451,1.0722 2.25451,2.39536z"/>
<path id="path2827" stroke="#000000" stroke-width="0.538" d="m46.44362,36.29185c0,1.24888 -2.54715,8.198 -11.35429,8.198c-8.80675,0 -10.37479,-6.67023 -10.37479,-7.91931c0,-1.24889 1.56804,2.54553 10.37479,2.54553c8.80716,0 11.35429,-4.07309 11.35429,-2.82421l0,-0.00001z"/>
</g>
<animate attributeName="x" to="350" dur="3s" repeatCount="10" />
</svg>
</g>
</svg>
Change the <object> element in index.html to the following:
Now when you preview the app, you'll see something like this:
WatzThis with an animated ghost!
Try pressing the OFF button in WatzThis for a truly scary effect.
Ghost on a black background
in WatzThis app
The boss is sure to be impressed with animations like these!
What kinds of animated images would work for the apps you plan to develop?
Please join me in Chapter 5, and we'll wrap up another lesson.