How to Make Animations

From Math Images

Jump to: navigation, search
Welcome to the Animation Tricks page!

Here you can find how to make some of the cool animations on this wiki.

Feel free to add to this page if you know some other ways to make animations.



Contents



Introduction


As Math Imagers, sometimes we would really like to make our images move. Moving pictures not only look better in our pages, but also explain our ideas more clearly and vividly. For example, see the following wiki pages made by previous Math Imagers:

Figure 1Envelope
Figure 1
Envelope


In this page, animations are used to visually show the readers how to generate envelopes of lines and curves, which is a rather abstract mathematical concept. It would be really hard to do this using still images.
Figure 2Involute
Figure 2
Involute
2. Involute

In this page, animations are used to show the dynamic process of unwinding one curve from another.
Figure 3Simple Harmonic Motion
Figure 3
Simple Harmonic Motion


In this page, animations grasp the readers' attention by presenting some of the interesting real life applications of simple harmonic motion.


There are many kinds of dynamic visuals, but our Math Images site only supports 3 of them: GIF (Graphics Interchange Format), SWF (ShockWave Flash), and Java applets. Among these three, GIF is the easiest one to make, so it's mostly used in our image pages. The 3 examples above are all GIF animations.

SWF and Java applets are more powerful than GIF, but making them requires much more technical knowledge, which may take several weeks, or even months to learn. Since most Math Image researchers only have 10 weeks, and writing image pages itself is no easy task, we shouldn't spend half of the summer learning how to make a Java applet. For now, the best choice would be to only focus on GIF animations. If you really need a SWF or Java applet for your page, you can ask for help from the Drexel/RPI folks.

Due to the reasons discussed above, most of this page will be devoted to GIF animations, though occasionally we will mention SWF and Java applets as well.


How GIF Animations Work


Before we try to make a GIF, we need to understand how GIF animations make things move.

In fact, a GIF is not a single moving image. It contains a series of "frames", in which each frame is slightly different from the previous one. When these frames are played rapidly from the beginning to the end, viewers will have the illusion that things are moving. It's kind of like a game we played in middle school. When students get bored of a class, they start to doodle on their textbooks. They draw the pictures carefully so that each doodle is slightly different from the one on previous page. When the book is flipped rapidly from the beginning to the end, the figures will move like a cartoon, as shown in the following image:

Click to stop animation.



GIF animations work in a similar way. When we make a GIF animation, we make its frames first, and then compress them into a .gif file. Those frames will start to play automatically when we put the GIF onto a website. The number of frames we make depends on the length of the animation, and the quality we want to achieve. To ensure that the motion looks smooth, at least 15 - 25 frames should be played in a second. For example, Figure 1 in the previous section plays only 8 frames per second, so the motion is a little bit chunky (to see this clearly, click the animation to enlarge). However, Figure 3 plays at a speed of 20 frames/sec, so the motion is very smooth, even in the enlarged version.

Now you probably understand why GIF is hard to make: because they simply have too many frames! To make a typical 4-second-long GIF animation, we have to draw 60 - 100 separate frames! Fortunately, there are some software that allow us to make/edit multiple frames at a time, and quickly compress them into a GIF file, as we are going to introduce in the following sections.


The First Animation - Introduction to Photoscape


Now we are ready to actually make some GIF animations. Let's start from a simple one like the following "HelloWorld" animation:


Animation 1: Hello, World!
Animation 1: Hello, World!



Obviously this animation contains 15 frames, one for each character. We can easily make these frames using almost any graphics editing software. I used Paint.Net v3.5. It's similar to Windows Paint, but with more power functions. Here is the download link:

http://www.getpaint.net/



Following are detailed steps of making the "HelloWorld" animation. Steps 1~7 show how to make the 15 frames, and steps 8~15 show how to compress these frames into a .gif file. Notice that you don't have to use Paint.Net to follow these steps. They can be done in almost any graphics editing software, including Windows Paint.



Step 1 : Open Paint.Net v3.5, or other graphics editing software. Start a new file, and set the canvas to a comfortable size. I used 600*600 pixels.


Step 1: Start a new fileClick the image to enlarge
Step 1: Start a new file

