CSS Summit, Environments for Humans 2015
Sarah Drasner, Senior UX Engineer at Trulia
@sarah_edo : twitter || @sdras : codepen
This pen.
Start with this technique from Joe Harrison
Splash with Step()
Of all web-based animation techniques, step animation most closely resembles old hand-drawn cel animation.
Let's use this to our advantage.
3 of 21 frames.
Shooting on twos.
This pen.
Large Sprite and animate the background position.
Keep it simple.
@keyframes splashit {
100% { background-position: 0 -3046px; }
}
.splash {
background: url(‘splash-sprite2.svg’);
animation: splashit 1.8s steps(21) infinite;
}
/* fallback */
.no-svg .splash {
background: url(‘splash-sprite2.png’);
}
Illustrator, with a template:
Works with Sketch, too.
In an SVG editor with Grunticon
Rolling Sprite Background
without Step()
The background rolls through...
Let's use this to our advantage.
/*--extend--*/
.area {
width: 600px;
height: 348px;
}
.fore, .mid, .bk, .container { @extend .area; }
Extend to keep it DRY
.fore {
background: url(‘fore.svg’);
animation: bk 7s -5s linear infinite;
}
.mid {
background: url(‘mid.svg’);
animation: bk 15s -5s linear infinite;
}
.bk {
background: url(‘bkwalk2.svg’);
animation: bk 20s -5s linear infinite;
}
@keyframes bk {
100% { background-position: 200% 0; }
}
Z-index for parallax, consistent bk position,
different length of animation in seconds.
Modern Day Book of Kels
Illustration with SVG Sprite
This pen.
Compare to using text with photos to illustrate an article.
[class^="star"] {
animation: blink 2s ease-in-out infinite both;
}
[class^="dot"] {
animation: blink 5s -3s ease-in-out infinite both;
}
@keyframes blink {
50% { opacity: 0; }
}
.initial {
width: 50%;
float: left;
margin: 0 7% 0 0;
}
We're using percentage here, but we could also use flexbox.
viewBox="0 0 490 474" preserveAspectRatio="xMidYMid meet"
Define smaller viewbox, put in preserveAspectRatio (though this is also the default)
[class^="mountain"], [class^="grass"] {
transform: skew(2deg);
}
@media screen and ( max-width: 500px ) {
[class^="mountain"], [class^="grass"] {
transform: skew(1.5deg);
}
}
var shape = document.getElementById("svg");
// media query event handler
if (matchMedia) {
var mq = window.matchMedia("(min-width: 500px)");
mq.addListener(WidthChange);
WidthChange(mq);
}
// media query change
function WidthChange(mq) {
if (mq.matches) {
shape.setAttribute("viewBox", "0 0 490 474");
}
else {
shape.setAttribute("viewBox", "0 490 500 500");
}
};
Acts like a window to show and hide the requisite parts of the sprite
One Scalable SVG with
Hidden Information
here's why...
(I don't work for them and they don't pay me.)
Bad transform origin bug on rotation.
More in this CSS-Tricks article.
Chrome
IE
Firefox
Safari (zoomed)
The issue with longer CSS animations:
This pen.
This pen courtesy of GreenSock.
This pen.
This pen.
Responsive Design for an
Animated Infographic
Pretty close to Technique #3,
Kels Illustration
This pen.
Change the viewbox in JavaScript like we did before:
Media queries for layout, and fallback with Modernizr:
/* media query example element, mobile first */
@media (max-width: 825px) {
.container {
width: 100%;
}
}
@media (min-width: 826px) {
.container {
width: 825px;
}
}
/* fallback */
.inlinesvg .fallback {
display: none;
}
.no-inlinesvg .fallback {
width: 500px;
height: 500px;
display: block;
}
<svg aria-labelledby="title" id="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 765 587">
<title id="title" lang="en">Circle of icons that illustrate Global Warming Solutions</title>
You can also add a title for elements in the SVG DOM
This resource, with support charts.
Also, this article by Dudley Storey.
Fun. Remember fun?
(I don't work for them and they don't pay me)
These Slides:
slides.com/sdrasner/css-summit