Apps, desktop toys and computer sound

Make a browser pet with sprites and JavaScript

Published

Make a browser pet from four drawings and a short repeating piece of code. The working bird example walks inside its own strip, turns at the edges, blinks and rests. A slider changes the speed; a button starts or pauses it.

On this page

Download the source and sprite sheet to replace the bird or change how it moves. The sprite sheet holds all four poses in one image. The pet stays inside the webpage, and its strip leaves the surrounding text and links clear.

The original white, round-headed Shimeji character stands with small black eyes.
A pose from the original Shimeji character set. Separate drawings create movement; the tutorial uses its own bird artwork. Image: Original Shimeji / Group Finity character asset, preserved by Shimeji-ee contributors and LavenderSnek. Source.

Draw the four frames

Draw each pose on a 16 × 16 grid. Enlarge it to 48 × 48 with nearest-neighbor scaling, which keeps pixel edges sharp. Use a beak, tail or ears to show which way it faces. Move the feet between two walking poses while keeping the body in place; add one blink and one resting pose.

Place the four frames, or pose pictures, side by side in a 192 × 48 PNG image. Each frame is 48 pixels wide. Use this order:

Frame Pose Background offset
0 Walk A 0 px
1 Walk B −48 px
2 Blink −96 px
3 Rest −144 px

Keep that size and order when replacing bird-sprites.png. The existing script will then select the intended poses.

Reveal a frame and move the bird

The pet’s box shows a 48-pixel-wide slice of the sprite sheet. In pet.js, paint(frame) chooses the slice by setting its horizontal position to -48 * frame. It uses translateX(x) to move the bird and scaleX(dir) to turn it around.

The strip hides anything drawn beyond its edges. The bird’s right-hand limit is track.clientWidth - 48, which leaves room for its width. If you change the frame size, also change the pet’s box size, the background-position calculation and the width subtracted at the edge.

Read the eight-second cycle

The script uses elapsed % 8 to repeat this sequence:

Elapsed part of each cycle Behavior
0–6 seconds Walk; alternate frames 0 and 1
6–6.5 seconds Hold the blink frame
6.5–8 seconds Hold the resting frame

Walking uses Math.floor(elapsed * 6) % 2 to choose the foot pose. The speed slider starts at 28 pixels per second. Each step multiplies that speed by the time since the last update. This keeps travel speed steady on screens that refresh at different rates, as MDN’s animation-frame reference explains.

For a longer rest, change the cycle length from 8 to 10 seconds. Keep the walking and blink end times at 6 and 6.5. To slow the feet without changing travel speed, reduce the 6 in elapsed * 6.

Preserve the pause behavior

The example starts at rest. Pausing stops the movement code and shows the resting pose. Leaving the tab or turning on your device’s reduced-motion setting also pauses it. When you resize the window, the code moves the bird back inside the strip if needed.

After changing the code, start and pause with the keyboard, narrow the window and leave the tab. The bird should stay inside its strip and wait for you to start it again.

Add another pose

Add a fifth 48-pixel frame to make the sheet 240 pixels wide. Give it frame number 4 and use paint(4) when you want to show the new pose. Keep the original four numbers unchanged so walking, blinking and resting still use the right pictures.

DesktopPet and Shimeji use more complex versions of the same idea: artwork supplies the poses, and rules choose when and where to show them.