SlideShare a Scribd company logo
Styling & Animating
Scalable Vector Graphics
with CSS
CSSConf - May 27th 2014, Florida
@SaraSoueidan / sarasoueidan.com
Hello,
I’m Sara.
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSConfUS2014]
Scalable Vector Graphics (SVG) is an
XML-based vector image format for
two-dimensional graphics with support
for interactivity and animation.
Why SVGs?
➔ SVGs are accessible
➔ Scalable & Resolution Independent
➔ Very Good Browser Support
➔ Smaller sizes (can be gzipped)
➔ Built-in Graphics Effects (Blend Modes, Filters, etc.)
➔ Interactive and Styleable (CSS and Javascript)
➔ SVGs Have Tools
Creating SVGs
Adobe Illustrator Inkscape (Free) Sketch (Mac OS X only)
Optimize Exported Code (Standalone Tools)
http://goo.gl/0XvzHs
SVG Editor by Peter Collingridge
(Online)
Optimize Exported Code (Standalone Tools)
SVG O (NodeJS-Based + GUI)
Peter’s SVG
Editor
Clean Up & Give Classes
bird.svg
Replace generic class names with semantic ones that describe your illustration.
Styling SVGs with CSS
SVG Presentation Attributes
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
width="300px" height="300px" viewBox="0 0 300 300">
<polygon
fill = "#FF931E"
stroke = "#ED1C24"
stroke-width = "5"
points = "279.1,160.8 195.2,193.3 174.4,280.8
117.6,211.1 27.9,218.3 76.7,142.7 42.1,59.6 129.1,82.7
197.4,24.1 202.3,114 "/>
</svg>
star.svg
Shared with CSS SVG-only
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-variant
font-weight
direction
letter-spacing
text-decoration
unincode-bidi
word-spacing
visibility
text-rendering
writing-mode
clip-path
mask opacity
filter
pointer-events
image-rendering
clip
color
cursor
display
overflow
clip-rule
flood-color
flood-opacity
stop-opacity
kerning
text-anchor
color-profile color-
rendering
fill
fill-opacity
fill-rule
marker
marker-end
marker-mid marker-
start
stroke
stroke-width
stroke-opacity
stroke-dasharray
stroke-dashoffset
stroke-linecap
stroke-linejoin
stroke-miterlimit
alignment-baseline
baseline-shift
shape-rendering
glyph-orientation-horizontal
glyph-orientation-vertical
color-interpolation
color-interpolation-filters
dominant-baseline
lighting-color
stop-color
enable-background
http://goo.gl/LVkev
Inline Styles (style=” ”)
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
style=”width: 300px; height: 300px;” viewBox="0 0 300 300">
<polygon
style = ”fill: #FF931E; stroke: #ED1C24; stroke-width:
5;”
points = "279.1,160.8 195.2,193.3 174.4,280.8
117.6,211.1 27.9,218.3 76.7,142.7 42.1,59.6 129.1,82.7
197.4,24.1 202.3,114 "/>
</svg>
star.svg
Embedded Styles (<style>) Inside svg
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
width="300px" height="300px" viewBox="0 0 300 300">
<style type=”text/css”>
<![CDATA[
selector {/* styles */}
]]>
</style>
<g id=”..”> … </g>
</svg>
filename.svg
Embedded Styles (<style>) Outside svg
<!DOCTYPE html><!-- HTML5 document -->
<html> <head> ... </head> <body>
<style type=”text/css”>
/* style rules */
</style>
<!-- xmlns is optional in an HTML5 document -->
<svg version="1.1" viewBox="0 0 300 300">
<!-- SVG content -->
</svg>
</body> </html>
filename.html
External Style Sheets
<?xml version="1.0" standalone="no"?><?xml-stylesheet
type="text/css" href="style.css"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
width=".." height=".." viewBox="..">
<!-- SVG content -->
</svg> filename.svg
Style Rules (Selectors)
Type Selectors:
g {/* style a group */}
circle, rect { fill: #009966; }
Class and ID Selectors:
.arrow-head { /* styles */ }
#bird { /* styles */ }
Dynamic Pseudo-Class Selectors:
.icon:hover, .icon:active, .icon:focus
{ /* styles */ }
Pseudo-Class Selectors:
:first-child, :last-child, :nth-*-
child(), :visited, :link and :lang,
:last-of-type, :first-of-type, :not(),
:nth-*-type(), :only-child, :only-of-
type, ...
Children Selectors:
g > circle {/* styles */}
Notes: Pseudo-elements/Generated Content don’t
work.
Style Cascades
http://goo.gl/QHFp6w
Presentation attributes count as low level “author stylesheets”
and are overridden by any other style definition (external
stylesheets, document stylesheets and inline styles).
<circle cx="100" cy="100" r="75"
fill="blue" style="fill:deepPink;" />
Example: Simple Hover Effect (Iconic)
Changing the stroke color and
stroke width. Fading background
in on hover.
http://goo.gl/Fofspo
Embedding SVGs
http://goo.gl/oiYssv
1
<img src=”image.svg” alt=”Site Logo” />
2
<object type="image/svg+xml" data="image.svg">
<img src=”fallback.jpg”><!-- fallback -->
</object>
3 <embed type="image/svg+xml" src="image.svg" />
4
<iframe src="image.svg">
<!-- fallback here -->
</iframe>
5 <svg> … </svg> <!-- XHTML/HTML5 Document -- >
6 #el { background-image: url(image.svg); }
SVG Embed Technique
Links, External Styles,
Interactivity, CSS Animations
<img src=”img.svg” alt=”” />
Nope*
<object type="image/svg+xml"
data="image.svg"></object> Yep
<embed type="image/svg+xml" src="image.svg" /> Yep
<iframe src="image.svg"></iframe>
Yep
Inline <svg> … </svg> Yep
background-image: url(image.svg); Nope*
* CSS animations won’t be played, but SVG (SMIL) animations will be preserved.
.
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="350px"
height="400px” viewBox="0 0 350 400" >
<style type="text/css">
<![CDATA[
.fish {
transform-origin: 50%;
animation: shake .4s linear infinite;
}
@keyframes shake {
to {-webkit-transform: rotate(-10deg);}
}
]]>
</style>
<g id="bear" …> <!-- ... --></g>
</svg>
bear-css.svg
Example: CSS Animation
Example
(Embedded)
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="350px"
height="400px” viewBox="0 0 350 400" >
<g id="bear" …>
<!-- … -->
<g class=”fish”>
<!-- ... -->
<animateTransform attributeName="transform"
type="rotate" begin="0s" dur=".4s"
from="0 380 530" to="-10 380 530"
repeatCount="indefinite" fill="freeze"
/>
</g>
</g>
</svg>
bear-smil.svg
Example: SVG (SMIL) Animation
Example (Embedded)
Responsifying SVGs
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
width=”300px height=”300px” >
<style type="text/css">
.eye{fill:#8B6A62;}
.face{fill:#F6E6E5;}
.head{fill:#F7A591;}
/* ... */
</style>
<path class="body" d=”...”>
<!-- ... -->
</svg>
owl.svg
Example
1. Remove Width & Height, Add viewBox
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0
0 300 300">
<style type="text/css">
/* ... */
</style>
<path class="body" d=”...”>
<!-- ... -->
</svg>
owl.svg
2. Preserve Aspect Ratio
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0
0 300 300" preserveAspectRatio="xMinYMin meet">
<style type="text/css">
/* ... */
</style>
<path class="body" d=”...”>
<!-- ... -->
</svg>
owl.svg
Note: Without viewBox, preserveAspectRatio has no effect.
http://goo.gl/b50HY8
3. Embed & Wrap in a container
<!DOCTYPE html><!-- HTML5 document -->
<html> <head> ... </head>
<body>
<div class=”my-svg-container”>
<object type="image/svg+xml" data="owl.svg" class=”my-
svg”>
<!-- fallback here -->
</object>
</div>
</body>
</html>
responsive-owl.html
SVG can be embedded as <object>, <img> or inline as an <svg>. The technique will work for all three embeds.
4. Padding Hack on Container
.my-svg-container {
height: 0;
position: relative; /* create positioning context for svg
*/
width: width-value;
padding-top: (SVG height / SVG width) * width-value;
}
responsive-owl.css
For owl.svg: padding-top = 300 / 300 * 100 = 1 * 100 = 100%;
If width of container = 60%: padding-top = 300 / 300 * 60 = 1 * 60 = 60%;
If owl.svg has width 250px, height 400px: padding-top = 400/250 * 100 = 1.6 * 100 = 160%;
4. Position SVG Inside Container
/* pull the svg to the top of the container */
.my-svg {
position: absolute;
top: 0;
left: 0;
width: 100%; /* only required for <img /> */
}
responsive-owl.css
6. Resize!
Animating SVGs with CSS
Transforming SVGs
Initial transform-origin on HTML & SVG
Box Model?
transform-origin
(default value)
HTML elements (div, etc.) Yes 50% 50%*
SVG elements (circle, rect, etc.) No 0 0**
*50% 50% = center of the HTML element itself
** 0 0 is the top left corner of the SVG canvas, not the element itself
CSS Transforms on HTML & SVG
<!DOCTYPE html>
...
<div style=”width: 100px; height: 100px; background-color: orange”>
</div>
<svg style=”width: 150px; height: 150px; background-color: #eee”>
<rect width="100" height="100" x="25" y="25" fill="orange" />
</svg>
Setting transform-origin on SVG Elements
1. Using Percentage Values: The value is set relative to the
element’s bounding box, which includes the stroke used to
draw its border.
2. Using absolute length values: The origin is set relative to the
SVG canvas.
CSS Transforms on HTML vs SVG (cont’d)
<!DOCTYPE html>
<style>
div, rect {
transform-origin: 50% 50%;
}
</style>
Heads up: Transform-Origin Issue In
Firefox With Percentage Values
Setting transform-origin using a percentage value currently
doesn’t work in Firefox. That is a bug. (It shouldn’t be a problem
if you’re not rotating anything.)
Setting it in absolute length values should be fairly simple using a graphics editor.
Example: PinWheel
PinWheel by http://pixeldoree.com/vector/free-vector-pinwheels-adobe-illustrator-and-png-files/
.wheel {
-webkit-transform-origin: 50% 50%;
transform-origin: 193px 164px;
animation:
spin 4s
cubic-bezier(.49,.05,.32,1.04)
infinite alternate;
}
@keyframes spin {
50% {
transform: rotate(360deg);
}
}
Zooming out (or in) in Webkit/Blink
does not maintain the transform
origin at the center of the rotating
element. This is a bug.
This issue does not happen in
Firefox, where the transform origin
is set in absolute values (relative to
the SVG canvas).
Heads up: Transform-Origin Issue In
Chrome With Percentage Values
→ Just use absolute values (for
the time being)!
Heads up: Hardware Acceleration In
Chrome
CSS 3D transforms are hardware accelerated in
Chrome when applied to HTML elements. However,
they are not accelerated when used on SVG elements -
they have the same performance profile as SVG
transform attributes. They are accelerated in Firefox.
Animating SVG Paths
Animated Line Drawing
To Animate an SVG path you need to know its exact length. Then:
#path {
stroke-dasharray: pathLength;
stroke-dashoffset: pathLength;
/* transition stroke-dashoffset */
transition: stroke-dashoffset 2s linear;
}
svg:hover #path{
stroke-dashoffset: 0;
}
Example
#cable {
stroke: #FFF2B1;
stroke-dasharray: 4000 4000;
stroke-dashoffset: 4000;
stroke-width: 4;
transition: stroke-dashoffset 8s
linear;
}
svg:hover #cable {
stroke-dashoffset: 0;
}
/* turn lamp on */
.inner-lamp{
fill:grey;
transition: fill .5s ease-in 6s;
}
svg:hover .inner-lamp {
fill: #FBFFF8;
}
/* ... */
The animated path is positioned on top of the black
path. When SVG is hovered path is animated, and the
lamp is “turned on” after path finishes animation, using a
delay that is equal to the animation (transition) time of
the path.
Example 2
http://goo.gl/zII9p0
var path = document.querySelector('.drawing-
path');path.getTotalLength();
//set CSS properties up
path.style.strokeDasharray = length + ' ' + length;
path.style.strokeDashoffset = length;
//set transition up
path.style.transition = 'stroke-dashoffset 2s ease-in-
out';// animatepath.style.strokeDashoffset = '0';
//more: http://jakearchibald.com/2013/animated-line-drawing-svg/
Animated Line Drawing: Retrieving Path
Length Using Javascript
#RecommendedReading
http://jakearchibald.com/2013/animated-line-drawing-svg/
Animated Paths (Morphing Paths)
http://goo.gl/ri9nwU
Animated Paths (Morphing Paths)
The idea is to create a SVG with one path and to morph that
path into another one.
You’ll need: the first path, the second path, and possibly
intermediate paths (depending on your animation).
There is no way in CSS to animate
one SVG path into another.
http://goo.gl/93gFYh
http://snapsvg.io/
Use Snap.svg
“ By using animated SVGs instead of
GIFs we were able to reduce our page
size from 1.6 mb to 389 kb, and reduce
our page load time from 8.75 s to 412
ms. That’s a huge difference.”
Animated SVGs As GIFs Replacement
http://oak.is/thinking/animated-svgs/
Don’t forget to..
Make SVGs Accessible
http://goo.gl/sG7G0j
#MustRead
Optimize & Degrade Gracefully
http://goo.gl/nhXtbu
#MustRead
http://caniuse.com/#
search=svg
Thank You!
@SaraSoueidan / sarasoueidan.com
Ad

More Related Content

What's hot (20)

Quality Development with CSS3
Quality Development with CSS3Quality Development with CSS3
Quality Development with CSS3
Shay Howe
 
Accessibility Hacks version 2
Accessibility Hacks version 2Accessibility Hacks version 2
Accessibility Hacks version 2
Graham Armfield
 
Html5
Html5Html5
Html5
Mehdi Jalal
 
Before Going Vector
Before Going VectorBefore Going Vector
Before Going Vector
david deraedt
 
Accessibility Hacks Wordcamp Manchester October 2018
Accessibility Hacks Wordcamp Manchester October 2018Accessibility Hacks Wordcamp Manchester October 2018
Accessibility Hacks Wordcamp Manchester October 2018
Graham Armfield
 
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Erin M. Kidwell
 
Intro to CSS3
Intro to CSS3Intro to CSS3
Intro to CSS3
Denise Jacobs
 
CSS3: Simply Responsive
CSS3: Simply ResponsiveCSS3: Simply Responsive
CSS3: Simply Responsive
Denise Jacobs
 
Introduction to CSS3
Introduction to CSS3Introduction to CSS3
Introduction to CSS3
Doris Chen
 
Css3
Css3Css3
Css3
Rahma Boufalgha
 
Html5
Html5Html5
Html5
Mehdi Jalal
 
CSS3: Are you experienced?
CSS3: Are you experienced?CSS3: Are you experienced?
CSS3: Are you experienced?
Denise Jacobs
 
HTML5 - A Whirlwind tour
HTML5 - A Whirlwind tourHTML5 - A Whirlwind tour
HTML5 - A Whirlwind tour
Lohith Goudagere Nagaraj
 
Css3
Css3Css3
Css3
Deepak Mangal
 
Html5 more than just html5 v final
Html5  more than just html5 v finalHtml5  more than just html5 v final
Html5 more than just html5 v final
Lohith Goudagere Nagaraj
 
New Elements & Features in CSS3
New Elements & Features in CSS3New Elements & Features in CSS3
New Elements & Features in CSS3
Jamshid Hashimi
 
Css3
Css3Css3
Css3
Bronson Quick
 
Sass - Getting Started with Sass!
Sass - Getting Started with Sass!Sass - Getting Started with Sass!
Sass - Getting Started with Sass!
Eric Sembrat
 
Илья Пухальский (EPAM Systems)
Илья Пухальский (EPAM Systems)Илья Пухальский (EPAM Systems)
Илья Пухальский (EPAM Systems)
Ontico
 
Improving the Responsive Web Design Process in 2016
Improving the Responsive Web Design Process in 2016Improving the Responsive Web Design Process in 2016
Improving the Responsive Web Design Process in 2016
Cristina Chumillas
 
Quality Development with CSS3
Quality Development with CSS3Quality Development with CSS3
Quality Development with CSS3
Shay Howe
 
Accessibility Hacks version 2
Accessibility Hacks version 2Accessibility Hacks version 2
Accessibility Hacks version 2
Graham Armfield
 
Accessibility Hacks Wordcamp Manchester October 2018
Accessibility Hacks Wordcamp Manchester October 2018Accessibility Hacks Wordcamp Manchester October 2018
Accessibility Hacks Wordcamp Manchester October 2018
Graham Armfield
 
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Erin M. Kidwell
 
CSS3: Simply Responsive
CSS3: Simply ResponsiveCSS3: Simply Responsive
CSS3: Simply Responsive
Denise Jacobs
 
Introduction to CSS3
Introduction to CSS3Introduction to CSS3
Introduction to CSS3
Doris Chen
 
CSS3: Are you experienced?
CSS3: Are you experienced?CSS3: Are you experienced?
CSS3: Are you experienced?
Denise Jacobs
 
New Elements & Features in CSS3
New Elements & Features in CSS3New Elements & Features in CSS3
New Elements & Features in CSS3
Jamshid Hashimi
 
Sass - Getting Started with Sass!
Sass - Getting Started with Sass!Sass - Getting Started with Sass!
Sass - Getting Started with Sass!
Eric Sembrat
 
Илья Пухальский (EPAM Systems)
Илья Пухальский (EPAM Systems)Илья Пухальский (EPAM Systems)
Илья Пухальский (EPAM Systems)
Ontico
 
Improving the Responsive Web Design Process in 2016
Improving the Responsive Web Design Process in 2016Improving the Responsive Web Design Process in 2016
Improving the Responsive Web Design Process in 2016
Cristina Chumillas
 

Viewers also liked (20)

Matricula tcp 2011
Matricula tcp 2011Matricula tcp 2011
Matricula tcp 2011
CIMFORMACION1
 
XXXIX Congreso Odontológico Ecuatoriano 2010
XXXIX Congreso Odontológico Ecuatoriano 2010XXXIX Congreso Odontológico Ecuatoriano 2010
XXXIX Congreso Odontológico Ecuatoriano 2010
Federación Odontológica Ecuatoriana
 
23 el costerito noviembre 2013, periódico escolar
23 el costerito noviembre 2013, periódico escolar23 el costerito noviembre 2013, periódico escolar
23 el costerito noviembre 2013, periódico escolar
Ricardo7000
 
Concurso cuentos Ampa
Concurso cuentos AmpaConcurso cuentos Ampa
Concurso cuentos Ampa
Carmelablan13
 
Présentation Mondes Virtuels et SeriousGame
Présentation Mondes Virtuels et SeriousGamePrésentation Mondes Virtuels et SeriousGame
Présentation Mondes Virtuels et SeriousGame
Xavier COIFFARD
 
Avaliações Eficazes
Avaliações EficazesAvaliações Eficazes
Avaliações Eficazes
Barreiro Toastmasters Club
 
Thousand Foot Krutch
Thousand Foot KrutchThousand Foot Krutch
Thousand Foot Krutch
GenoEndara
 
Horoscopo2
Horoscopo2Horoscopo2
Horoscopo2
Ana Pacheco
 
Motivaciones y alternativas para el uso de la programación de ordenadores en ...
Motivaciones y alternativas para el uso de la programación de ordenadores en ...Motivaciones y alternativas para el uso de la programación de ordenadores en ...
Motivaciones y alternativas para el uso de la programación de ordenadores en ...
Piru Rex
 
Address resolution protocol c
Address resolution protocol  cAddress resolution protocol  c
Address resolution protocol c
Tensor
 
profile
profile profile
profile
Integrated Industrial Services Private Limited
 
Curso de formación de oficiales de control animal (FOCA). Nueva perspectiva e...
Curso de formación de oficiales de control animal (FOCA). Nueva perspectiva e...Curso de formación de oficiales de control animal (FOCA). Nueva perspectiva e...
Curso de formación de oficiales de control animal (FOCA). Nueva perspectiva e...
elreporteroanimal
 
Ccofesud
CcofesudCcofesud
Ccofesud
juan del pino tineo
 
bolsa mexicana de valores
bolsa mexicana de valoresbolsa mexicana de valores
bolsa mexicana de valores
Biiby Pola Ochoa
 
www.yoimginsengcoffee.com Carta presentacion
www.yoimginsengcoffee.com Carta presentacionwww.yoimginsengcoffee.com Carta presentacion
www.yoimginsengcoffee.com Carta presentacion
ITALY COFFEE TEA STORE
 
Effective email communications
Effective email communicationsEffective email communications
Effective email communications
wearnerr
 
Como hacer una tesis sabino
Como hacer una tesis sabinoComo hacer una tesis sabino
Como hacer una tesis sabino
alex17_86
 
Nefos mobile, die offline salesforce app, bei kb a
Nefos mobile, die offline salesforce app, bei kb aNefos mobile, die offline salesforce app, bei kb a
Nefos mobile, die offline salesforce app, bei kb a
Kathrin Schmidt
 
Dsign Machine Indoor Media Advertising
Dsign Machine Indoor Media AdvertisingDsign Machine Indoor Media Advertising
Dsign Machine Indoor Media Advertising
ayazali73
 
23 el costerito noviembre 2013, periódico escolar
23 el costerito noviembre 2013, periódico escolar23 el costerito noviembre 2013, periódico escolar
23 el costerito noviembre 2013, periódico escolar
Ricardo7000
 
Concurso cuentos Ampa
Concurso cuentos AmpaConcurso cuentos Ampa
Concurso cuentos Ampa
Carmelablan13
 
Présentation Mondes Virtuels et SeriousGame
Présentation Mondes Virtuels et SeriousGamePrésentation Mondes Virtuels et SeriousGame
Présentation Mondes Virtuels et SeriousGame
Xavier COIFFARD
 
Thousand Foot Krutch
Thousand Foot KrutchThousand Foot Krutch
Thousand Foot Krutch
GenoEndara
 
Motivaciones y alternativas para el uso de la programación de ordenadores en ...
Motivaciones y alternativas para el uso de la programación de ordenadores en ...Motivaciones y alternativas para el uso de la programación de ordenadores en ...
Motivaciones y alternativas para el uso de la programación de ordenadores en ...
Piru Rex
 
Address resolution protocol c
Address resolution protocol  cAddress resolution protocol  c
Address resolution protocol c
Tensor
 
Curso de formación de oficiales de control animal (FOCA). Nueva perspectiva e...
Curso de formación de oficiales de control animal (FOCA). Nueva perspectiva e...Curso de formación de oficiales de control animal (FOCA). Nueva perspectiva e...
Curso de formación de oficiales de control animal (FOCA). Nueva perspectiva e...
elreporteroanimal
 
www.yoimginsengcoffee.com Carta presentacion
www.yoimginsengcoffee.com Carta presentacionwww.yoimginsengcoffee.com Carta presentacion
www.yoimginsengcoffee.com Carta presentacion
ITALY COFFEE TEA STORE
 
Effective email communications
Effective email communicationsEffective email communications
Effective email communications
wearnerr
 
Como hacer una tesis sabino
Como hacer una tesis sabinoComo hacer una tesis sabino
Como hacer una tesis sabino
alex17_86
 
Nefos mobile, die offline salesforce app, bei kb a
Nefos mobile, die offline salesforce app, bei kb aNefos mobile, die offline salesforce app, bei kb a
Nefos mobile, die offline salesforce app, bei kb a
Kathrin Schmidt
 
Dsign Machine Indoor Media Advertising
Dsign Machine Indoor Media AdvertisingDsign Machine Indoor Media Advertising
Dsign Machine Indoor Media Advertising
ayazali73
 
Ad

Similar to Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSConfUS2014] (20)