Click the image to enlarge





Step 2 : Use the text tool to write the first character "H". Use any font size you want. Drag it to a comfortable position.


Step 2: Write the first characterClick the image to enlarge
Step 2: Write the first character

Click the image to enlarge





Step 3 : Save this frame. Use the file name 1.png or frame1.png to note that it's the first frame. You can also use other formats like .jpg, but remember to be consistent for all frames.


(Forgive me for the Chinese characters in the screenshot. The Windows on my computer is a Chinese version.)


Step 3: Save the first frameClick the image to enlarge
Step 3: Save the first frame

Click the image to enlarge





Step 4 : Choose a different color and write the second character.


Step 4: Write the second characterClick the image to enlarge
Step 4: Write the second character

Click the image to enlarge





Step 5 : Save the second frame. Do not just click the "save" icon. Otherwise it overwrites the first frame that we already saved. Use the "save as" button shown in the following image. If you accidentally covered the previous frame, you can use the "undo" icon to return to the previous frame and cover it back.


Step 5: Save the second frame
Step 5: Save the second frame





Step 6 : Enter the file name for the second frame. We can see the already-saved first frame there.


Step 6: Name the second frameClick the image to enlarge
Step 6: Name the second frame

Click the image to enlarge





Step 7 : Keep doing this until you have all the frames. If you forget to save one frame, just use the "undo" icon to return to that frame, save it, and then use to "redo" icon to go back.


Step 7: All 15 frames are readyClick the image to enlarge
Step 7: All 15 frames are ready

Click the image to enlarge





Step 8 : Introduction to Photoscape


Since we have made all 15 frames, the task remains now is to compress them into a .gif file. There are many software that do this. The first one that came into my mind was Adobe Photoshop, because Photoshop is the graphics editing master. It literally does everything you want. However, I didn't choose it because of the following reasons:


1. It's not free. But this is not a big problem for Swarthmore students, since Swarthmore ITS provides access to all Adobe software. I'm not sure about the condition in other colleges and universities.


2. It's large and hard to install. The package provided by Swarthmore ITS never installed correctly on my computer, and I have seen same thing happening on other computers. Even if things worked out, it will take almost an hour to install.


3. The fact that Photoshop is so powerful makes it hard to learn. There are so many tools in Photoshop that people get lost in them.


Due to the problems stated above, I did not use Photoshop as my GIF compresser. Instead I used another program named Photoscape, which is free, convenient, easier than Photoshop, yet still adequate for most GIF animations we will make. Here is the download link:


http://www.photoscape.org/ps/main/index.php?lc=en


Open Photoscape after downloading and installing. We will see the main menu as shown in the following image. The icon in red circle leads us to the GIF compresser in Photoscape. We can also use the top button in the red box.



Step 8: Main menu of PhotoscapeClick the image to enlarge
Step 8: Main menu of Photoscape

Click the image to enlarge





Step 9 : Click the Animated GIF icon in Step 8, and we will see the following window:


Step 9: The GIF making tool in PhotoscapeClick the image to enlarge
Step 9: The GIF making tool in Photoscape

Click the image to enlarge





Step 10 : Open the folder that has the 15 frames in it, and drag photos into the preview zone to add frames. We can do this by dragging these frames one by one, from #1 to #15. The order matters here! We don't want these frames to play in a wrong order.


Notice that Photoscape will start to preview the GIF once we add a frame.



Step 10 (1): Adding frames one by oneClick the image to enlarge
Step 10 (1): Adding frames one by one

Click the image to enlarge



However, there is an easier way to add these frames. We can go to the frame folder, press Ctrl + A to select all frames, and drag them together into Photoscape. It will not make much difference here, since we only have 15 frames. However, when we make more complicated animations with more than 100 frames, it would be a huge relief if we can add all frames together.


One thing to keep in mind: When all frames are selected, dragging them with mouse on any frame will add all frames to Photoscape. BUT it is recommended that we drag them with mouse on the first frame, because that makes the animation start from the first frame. If we drag with mouse on a random frame, say frame 7, then the animation will start from frame 7. Of course we can fix it by rearranging these frames in the frame zone, but it's a waste of time and energy. Using this trick will ensure that the animation starts from the right place.



