Animate a fish

Draw an orange fish, then make it swim, wag its tail, bob and blow bubbles using gentle SVG animation. No code to run, just markup you can edit.

Ages 8 to 11, about 35 minutes. You will practice: How to draw a fish from simple shapes, How animate changes one number over time, How animateTransform moves and turns a shape, How to combine several slow animations in one scene.

Goal: make a fish that swims across the water, wags its tail and blows bubbles.

SVG can animate by itself. You describe what should change and how long it should take, and the browser does the rest. This type of animation is called SMIL (say “smile”). It works inside the live examples here. All the motion in this project is slow on purpose, because slow movement is calmer to watch.

Step 1: Draw the fish

A fish is an ellipse for the body, a triangle for the tail and a small circle for the eye. The tail goes first, so the body covers the point where they join. Everything sits in a group, <g>, so we can move the whole fish later.

Try this: change the body color and the tail color to your own favorites. Try making the body ry="22" for a slimmer fish.

Step 2: Bubbles that rise

The <animate> element changes one thing over time. Put it inside the shape you want to change. attributeName says what to change. from and to are the start and end values. dur is how long the trip takes. repeatCount="indefinite" means it repeats for as long as the picture is open.

Here the bubble’s cy, its up and down position, goes from 170 to 40 in 6 seconds. Smaller numbers are higher up.

Try this: add a second bubble with cx="245", r="4" and dur="8s". Bubbles that move at different speeds look more real.

Step 3: Swim across the water

animateTransform moves, turns or resizes a whole group. With type="translate", the two numbers are how far to move across and down. Here the fish starts well to the left of the picture, at -180 0, and ends well to the right, at 250 0. It swims out of view on one side and slides back in from the other. The trip takes 20 seconds, which is a calm pace.

Try this: change 20s to 40s for a sleepy fish, or to 12s for a fast one. Which speed feels best?

Step 4: Wag the tail

To wag the tail, we turn only the tail. type="rotate" takes three numbers: the angle, then the x and y of the point to turn around. The tail joins the body at 90,100, so that is the pivot point. The tail swings from minus 12 degrees to 12 degrees and back, and a full wag takes 2 seconds.

Try this: change the angles from 12 to 6 for a shy wag. Keep the three values in the same pattern: first and last the same, middle the opposite.

Step 5: Bob, seaweed and sand

Real fish do not swim in a perfectly straight line. A second group inside the first one adds a gentle up and down bob. The seaweed sways by turning slowly around its root. Because the sand is drawn last, it hides the bottom of the seaweed.

Try this: make a school of fish. Copy the fish group, change its colors and give it a different dur and a different from value so the fish are not in a line.

For teachers and families

Ask: “Which of the animations made the fish look most alive, and why do you think so?” As a classroom extension, talk about how a flip book works. Animation is a change over time, and SVG describes the start, the end and how long it takes. Children can predict what will happen before they edit a number, then test the prediction. If any child finds movement tiring, they can delete the animation lines and keep a still picture. Next, try A bouncing ball.

Next project: A bouncing ball