Responsive Sprite Animations with ImageMagick and GreenSock

Share this article

CSS Sprites are not new. Since being popularized on A List Apart in 2004, the humble sprite has become a staple technique in many a web developer’s toolkit. But while the speed benefits afforded by sprites are well-understood, their uses in modern web animation are rarely discussed. The principle remains the same: combine multiple images into a single ‘master’ graphic, of which only selected portions are displayed at a time.

In this article, we’ll explore an easy way to create responsive CSS sprite animations that are lightweight, mobile-friendly, and even interactive. You don’t need any special graphics software, just a bit of CSS and JavaScript know-how. Let’s get started.

Building A Sprite

Sprites originate from the world of video games, so we’ll pay tribute to their heritage and animate Ryu from Street Fighter. Needless to say, all artwork belongs to Capcom.

We need to combine each frame of our animation into a single image. There are dozens of tools out there to aid with the creation of sprites, many of which will even generate an accompanying stylesheet for you. Compass’s built-in spriting features are immensely powerful, and SpritePad is a good example of a web-based generator. For our purposes, however, a simple command-line approach works fine.

ImageMagick, the ‘swiss army knife’ of image processing, is a free and open-source image manipulation tool that’s perfect for automating tasks that could become laborious, such as combining images. ImageMagick is also available for just about every operating system. Mac users can install it via Homebrew, Windows adherents can download an executable installer from the official website, and Linux devotees probably don’t need me to explain anything.

Save the identically-sized frames of your animation in a folder as a sequence of PNGs. Break out a command-line terminal, navigate (cd) to the directory where your images are saved, and execute the following command:

convert *.png -append result-sprite.png

This command instructs ImageMagick to vertically concatenate all the .PNG files in our directory (since ‘*’ is essentially a wildcard) into one complete sprite called “result-sprite.png”. This is the graphic that we will apply as a background-image in CSS to the element we want to animate, altering its position to cycle through each frame in sequence.

Pure CSS Animation

We’ll start simply, and animate our sprite with CSS3 keyframe animation. If you’re unfamiliar with the basic syntax, Louis Lazaris’s article on Smashing Magazine is a very good introduction. We’re looking to animate the vertical background-position of our element from 0% to 100% (top to bottom), exposing each frame in turn.

Allowing the animation to run in one continuous sequence, as per default behaviour, would expose our sprite’s in-between frames and ruin the illusion. By using the steps() function, however, we can control the number of rendered keyframes. Because of the way background-position works when defined as a percentage, we’ll need to set the number of steps to be one fewer than the total number of frames in our animation.

Experiment with the following CodePen to get the idea:

See the Pen MYRKmJ by SitePoint (@SitePoint) on CodePen.

Note that we’ve defined the width and height of our element to exactly match that of a single frame, to avoid any bleeding of prior or subsequent frames. We’ve used the shorthand notation to set the animation-iteration-count to “infinite” and the animation-duration to 3.5 seconds, with the result that Ryu will carry out his punch-kick-hadoken combo until the end of time.

This is far from perfect, however. Try changing the height of our Ryu element, or applying a background-size to our sprite, and the effect will break. Our animated element is not yet responsive, and is dependent on rigid, pixel-based dimensions.

Making it Responsive

If we wish to freely resize our animated sprite, we have two options. A key to both options is our sprite’s dependence on a consistent aspect ratio, rather than a particular size. We must retain its proportions for it to scale correctly.

The first option is to set the background-size of our sprite to 100% of the width of our element, convert the width and height of our element from pixels to ems, and then alter the base font-size as required. This would achieve the desired effect, and may be suitable for most purposes, but would not result in true fluidity.

Our second option is to take advantage of Marc Hinse’s pure-CSS fixed aspect ratio technique, which uses percentage-based padding on a pseudo-element to maintain an element’s proportions at any size. This trick is used in the demo below to give our sprite a fluid width of 70%. Responsive Ryu.

See the Pen zxXrzP by SitePoint (@SitePoint) on CodePen.

This is a lightweight and mobile-friendly method of achieving a responsive frame-by-frame animation effect. Browser support for CSS animations is excellent, with the CSS3 keyframe syntax being the only limiting factor. Include the appropriate -webkit- vendor prefix, and the effect works in all modern browsers including IE10 and above.

JavaScript Animation

What if we want more precise control over our animation than is generally possible with the CSS3 keyframe syntax? Let’s say that instead of a single, looping animation, we wish to incorporate multiple sprites into a complex scene and bind triggers and timing functions to the position of the scrollbar?

For this to be possible, we will need to animate our sprites with JavaScript, harnessing the power of the GreenSock animation platform using Jan Paepke’s excellent library ScrollMagic. For those unfamiliar with this powerful combination, the official ScrollMagic demos are a great place to start.

These are powerful tools on which entire series of tutorials have been written. As such, rather than diving into the details of how to work with ScrollMagic, we will instead demonstrate some of the powerful timing functions now available to us. The basic sprite principles remain identical to our earlier pure-CSS animation.

First, let’s trigger a 2-second-long animation at a particular scroll position. It should finish on the final frame, and play in reverse when the user scrolls back up (even if this occurs midway through playback). We’ll stick with the video game theme and use a sprite from Westwood Studios’ strategy classic Red Alert, now released as freeware.

See the Pen Demo 3: JavaScript-Powered Timing Functions by SitePoint (@SitePoint) on CodePen.