Step 10 (2): Adding all frames at a timeClick the image to enlarge
Step 10 (2): Adding all frames at a time

Click the image to enlarge





Step 11 : Select all frames in the frame zone (use Ctrl + A), and click the "Change time" button the change their display time. I used 0.5 second per frame, or 50 units here (one unit stands for 1/100 second).


Step 11: Change the display time of all framesClick the image to enlarge
Step 11: Change the display time of all frames

Click the image to enlarge





Step 12 : Sometimes we want to change the display time of only one frame. For instance, in the example "HelloWorld" GIF, I made the last frame to display longer (3 seconds) so that readers can get the big picture. To do this, select the last frame and click "Change time", or simply double click that frame. Remember to choose "change only display time of the selected frame" in the popped out window.


Step 12: Change the display time of one frameClick the image to enlarge
Step 12: Change the display time of one frame

Click the image to enlarge





Step 13 : Click the "save" button to save these frames as a GIF file, but Photoscape gives a warning, saying that the canvas size 600*600 is too large.


We can click "Yes" to continue, since it's not going to make much difference. Or we can click "No" and use the slider on the right to change canvas size. Notice that we can ignore this warning here because there are only 15 frames. For GIF animations with more frames, it would be better to use small canvas sizes (no larger than 500*500). See the Tradeoff between size and quality section for more details.


Step 13: Change the display time of one frameClick the image to enlarge
Step 13: Change the display time of one frame

Click the image to enlarge





Step 14 : Once the GIF is saved, we will get a .gif file in the designated folder. We can then upload it onto the Math Images Site, and use it like any other image files.


Step 14: The saved GIF fileClick the image to enlarge
Step 14: The saved GIF file

Click the image to enlarge





Step 15 : Use the Batch Editor in Photoscape


Photoscape is a wonderful software not only because it compresses GIF easily and efficiently, but also because it has a "batch editor" tool that allows us to edit multiple frames at a time. Suppose after making all frames, we suddenly want to add the author's name on every frame, and we don't want to edit these frames one by one, what shall we do? This problem is easily solved using the Batch Editor in Photoscape, as shown in the following image:


Step 15(1): The Batch Editor tool in PhotoscapeClick the image to enlarge
Step 15(1): The Batch Editor tool in Photoscape

Click the image to enlarge


Enter the Batch Editor, and drag the frames into Photoscape like what we did in step 10. Then click the "Object" button on the right, since we want to add an object into the frames.


Step 15(2): Add photosClick the image to enlarge
Step 15(2): Add photos

Click the image to enlarge


Check the box besides "Text 1", and enter the text we want to add. Then we can double click the text to change font / properties.


Step 15(3): Write textClick the image to enlarge
Step 15(3): Write text

Click the image to enlarge


Once we are done, click "convert all" to apply the change to all frames. The GIF made of these converted frames will look like the following:


The converted GIF
The converted GIF





Conclusion


Photoscape is an excellent GIF compresser. It's powerful, convenient, and easy to use. However, Photoscape can't help us to draw the frames of a GIF. In this "HelloWorld" example, we can use Paint to draw the frames because there are only 15 of them, and no complicated figures or careful alignments are involved. In more complicated animations, we may have to make more than 100 frames, each involving complicated geometric figures, whose positions need to be adjusted carefully so that the motion looks even and smooth. Obviously we need graphing softwares that are more advanced than Paint to handle this task. One such software is Wolfram Mathematica, which will be introduced in the next section.



Mathematica


Mathematica 8 LogoCopyright belongs to Wolfram Research, Inc.
Mathematica 8 Logo

Copyright belongs to Wolfram Research, Inc.

Wolfram Mathematica is a popular technical software program developed by Wolfram Research. It does almost everything that involves computing results. In fact, Mathematica is so powerful that its help book is larger than the whole Math Images site put together. However, in this page we will only briefly introduce Mathematica's "Graphics and Visualization" feature, since this will be enough for making frames of a GIF animation.


Some basic syntax first


