For this task, I wanted to make some animated smoke come out of the frogs' rocket ship before the launch. My style is mostly just simple geometric shapes arranged to look like things, so using circular DIV elements to look like puffs of smoke was fine with me. I found a great starting point by Andrea Verlicchi on CodePen, and then modified it for my comic.
Here's the basic idea - puffs of smoke emanate from a given source element. They move downward and off to the side while fading away.
The puffs of smoke will be represented by rounded SPAN elements, with this CSS applied to them:
span.smokepuff { display: block; position: absolute; bottom: -35px; left: 50%; margin-left: -20px; height: 0px; width: 0px; border: 35px solid #4b4b4b; border-radius: 35px; left: -14px; opacity: 0; transform: scale(0.2); }
The above styling just makes them round, grey, and positioned absolutely in their container. I also have two animation keyframes defined, one for the down-and-left movement and one for the down-and-right movement:
@keyframes smokeL { 0% { transform: scale(0.2) translate(0, 0); } 10% { opacity: 1; transform: scale(0.2) translate(0, 5px); } 100% { opacity: 0; transform: scale(1) translate(-50px, 80px); } } @keyframes smokeR { 0% { transform: scale(0.2) translate(0, 0); } 10% { opacity: 1; transform: scale(0.2) translate(0, 5px); } 100% { opacity: 0; transform: scale(1) translate(50px, 80px); } }
The above keyframe definitions define an animation that will move the smoke puffs lower by 80 pixels and 50 pixels to either side while at the same time scaling them up and fading them out. It defines 3 steps: 0% (the start), 10% (moved a little down), and 100% (moved completely down and over). There's one for the left, smokeL, and one for the right, smokeR.
Note: if you care about being compatible with slightly older browsers, you would want copies of these with @-moz-keyframes and @-webkit-keyframes as the names as well as adding -moz-transform and -webkit-transform to them all! I left that out here to keep the example simpler!
I said this was pure CSS3 animation, but there's still a little JavaScript involved. It doesn't really do the animating, but I use some code to generate the puffs in the first place. Something like this:
function createSmoke(time, num) { var timeGap = (time / num); for( var i = 0; i < num; i++) { var delay = (timeGap * i) + 's'; var aniName = "smokeL"; if (((i+1) % 2) == 0) { aniName = "smokeR"; } var aniStyle = "animation: " + aniName + " " + duration + " " + delay + " infinite"; $('#smoker').append('<span class="smokepuff" style="' + aniStyle + '"></span>'); } }
When this function is called, you give it the length of the animation and the number of puffs of smoke you want. It figures out how much of a delay there should be between each puff's animation starting based on those two values. For example, if you want the animation to run 5 seconds and have 10 puffs of smoke, the first puff would have no delay, the second would have a delay of ( 5 / 10 ) * 1, the third a delay of (5 / 10 ) * 2, and so on. In this example, that just means add a half-second delay for each puff you generate. Also, each time through the loop, it alternates between the smokeL and smokeR animations so that every other puff moves in the opposite direction. One final piece of the total animation style is to set the repeat-count to infinite, so the puffs keep on coming! The function generates new SPAN tags with these animation styles applied and appends them to the parent element, which here is named smoker. It's just a DIV somewhere on the page - all the puffs of smoke will appear to come out of it.
The finished animation. Don't miss it! |
I think the effect turned out great. Even though I used a little JavaScript to create the elements, doing all the animation with JavaScript would have been much, much more complicated. I may consider replacing all the old JavaScript-powered animation on Amphibian.com with CSS animation, and fix up my animation editor to go with it!
Now don't miss the smoke effect in today's comic! If you're reading this on the publication day (16 May 2016) the countdown to launch will be live! That means the last frame of the comic will change and do different things right up to the launch time! Keep watching it!
Amphibian.com comic for 16 May 2016 |