SVG and the web
SVG and the webSVG and the web
SVG and the web
Ken Shoufer Web Design
 
SVG - Scalable Vector Graphics
SVG - Scalable Vector GraphicsSVG - Scalable Vector Graphics
SVG - Scalable Vector Graphics
Shweta Sadawarte
 
D3.js and SVG
D3.js and SVGD3.js and SVG
D3.js and SVG
Karol Depka Pradzinski
 
Html5
Html5Html5
Html5
Mario Delgado
 
SVG introduction
SVG   introductionSVG   introduction
SVG introduction
SathyaseelanK1
 
SVG, CSS3, and D3 for Beginners
SVG, CSS3, and D3 for BeginnersSVG, CSS3, and D3 for Beginners
SVG, CSS3, and D3 for Beginners
Oswald Campesato
 
SVG Icons and Screen Reader Accessibility
SVG Icons and Screen Reader AccessibilitySVG Icons and Screen Reader Accessibility
SVG Icons and Screen Reader Accessibility
Dennis Lembree
 
Svcc 2013-d3
Svcc 2013-d3Svcc 2013-d3
Svcc 2013-d3
Oswald Campesato
 
SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)
Oswald Campesato
 
Accessibility Hacks Version 2
Accessibility Hacks Version 2Accessibility Hacks Version 2
Accessibility Hacks Version 2
Graham Armfield
 