Mathematica uses a "Notebook" interface. When you open Mathematica, you can always go to file -> new -> notebook to create a new notebook, enter codes and commands into the notebook, then execute them. The following image shows a typical Mathematica notebook:

An example Mathematica notebook
An example Mathematica notebook


As we can see in this example, Mathematica has its own coding language. We have to learn and use this syntax, otherwise Mathmematica won't recognize our commands. Unfortunately, Mathematica language is rather difficult and uncomfortable to learn, especially for those without previous coding experiences. But our effort will pay off. Learning Mathematica will benefit us greatly not only in the Math Image Project, but also in our future academic careers.

Following are some useful tips for the Mathematica syntax. Some of them may puzzle beginners for a long time.


1. "Enter" means "to return"; "Shift + Enter" means "to execute".

If you finish a line of code and hit the Enter key, no output will be shown, because in Mathematica "Enter" only means "return and start a new line". To execute the command you just typed in, use "Shift + Enter".






2. Use round brackets () to change the order of operations. Use square brackets [] for functions and commands. Use curly brackets {} for vectors, matrices, and other lists of parallel objects.

In handwriting math, we use round brackets for all these functions. But Mathematica uses different brackets for clarity.






3. All functions begin with capital letters.

For example, use "Sin" instead of "sin". Same for some important constants: "Pi" stands for π , "E" stands for the Euler's constant e , and "I" stands for the imaginary unit.




4. Using := to define functions.

In Mathematica, lhs (left hand side):= rhs (right hand side) defines rhs as lhs. Whenever lhs appears, it will be replaced by rhs, and evaluated afresh each time.


We can use := to define our own functions, as shown in the following image. This trick will be very useful in the following sections.






5. Using the Basic Math Assistant.

The Basic Math Assistant is in the "Palettes" menu. It includes the symbols, functions, special characters, and type settings that we frequently use in math writing.






6. How to ask for help.

Mathematica comes with a nice and detailed help book. If you want to know how to use a function or command, just type "?funtion" and execute. Mathematica will tell you the basics about this function.



If you don't know the name of the function you are looking for, you can use the "Function Navigator" in the "Help" menu, where all Mathematica functions are classified by features. Since we want to use Mathematica to draw the frames of a GIF, most of the functions we will use belong to the "Visualization & Graphics" section.





Example Animations


Now we know how Mathematica works. Let's write some codes and make some animations!


Example 1: Using the "Plot" command

One Mathematica command that is often used to make animations is the "Plot" command. For example, the following GIF shows the sinusoidal function y = sin(x + φ) plotted with changing φ:


Example 1Using the "Plot" command
Example 1
Using the "Plot" command


Here are the detailed steps of making this animation. First, open a new notebook and type "?Plot" to learn about this function. Users who already know this function can skip this and go on.


Step 1Learning the "Plot" function
Step 1
Learning the "Plot" function





Step 2: Following the format in the help page above, let's plot the function sin(x) from -2\pi to 2\pi :


Step 2Plot sin(x)
Step 2
Plot sin(x)





Step 3: When we do the plot, we can choose to use various different options. For example, we can tell Mathematica to hide the y - axis, use the blue color, and plot the curve in thick style, as shown in the following image. The complete list of options are listed under the "More Information" tag in the help page.


A special note: the right arrows are typed using ->, or "minus sign" + "greater than sign".


Step 3Change the options of a plot.
Step 3
Change the options of a plot.





Step 4: Next, we make the image move. To do this, we add a variable factor φ into the function sin(x), and use the "Manipulate" command that allows us to change this factor with the slider.


We can type φ in Mathematica using "\[phi]", or using the "Basic Math Assistant" tool in the "Palettes" menu.


Step 4Use "Manipulate" to make things move.
Step 4
Use "Manipulate" to make things move.





Step 5: Once we are satisfied with the effect in "Manipulate", we can start to save the frames. Use := to define a new function whose input is the value of our changing factor, and whose output is the frame corresponding to this value:


Step 5Save the frames
Step 5
Save the frames





Step 6: We can right-click these plots to save them as individual frames. However, there is an easier way to do this, using the "Export" command:


Step 6The "Export" command
Step 6
The "Export" command