Note that we’re using the aforementioned em-based sizing method; try altering the font-size to scale the sprite up and down. We’ve defined a tween, setting the destination value of our element’s vertical background-position to 100%, and used the SteppedEase method to achieve the required frame-by-frame effect.

Let’s take this one step further. We’ll synchronise our animation with the scroll position, using the scrollbar like a playback scrub control.

See the Pen Demo 4: Synchronising Playback with the Scrollbar by SitePoint (@SitePoint) on CodePen.

The sprite remains in a fixed position for the duration of the animation, which is now controlled by the user’s scroll behaviour. To achieve this effect, we’ve given our ScrollMagic.Scene a duration of 1500px and used the setPin method to fix the parent element for the entirety of the scene. For a more detailed breakdown of these techniques, be sure to peruse the excellent documentation on the ScrollMagic website.

Browser support for these JavaScript-powered sprite animations is even better than that of our pure-CSS version. ScrollMagic and GreenSock are supported in all modern browsers including IE 9+, with browser prefixes and inconsistencies handled automatically. Mobile support is also good; these techniques work on iOS 8+ with no special workarounds, including on-the-fly repainting and no loss in scroll momentum.


We’ve barely scratched the surface of what’s made possible by combining responsive CSS sprites with advanced scrolling animation, but it’s my hope to inspire others to start experimenting. I’d love to see any sites that use variations of the techniques described above. You can take a look at the personal project that first prompted me to explore sprite animation — videogame aficionados may notice a pattern.

Finally, in the words of Rachel Nabors, “please animate responsibly.” Just because something can be animated it doesn’t necessarily mean it should be. And if you do choose to animate, do so deliberately, beautifully, and with purpose.

Thanks for reading!

Frequently Asked Questions (FAQs) on Responsive Sprite Animations

What are the benefits of using ImageMagick and GreenSock for responsive sprite animations?

ImageMagick and GreenSock are powerful tools for creating responsive sprite animations. ImageMagick is a software suite that allows you to create, edit, and compose bitmap images in a variety of formats. It can resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves. GreenSock, on the other hand, is a JavaScript library for creating high-performance animations that work in all major browsers. It provides a simple, easy-to-use API for animating anything JavaScript can touch (CSS properties, SVG, React, Vue, WebGL, etc.) and it solves countless browser inconsistencies.

How can I make my sprite animations responsive?

Making sprite animations responsive involves a few steps. First, you need to create a sprite sheet, which is a collection of individual images arranged together in a grid. This can be done using a tool like ImageMagick. Next, you need to use CSS to position and display the sprite sheet on your webpage. Then, you can use JavaScript (or a library like GreenSock) to animate the sprite sheet, changing the position of the sprite sheet over time to create the illusion of movement. Finally, to make the animation responsive, you can use CSS media queries to adjust the size and position of the sprite sheet based on the size of the user’s screen.

Can I use other tools besides ImageMagick and GreenSock for creating responsive sprite animations?

Yes, there are many other tools you can use to create responsive sprite animations. Some popular alternatives include Piskel, Aseprite, and CSS-Tricks. Each of these tools has its own strengths and weaknesses, so the best tool for you will depend on your specific needs and preferences.

How can I optimize my sprite animations for performance?

Optimizing sprite animations for performance can involve a few different strategies. One is to minimize the size of your sprite sheet as much as possible, as larger sprite sheets will take longer to load and can slow down your webpage. Another strategy is to use CSS and JavaScript efficiently, avoiding unnecessary calculations and reflows. Finally, you can use tools like GreenSock’s GSAP to help manage and optimize your animations.

How can I make my sprite animations work on all devices and screen sizes?

Making sprite animations work on all devices and screen sizes involves making them responsive. This can be done using CSS media queries, which allow you to apply different styles based on the size of the user’s screen. You can use media queries to adjust the size and position of your sprite sheet, ensuring that it looks good and works well on all devices.

What are some common challenges when creating responsive sprite animations?

Some common challenges when creating responsive sprite animations include managing the size and complexity of your sprite sheet, ensuring that your animations work well on all devices and screen sizes, and optimizing your animations for performance. These challenges can be overcome with careful planning, efficient use of CSS and JavaScript, and the use of tools like ImageMagick and GreenSock.

Can I use responsive sprite animations in my web design projects?

Yes, responsive sprite animations can be a great addition to your web design projects. They can add visual interest and interactivity, helping to engage users and enhance the user experience. However, it’s important to use animations judiciously, as too many animations can be distracting and can slow down your webpage.

How can I learn more about creating responsive sprite animations?

There are many resources available for learning more about creating responsive sprite animations. Some good places to start include the documentation for ImageMagick and GreenSock, as well as tutorials and articles on websites like SitePoint, CSS-Tricks, and LogRocket.

What are some best practices for creating responsive sprite animations?

Some best practices for creating responsive sprite animations include planning your animations carefully, optimizing your sprite sheet for performance, using CSS and JavaScript efficiently, making your animations responsive with media queries, and testing your animations on a variety of devices and screen sizes.

Can I use responsive sprite animations in my mobile app?

Yes, responsive sprite animations can be used in mobile apps as well as webpages. However, creating animations for mobile apps may involve some additional considerations, such as the size and resolution of the device’s screen, the performance capabilities of the device, and the user’s interaction model (touch vs. mouse).

Tom BennetTom Bennet
View Author

Tom is a writer, web developer, technical SEO, and skydiver based in London. He has a passion for all things front-end, and can be found at

css3 animationgreensock animationLouisLresponsive spritessprite animation
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form