Professional reports with SVG
Professional reports with SVGProfessional reports with SVG
Professional reports with SVG
SpeedPartner GmbH
 
SVGD3Angular2React
SVGD3Angular2ReactSVGD3Angular2React
SVGD3Angular2React
Oswald Campesato
 
Thats Not Flash?
Thats Not Flash?Thats Not Flash?
Thats Not Flash?
Mike Wilcox
 
SVG overview
SVG overviewSVG overview
SVG overview
Satoshi Watanabe
 
IconFonts
IconFontsIconFonts
IconFonts
Sara Cannon
 
Css3
Css3Css3
Css3
Anjan Banda
 
Svg, canvas e animations in angular (3 maggio 2019)
Svg, canvas e animations in angular (3 maggio 2019)Svg, canvas e animations in angular (3 maggio 2019)
Svg, canvas e animations in angular (3 maggio 2019)
Leonardo Buscemi
 
CSS and CSS3
CSS and CSS3CSS and CSS3
CSS and CSS3
Robyn Overstreet
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
ghnash
 
Pfnp slides
Pfnp slidesPfnp slides
Pfnp slides
William Myers
 
Ad

Recently uploaded (20)

Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 

Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSConfUS2014]

  • 1. Styling & Animating Scalable Vector Graphics with CSS CSSConf - May 27th 2014, Florida @SaraSoueidan / sarasoueidan.com
  • 4. Scalable Vector Graphics (SVG) is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation.
  • 5. Why SVGs? ➔ SVGs are accessible ➔ Scalable & Resolution Independent ➔ Very Good Browser Support ➔ Smaller sizes (can be gzipped) ➔ Built-in Graphics Effects (Blend Modes, Filters, etc.) ➔ Interactive and Styleable (CSS and Javascript) ➔ SVGs Have Tools
  • 6. Creating SVGs Adobe Illustrator Inkscape (Free) Sketch (Mac OS X only)
  • 7. Optimize Exported Code (Standalone Tools) http://goo.gl/0XvzHs SVG Editor by Peter Collingridge (Online)
  • 8. Optimize Exported Code (Standalone Tools) SVG O (NodeJS-Based + GUI)
  • 10. Clean Up & Give Classes bird.svg Replace generic class names with semantic ones that describe your illustration.
  • 12. SVG Presentation Attributes <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="300px" height="300px" viewBox="0 0 300 300"> <polygon fill = "#FF931E" stroke = "#ED1C24" stroke-width = "5" points = "279.1,160.8 195.2,193.3 174.4,280.8 117.6,211.1 27.9,218.3 76.7,142.7 42.1,59.6 129.1,82.7 197.4,24.1 202.3,114 "/> </svg> star.svg
  • 13. Shared with CSS SVG-only font font-family font-size font-size-adjust font-stretch font-style font-variant font-weight direction letter-spacing text-decoration unincode-bidi word-spacing visibility text-rendering writing-mode clip-path mask opacity filter pointer-events image-rendering clip color cursor display overflow clip-rule flood-color flood-opacity stop-opacity kerning text-anchor color-profile color- rendering fill fill-opacity fill-rule marker marker-end marker-mid marker- start stroke stroke-width stroke-opacity stroke-dasharray stroke-dashoffset stroke-linecap stroke-linejoin stroke-miterlimit alignment-baseline baseline-shift shape-rendering glyph-orientation-horizontal glyph-orientation-vertical color-interpolation color-interpolation-filters dominant-baseline lighting-color stop-color enable-background http://goo.gl/LVkev
  • 14. Inline Styles (style=” ”) <svg xmlns="http://www.w3.org/2000/svg" version="1.1" style=”width: 300px; height: 300px;” viewBox="0 0 300 300"> <polygon style = ”fill: #FF931E; stroke: #ED1C24; stroke-width: 5;” points = "279.1,160.8 195.2,193.3 174.4,280.8 117.6,211.1 27.9,218.3 76.7,142.7 42.1,59.6 129.1,82.7 197.4,24.1 202.3,114 "/> </svg> star.svg
  • 15. Embedded Styles (<style>) Inside svg <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="300px" height="300px" viewBox="0 0 300 300"> <style type=”text/css”> <![CDATA[ selector {/* styles */} ]]> </style> <g id=”..”> … </g> </svg> filename.svg
  • 16. Embedded Styles (<style>) Outside svg <!DOCTYPE html><!-- HTML5 document --> <html> <head> ... </head> <body> <style type=”text/css”> /* style rules */ </style> <!-- xmlns is optional in an HTML5 document --> <svg version="1.1" viewBox="0 0 300 300"> <!-- SVG content --> </svg> </body> </html> filename.html
  • 17. External Style Sheets <?xml version="1.0" standalone="no"?><?xml-stylesheet type="text/css" href="style.css"?> <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width=".." height=".." viewBox=".."> <!-- SVG content --> </svg> filename.svg
  • 18. Style Rules (Selectors) Type Selectors: g {/* style a group */} circle, rect { fill: #009966; } Class and ID Selectors: .arrow-head { /* styles */ } #bird { /* styles */ } Dynamic Pseudo-Class Selectors: .icon:hover, .icon:active, .icon:focus { /* styles */ } Pseudo-Class Selectors: :first-child, :last-child, :nth-*- child(), :visited, :link and :lang, :last-of-type, :first-of-type, :not(), :nth-*-type(), :only-child, :only-of- type, ... Children Selectors: g > circle {/* styles */} Notes: Pseudo-elements/Generated Content don’t work.
  • 19. Style Cascades http://goo.gl/QHFp6w Presentation attributes count as low level “author stylesheets” and are overridden by any other style definition (external stylesheets, document stylesheets and inline styles). <circle cx="100" cy="100" r="75" fill="blue" style="fill:deepPink;" />
  • 20. Example: Simple Hover Effect (Iconic) Changing the stroke color and stroke width. Fading background in on hover. http://goo.gl/Fofspo
  • 22. http://goo.gl/oiYssv 1 <img src=”image.svg” alt=”Site Logo” /> 2 <object type="image/svg+xml" data="image.svg"> <img src=”fallback.jpg”><!-- fallback --> </object> 3 <embed type="image/svg+xml" src="image.svg" /> 4 <iframe src="image.svg"> <!-- fallback here --> </iframe> 5 <svg> … </svg> <!-- XHTML/HTML5 Document -- > 6 #el { background-image: url(image.svg); }
  • 23. SVG Embed Technique Links, External Styles, Interactivity, CSS Animations <img src=”img.svg” alt=”” /> Nope* <object type="image/svg+xml" data="image.svg"></object> Yep <embed type="image/svg+xml" src="image.svg" /> Yep <iframe src="image.svg"></iframe> Yep Inline <svg> … </svg> Yep background-image: url(image.svg); Nope* * CSS animations won’t be played, but SVG (SMIL) animations will be preserved. .
  • 24. <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="350px" height="400px” viewBox="0 0 350 400" > <style type="text/css"> <![CDATA[ .fish { transform-origin: 50%; animation: shake .4s linear infinite; } @keyframes shake { to {-webkit-transform: rotate(-10deg);} } ]]> </style> <g id="bear" …> <!-- ... --></g> </svg> bear-css.svg Example: CSS Animation
  • 26. <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="350px" height="400px” viewBox="0 0 350 400" > <g id="bear" …> <!-- … --> <g class=”fish”> <!-- ... --> <animateTransform attributeName="transform" type="rotate" begin="0s" dur=".4s" from="0 380 530" to="-10 380 530" repeatCount="indefinite" fill="freeze" /> </g> </g> </svg> bear-smil.svg Example: SVG (SMIL) Animation
  • 29. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width=”300px height=”300px” > <style type="text/css"> .eye{fill:#8B6A62;} .face{fill:#F6E6E5;} .head{fill:#F7A591;} /* ... */ </style> <path class="body" d=”...”> <!-- ... --> </svg> owl.svg Example
  • 30. 1. Remove Width & Height, Add viewBox <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 300 300"> <style type="text/css"> /* ... */ </style> <path class="body" d=”...”> <!-- ... --> </svg> owl.svg
  • 31. 2. Preserve Aspect Ratio <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 300 300" preserveAspectRatio="xMinYMin meet"> <style type="text/css"> /* ... */ </style> <path class="body" d=”...”> <!-- ... --> </svg> owl.svg Note: Without viewBox, preserveAspectRatio has no effect. http://goo.gl/b50HY8
  • 32. 3. Embed & Wrap in a container <!DOCTYPE html><!-- HTML5 document --> <html> <head> ... </head> <body> <div class=”my-svg-container”> <object type="image/svg+xml" data="owl.svg" class=”my- svg”> <!-- fallback here --> </object> </div> </body> </html> responsive-owl.html SVG can be embedded as <object>, <img> or inline as an <svg>. The technique will work for all three embeds.
  • 33. 4. Padding Hack on Container .my-svg-container { height: 0; position: relative; /* create positioning context for svg */ width: width-value; padding-top: (SVG height / SVG width) * width-value; } responsive-owl.css For owl.svg: padding-top = 300 / 300 * 100 = 1 * 100 = 100%; If width of container = 60%: padding-top = 300 / 300 * 60 = 1 * 60 = 60%; If owl.svg has width 250px, height 400px: padding-top = 400/250 * 100 = 1.6 * 100 = 160%;
  • 34. 4. Position SVG Inside Container /* pull the svg to the top of the container */ .my-svg { position: absolute; top: 0; left: 0; width: 100%; /* only required for <img /> */ } responsive-owl.css
  • 38. Initial transform-origin on HTML & SVG Box Model? transform-origin (default value) HTML elements (div, etc.) Yes 50% 50%* SVG elements (circle, rect, etc.) No 0 0** *50% 50% = center of the HTML element itself ** 0 0 is the top left corner of the SVG canvas, not the element itself
  • 39. CSS Transforms on HTML & SVG <!DOCTYPE html> ... <div style=”width: 100px; height: 100px; background-color: orange”> </div> <svg style=”width: 150px; height: 150px; background-color: #eee”> <rect width="100" height="100" x="25" y="25" fill="orange" /> </svg>
  • 40. Setting transform-origin on SVG Elements 1. Using Percentage Values: The value is set relative to the element’s bounding box, which includes the stroke used to draw its border. 2. Using absolute length values: The origin is set relative to the SVG canvas.
  • 41. CSS Transforms on HTML vs SVG (cont’d) <!DOCTYPE html> <style> div, rect { transform-origin: 50% 50%; } </style>
  • 42. Heads up: Transform-Origin Issue In Firefox With Percentage Values Setting transform-origin using a percentage value currently doesn’t work in Firefox. That is a bug. (It shouldn’t be a problem if you’re not rotating anything.) Setting it in absolute length values should be fairly simple using a graphics editor.
  • 43. Example: PinWheel PinWheel by http://pixeldoree.com/vector/free-vector-pinwheels-adobe-illustrator-and-png-files/ .wheel { -webkit-transform-origin: 50% 50%; transform-origin: 193px 164px; animation: spin 4s cubic-bezier(.49,.05,.32,1.04) infinite alternate; } @keyframes spin { 50% { transform: rotate(360deg); } }
  • 44. Zooming out (or in) in Webkit/Blink does not maintain the transform origin at the center of the rotating element. This is a bug. This issue does not happen in Firefox, where the transform origin is set in absolute values (relative to the SVG canvas). Heads up: Transform-Origin Issue In Chrome With Percentage Values
  • 45. → Just use absolute values (for the time being)!
  • 46. Heads up: Hardware Acceleration In Chrome CSS 3D transforms are hardware accelerated in Chrome when applied to HTML elements. However, they are not accelerated when used on SVG elements - they have the same performance profile as SVG transform attributes. They are accelerated in Firefox.
  • 48. Animated Line Drawing To Animate an SVG path you need to know its exact length. Then: #path { stroke-dasharray: pathLength; stroke-dashoffset: pathLength; /* transition stroke-dashoffset */ transition: stroke-dashoffset 2s linear; } svg:hover #path{ stroke-dashoffset: 0; }
  • 49. Example #cable { stroke: #FFF2B1; stroke-dasharray: 4000 4000; stroke-dashoffset: 4000; stroke-width: 4; transition: stroke-dashoffset 8s linear; } svg:hover #cable { stroke-dashoffset: 0; } /* turn lamp on */ .inner-lamp{ fill:grey; transition: fill .5s ease-in 6s; } svg:hover .inner-lamp { fill: #FBFFF8; } /* ... */ The animated path is positioned on top of the black path. When SVG is hovered path is animated, and the lamp is “turned on” after path finishes animation, using a delay that is equal to the animation (transition) time of the path.
  • 51. var path = document.querySelector('.drawing- path');path.getTotalLength(); //set CSS properties up path.style.strokeDasharray = length + ' ' + length; path.style.strokeDashoffset = length; //set transition up path.style.transition = 'stroke-dashoffset 2s ease-in- out';// animatepath.style.strokeDashoffset = '0'; //more: http://jakearchibald.com/2013/animated-line-drawing-svg/ Animated Line Drawing: Retrieving Path Length Using Javascript
  • 53. Animated Paths (Morphing Paths) http://goo.gl/ri9nwU
  • 54. Animated Paths (Morphing Paths) The idea is to create a SVG with one path and to morph that path into another one. You’ll need: the first path, the second path, and possibly intermediate paths (depending on your animation). There is no way in CSS to animate one SVG path into another. http://goo.gl/93gFYh
  • 56. “ By using animated SVGs instead of GIFs we were able to reduce our page size from 1.6 mb to 389 kb, and reduce our page load time from 8.75 s to 412 ms. That’s a huge difference.” Animated SVGs As GIFs Replacement http://oak.is/thinking/animated-svgs/
  • 59. Optimize & Degrade Gracefully http://goo.gl/nhXtbu #MustRead
  • 61. Thank You! @SaraSoueidan / sarasoueidan.com