Step 7: If we want to make 100 frames for this animation, we don't have to Export 100 times. There is a "Do" command in Mathematica that does this for us.


Read the following code carefully to see how different values of n lead to different frames and different file names.


Step 7The "Do" command
Step 7
The "Do" command



Step 8: Compress these frames into a GIF using Photoscape. I chose a display time of 0.04s / frame.



There are many other functions related to "Plot", such as "Plot3D", "ParametricPlot", "ContourPlot", "DensityPlot", and so on. Also, there are a lot of options that we didn't have time to explore, such as "ColorFunction" and "ImageSize". Following is an incredibly beautiful GIF created by the "ReliefPlot" command. It's the 2D version of the 1D sinusoidal wave in Example 1.

ReliefPlot of a 2D sinusoidal wave
ReliefPlot of a 2D sinusoidal wave



Here is the source code for reference:






Example 2: Using the "Graphics" command

We can make many wonderful animations using the "Plot" command and its relatives, but this "Plot" family is not omnipotent. There are things that they can't do. For example, see the following animation in the Simple Harmonic Motion page:

Example 2
Example 2



This animation contains 4 parts: the ceiling, the massless string (or rod), the massive bob, and the "Simple Pendulum" text. We may draw the line segments using ParametricPlot: just parametrize them and use a suitable range will do. But what about the solid ball and the text? They are beyond reach of the "Plot" family, since they are not the image of any known function.

Fortunately, there is a "Graphics" command in Mathematica that handles these objects: solid disks, solid spheres, cylinders, rectangles, texts, and so on. Type ?Graphics and we will see the following page:

The "Graphics" command
The "Graphics" command



Obviously this animation involves 3 Graphics primitives: "Line", "Disk", and "Text", as shown in the red boxes. But there is more to it. To ensure that the animation is physically correct, we need to solve the equation of motion for simple pendulums. In mechanics, it's a known fact that the period of a pendulum is independent of the bob's mass m. Instead it's determined by the length of the string l, and the gravitational constant g:

Figure 3-1
Figure 3-1


T = 2\pi \sqrt{l \over g}


And its equation of motion can be approximated to the following form:

\theta = \theta _0 \sin ( \sqrt{g \over l} \cdot t)


which makes it a simple harmonic motion. Here θ0 is the magnitude of the motion, or the maximum angle that the pendulum swings away from vertical. For this approximation to be valid, θ0 must be a small angle (usually no larger than 10 degrees).

Once we know the value of θ , we can determine the position of the massive bob P using basic trigonometry:

P (x,y) = (l \sin \theta, -l \cos \theta)


Now we have got all results needed in this animation. Let's translate them into Mathematica codes, as shown in the following notebook:

Source code for the simple pendulum animation
Source code for the simple pendulum animation



As we can see, this program is much longer than the previous ones we wrote, primarily because of the more complicated nature of this problem. Here we are going to break this program into single lines, and explain each line separately for clarity.

[1]: Although it doesn't really matter, we can still assume that mass of the bob is 1kg, just in case we need it somewhere.


[2]: We assume that the length of the string, or massless rod, is 1m.


[3]: The gravitational acceleration g on earth is 9.81 m/s2.


[4]: The magnitude θ0 is assumed to be π/18, or 10 degrees. It's large enough so that readers can see the motion clearly, yet not too large so we can still use the model of simple harmonic motion.


[5]: The approximate equation of motion.


[6]: The coordinate of P, center of the bob.


[7]: The period of this pendulum.


[8]: If we want to draw more than one items in a Graphics function, they must go into curly brackets. See the examples in the help page for more details.


[9]: This line draws the horizontal, thick line. Notice that the directives for a single primitive goes before that primitive with curly brackets around them. See the "More Information" tag in the help page for more details.


[10]: This line draws the thin thread, or massless rod. It's a line segment between the origin O, and center of the bob P, whose position we have determined in line 6.


[11]: The bob should be centered at P. The radius of the bob doesn't really matter, as long as it looks comfortable.


[12]: The "Simple Pendulum" text is put at position (0, -1.2) with a size of 30.














Geometer's Sketchpad

Tradeoff between Size and Quality

Personal tools