SlideShare a Scribd company logo
Animating an SVG Icon
[Demo/Download]
Implement an animated menu Svg icon.
At first, the icon is the classic burger menu icon. But when you click on it, it becomes a close
icon with a fun “ninja” effect. The animation is reversed when you click on the close icon which
turns it into the initial hamburger icon again. Take a look:
Planning
To achieve this effect, I cannot imagine anything better than SVG. And the new library Segment
(which is an alternative to the DrawSVGPlugin from GSAP) provides the necessary utilities to
implement it.
The main idea is to create three paths that describe the trajectory of each bar on the burger
icon when it transforms to the close icon. The Segment library will allow us to animate the path
strokes in the way we want. To draw paths, any vector editor (like Adobe Illustrator or
Inkscape) can be used; in this case we’ll be drawing the paths manually (tying lines, curves and
arcs), because we want to get the best possible accuracy. Keep in mind that we are doing an
animation that contains “elastic” movements, therefore these must be considered in the length
of each path. But before we continue, let’s have a look at Segment.
Introducing Segment
The main tool we’ll be using is Segment, a little JavaScript class (without dependencies) for
drawing and animating SVG path strokes. Using Segment is pretty straightforward:
<!-- Add the segment script(less than 2kb) -->
<scriptsrc="/dist/segment.min.js"></script>
<!-- Define a path somewhere -->
<svg>
<path id="my-path" ...>
</svg>
<script>
// Initializea new Segment with the path
var myPath = document.getElementById("my-path"),
segment = new Segment(myPath);
// Draw a segment of a stroke at the time you want
// Syntax: .draw(begin, end[, duration,options])
segment.draw("25%", "75% - 10", 1);
/* Full examplewith all possibleoptions */
// Define a normalized easingfunction (t parameter will be in the range [0, 1])
function cubicIn(t) {
return t * t * t;
}
// Define a callback function
function done() {
alert("Done!");
}
// Draw the complete path
segment.draw(0, "100%", 1, {delay: 1, easing:cubicIn,callback:done});
</script>
To learn more you can play with the demo and check out the documentation on GitHub. Also, if
you want to understand how Segment works, you can read more about it in this article.
It is important to note that Segment does not include any easing function (except the default
linear one), so we will be using the excellent d3-ease library for this.
Drawing
It’s a very quick animation, but if we analyze the animation frame by frame, we can draw each
path. The result is something like this:
Created from the following code we’ve developed piece by piece:
<svg width="100px" height="100px">
<path d="M 30 40 L 70 40 C 90 40 90 75 60 85 A 40 40 0 0 1 20 20 L 80 80"></path>
<path d="M 30 50 L 70 50"></path>
<path d="M 70 60 L 30 60 C 10 60 10 20 40 15 A 40 38 0 1 1 20 80 L 80 20"></path>
</svg>
Now we need to add the proper CSS styles to the paths to achieve the desired effect, and an id
to access them easily from our script. This is the HTML structure we’ll be using:
<!-- Wrapper -->
<div id="menu-icon-wrapper" class="menu-icon-wrapper">
<!-- SVG element with paths -->
<svg width="100px" height="100px">
<path id="pathA" d="M 30 40 L 70 40 C 90 40 90 75 60 85 A 40 40 0 0 1 20 20 L 80 80"/>
<path id="pathB" d="M 30 50 L 70 50"/>
<path id="pathC" d="M 70 60 L 30 60 C 10 60 10 20 40 15 A 40 38 0 1 1 20 80 L 80 20"/>
</svg>
<!-- Trigger to perform the animations -->
<button id="menu-icon-trigger" class="menu-icon-trigger"></button>
</div>
And the CSS styles:
// The wrapper was defined with a fixed width and height
// Note, that the pointer-events property is set to 'none'.
// We don't need any pointer events in the entire element.
.menu-icon-wrapper{
position:relative;
display:inline-block;
width: 34px;
height: 34px;
pointer-events: none;
transition:0.1s;
}
// To perform the scaled transformfor the second demo
.menu-icon-wrapper.scaled{
transform: scale(0.5);
}
// Adjustingthe position of the SVG element
.menu-icon-wrapper svg{
position:absolute;
top: -33px;
left: -33px;
}
// Defining the styles for the path elements
.menu-icon-wrapper svg path{
stroke: #fff;
stroke-width: 6px;
stroke-linecap:round;
fill:transparent;
}
// Setting the pointer-events property to 'auto',
// and allowingonly events for the trigger element
.menu-icon-wrapper .menu-icon-trigger{
position:relative;
width: 100%;
height: 100%;
cursor:pointer;
pointer-events: auto;
background: none;
border: none;
margin: 0;
padding:0;
}
Animating
With the SVG code ready, our task now is to figure out or to guess the easing functions used in
each section of the animation, and to achieve a proper synchronization, always guided by the
animated GIF. Let’s see how to animate the top and bottom bars of the hamburger icon. First,
we need to initialize a segment for each bar with the initial begin and end values. Because we
don’t have the information at hand but only the visual animation of the GIF, this is a trial and
error process until we find the right values.
var pathA = document.getElementById('pathA'),
pathC = document.getElementById('pathC'),
segmentA = new Segment(pathA, 8, 32),
segmentC = new Segment(pathC, 8, 32);
With that we are ready to animate, always keeping the same length (end - begin = 24) during the
whole animation. Analyzing the animation sequence, we can see that the first part starts with a
linear easing function, and ends with an elastic one. We’ll be using functions that receive the
segment as a parameter to reuse the same function with the top and bottom bars, because they
will be animated in the same way.
[Demo/Download]

More Related Content

What's hot

Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 Canvas
Henry Osborne
 
Customizer-ing Theme Options: A Visual Playground
Customizer-ing Theme Options: A Visual PlaygroundCustomizer-ing Theme Options: A Visual Playground
Customizer-ing Theme Options: A Visual Playground
DrewAPicture
 
jQuery for Beginners
jQuery for Beginners jQuery for Beginners
jQuery for Beginners
NAILBITER
 
Илья Пухальский (EPAM Systems)
Илья Пухальский (EPAM Systems)Илья Пухальский (EPAM Systems)
Илья Пухальский (EPAM Systems)
Ontico
 
響應式網頁實作坊
響應式網頁實作坊響應式網頁實作坊
響應式網頁實作坊
Chih-cheng Wang
 
Intro to HTML5 Canvas
Intro to HTML5 CanvasIntro to HTML5 Canvas
Intro to HTML5 Canvas
Juho Vepsäläinen
 
Advanced html5 diving into the canvas tag
Advanced html5 diving into the canvas tagAdvanced html5 diving into the canvas tag
Advanced html5 diving into the canvas tag
David Voyles
 
Html5 canvas
Html5 canvasHtml5 canvas
Html5 canvas
Gary Yeh
 
HTML CANVAS
HTML CANVASHTML CANVAS
HTML CANVAS
Light Salt
 
HTML5 Canvas - The Future of Graphics on the Web
HTML5 Canvas - The Future of Graphics on the WebHTML5 Canvas - The Future of Graphics on the Web
HTML5 Canvas - The Future of Graphics on the Web
Robin Hawkes
 
Quick ref capybara
Quick ref capybaraQuick ref capybara
Quick ref capybara
fatec
 

What's hot (11)

Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 Canvas
 
Customizer-ing Theme Options: A Visual Playground
Customizer-ing Theme Options: A Visual PlaygroundCustomizer-ing Theme Options: A Visual Playground
Customizer-ing Theme Options: A Visual Playground
 
jQuery for Beginners
jQuery for Beginners jQuery for Beginners
jQuery for Beginners
 
Илья Пухальский (EPAM Systems)
Илья Пухальский (EPAM Systems)Илья Пухальский (EPAM Systems)
Илья Пухальский (EPAM Systems)
 
響應式網頁實作坊
響應式網頁實作坊響應式網頁實作坊
響應式網頁實作坊
 
Intro to HTML5 Canvas
Intro to HTML5 CanvasIntro to HTML5 Canvas
Intro to HTML5 Canvas
 
Advanced html5 diving into the canvas tag
Advanced html5 diving into the canvas tagAdvanced html5 diving into the canvas tag
Advanced html5 diving into the canvas tag
 
Html5 canvas
Html5 canvasHtml5 canvas
Html5 canvas
 
HTML CANVAS
HTML CANVASHTML CANVAS
HTML CANVAS
 
HTML5 Canvas - The Future of Graphics on the Web
HTML5 Canvas - The Future of Graphics on the WebHTML5 Canvas - The Future of Graphics on the Web
HTML5 Canvas - The Future of Graphics on the Web
 
Quick ref capybara
Quick ref capybaraQuick ref capybara
Quick ref capybara
 

Viewers also liked

Management final presentation 1
Management final presentation 1Management final presentation 1
Management final presentation 1
Luke Snyder
 
Resume For JohnWagnon
Resume For JohnWagnonResume For JohnWagnon
Resume For JohnWagnon
John Wagnon
 
Kaldor 2015 From the Beginning: First Day Strategies and Activities to Improv...
Kaldor 2015 From the Beginning: First Day Strategies and Activities to Improv...Kaldor 2015 From the Beginning: First Day Strategies and Activities to Improv...
Kaldor 2015 From the Beginning: First Day Strategies and Activities to Improv...
Eric Kaldor
 
yogesh_edited (1)
yogesh_edited (1)yogesh_edited (1)
yogesh_edited (1)
puppala yogesh
 
محاضرة التغذية الثانيه
محاضرة التغذية الثانيهمحاضرة التغذية الثانيه
محاضرة التغذية الثانيه
Anwar Al-thomali
 
RAA_Case Study_Namatjira_Jun'15
RAA_Case Study_Namatjira_Jun'15RAA_Case Study_Namatjira_Jun'15
RAA_Case Study_Namatjira_Jun'15
Holly Rankin-Smith
 
IntroductionToHydroponics
IntroductionToHydroponicsIntroductionToHydroponics
IntroductionToHydroponics
lfruberg
 
C pythontalk
C pythontalkC pythontalk
C pythontalk
Nicholaus Jackson
 
АТЛАНТ
АТЛАНТАТЛАНТ
АТЛАНТ
Artem Kozhukhovskiy
 
El Color Violeta
El Color VioletaEl Color Violeta
El Color Violeta
Aura Estrada
 
Quimica.
Quimica.Quimica.
Quimica.
Stefany Muñiz
 
MGMT 4710 Disney Case
MGMT 4710 Disney CaseMGMT 4710 Disney Case
MGMT 4710 Disney Case
Eden Schweigler
 
revenue analysis
revenue analysisrevenue analysis
revenue analysis
Shadman Mohammad
 

Viewers also liked (14)

Management final presentation 1
Management final presentation 1Management final presentation 1
Management final presentation 1
 
Resume For JohnWagnon
Resume For JohnWagnonResume For JohnWagnon
Resume For JohnWagnon
 
Kaldor 2015 From the Beginning: First Day Strategies and Activities to Improv...
Kaldor 2015 From the Beginning: First Day Strategies and Activities to Improv...Kaldor 2015 From the Beginning: First Day Strategies and Activities to Improv...
Kaldor 2015 From the Beginning: First Day Strategies and Activities to Improv...
 
yogesh_edited (1)
yogesh_edited (1)yogesh_edited (1)
yogesh_edited (1)
 
محاضرة التغذية الثانيه
محاضرة التغذية الثانيهمحاضرة التغذية الثانيه
محاضرة التغذية الثانيه
 
RAA_Case Study_Namatjira_Jun'15
RAA_Case Study_Namatjira_Jun'15RAA_Case Study_Namatjira_Jun'15
RAA_Case Study_Namatjira_Jun'15
 
IntroductionToHydroponics
IntroductionToHydroponicsIntroductionToHydroponics
IntroductionToHydroponics
 
C pythontalk
C pythontalkC pythontalk
C pythontalk
 
Salveo hiver 2015-16
Salveo hiver 2015-16Salveo hiver 2015-16
Salveo hiver 2015-16
 
АТЛАНТ
АТЛАНТАТЛАНТ
АТЛАНТ
 
El Color Violeta
El Color VioletaEl Color Violeta
El Color Violeta
 
Quimica.
Quimica.Quimica.
Quimica.
 
MGMT 4710 Disney Case
MGMT 4710 Disney CaseMGMT 4710 Disney Case
MGMT 4710 Disney Case
 
revenue analysis
revenue analysisrevenue analysis
revenue analysis
 

Similar to Animating an svg icon

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
 
SVG introduction
SVG   introductionSVG   introduction
SVG introduction
SathyaseelanK1
 
I Can't Believe It's Not Flash
I Can't Believe It's Not FlashI Can't Believe It's Not Flash
I Can't Believe It's Not Flash
Thomas Fuchs
 
SVG vs Canvas - Showdown of the Painters
SVG vs Canvas - Showdown of the PaintersSVG vs Canvas - Showdown of the Painters
SVG vs Canvas - Showdown of the Painters
Phil Reither
 
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Guillaume Kossi
 
Svg Overview And Js Libraries
Svg Overview And Js LibrariesSvg Overview And Js Libraries
Svg Overview And Js Libraries
seamusjr
 
SVG - Scalable Vector Graphic
SVG - Scalable Vector GraphicSVG - Scalable Vector Graphic
SVG - Scalable Vector Graphic
Akila Iroshan
 
Css3
Css3Css3
Html5 SVG
Html5 SVGHtml5 SVG
Html5 SVG
Nisa Soomro
 
SVGD3Angular2React
SVGD3Angular2ReactSVGD3Angular2React
SVGD3Angular2React
Oswald Campesato
 
Drawing a Circle Three Ways: Generating Graphics for the Web
Drawing a Circle Three Ways: Generating Graphics for the WebDrawing a Circle Three Ways: Generating Graphics for the Web
Drawing a Circle Three Ways: Generating Graphics for the Web
Cloudinary
 
Unlocking the full potential of SVG
Unlocking the full potential of SVGUnlocking the full potential of SVG
Unlocking the full potential of SVG
Magdalena Magličić
 
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Codemotion
 
CSS3 Takes on the World
CSS3 Takes on the WorldCSS3 Takes on the World
CSS3 Takes on the World
Jonathan Snook
 
Creating dynamic SVG elements in JavaScript
Creating dynamic SVG elements in JavaScriptCreating dynamic SVG elements in JavaScript
Creating dynamic SVG elements in JavaScript
Joseph Khan
 
Web Vector Graphics
Web Vector GraphicsWeb Vector Graphics
Web Vector Graphics
Dmitry Baranovskiy
 
STC Summit 2015 Hypergraphics for visual-first help
STC Summit 2015 Hypergraphics for visual-first helpSTC Summit 2015 Hypergraphics for visual-first help
STC Summit 2015 Hypergraphics for visual-first help
Dave Gardiner
 
CANVAS, SVG, WebGL, CSS3, WebEvent
CANVAS, SVG, WebGL, CSS3, WebEventCANVAS, SVG, WebGL, CSS3, WebEvent
CANVAS, SVG, WebGL, CSS3, WebEvent
민태 김
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdf
ShaiAlmog1
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 Canvas
Mindy McAdams
 

Similar to Animating an svg icon (20)

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)
 
SVG introduction
SVG   introductionSVG   introduction
SVG introduction
 
I Can't Believe It's Not Flash
I Can't Believe It's Not FlashI Can't Believe It's Not Flash
I Can't Believe It's Not Flash
 
SVG vs Canvas - Showdown of the Painters
SVG vs Canvas - Showdown of the PaintersSVG vs Canvas - Showdown of the Painters
SVG vs Canvas - Showdown of the Painters
 
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
 
Svg Overview And Js Libraries
Svg Overview And Js LibrariesSvg Overview And Js Libraries
Svg Overview And Js Libraries
 
SVG - Scalable Vector Graphic
SVG - Scalable Vector GraphicSVG - Scalable Vector Graphic
SVG - Scalable Vector Graphic
 
Css3
Css3Css3
Css3
 
Html5 SVG
Html5 SVGHtml5 SVG
Html5 SVG
 
SVGD3Angular2React
SVGD3Angular2ReactSVGD3Angular2React
SVGD3Angular2React
 
Drawing a Circle Three Ways: Generating Graphics for the Web
Drawing a Circle Three Ways: Generating Graphics for the WebDrawing a Circle Three Ways: Generating Graphics for the Web
Drawing a Circle Three Ways: Generating Graphics for the Web
 
Unlocking the full potential of SVG
Unlocking the full potential of SVGUnlocking the full potential of SVG
Unlocking the full potential of SVG
 
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
 
CSS3 Takes on the World
CSS3 Takes on the WorldCSS3 Takes on the World
CSS3 Takes on the World
 
Creating dynamic SVG elements in JavaScript
Creating dynamic SVG elements in JavaScriptCreating dynamic SVG elements in JavaScript
Creating dynamic SVG elements in JavaScript
 
Web Vector Graphics
Web Vector GraphicsWeb Vector Graphics
Web Vector Graphics
 
STC Summit 2015 Hypergraphics for visual-first help
STC Summit 2015 Hypergraphics for visual-first helpSTC Summit 2015 Hypergraphics for visual-first help
STC Summit 2015 Hypergraphics for visual-first help
 
CANVAS, SVG, WebGL, CSS3, WebEvent
CANVAS, SVG, WebGL, CSS3, WebEventCANVAS, SVG, WebGL, CSS3, WebEvent
CANVAS, SVG, WebGL, CSS3, WebEvent
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdf
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 Canvas
 

Recently uploaded

快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
3a0sd7z3
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
Donato Onofri
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
rtunex8r
 
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
dtagbe
 
cyber crime.pptx..........................
cyber crime.pptx..........................cyber crime.pptx..........................
cyber crime.pptx..........................
GNAMBIKARAO
 
Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
Tarandeep Singh
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
3a0sd7z3
 
How to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdfHow to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdf
Infosec train
 
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
APNIC
 
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
thezot
 
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
APNIC
 

Recently uploaded (11)

快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
 
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
 
cyber crime.pptx..........................
cyber crime.pptx..........................cyber crime.pptx..........................
cyber crime.pptx..........................
 
Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
 
How to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdfHow to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdf
 
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
 
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
 
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
 

Animating an svg icon

  • 1. Animating an SVG Icon [Demo/Download] Implement an animated menu Svg icon. At first, the icon is the classic burger menu icon. But when you click on it, it becomes a close icon with a fun “ninja” effect. The animation is reversed when you click on the close icon which turns it into the initial hamburger icon again. Take a look: Planning To achieve this effect, I cannot imagine anything better than SVG. And the new library Segment (which is an alternative to the DrawSVGPlugin from GSAP) provides the necessary utilities to implement it. The main idea is to create three paths that describe the trajectory of each bar on the burger icon when it transforms to the close icon. The Segment library will allow us to animate the path strokes in the way we want. To draw paths, any vector editor (like Adobe Illustrator or Inkscape) can be used; in this case we’ll be drawing the paths manually (tying lines, curves and arcs), because we want to get the best possible accuracy. Keep in mind that we are doing an animation that contains “elastic” movements, therefore these must be considered in the length of each path. But before we continue, let’s have a look at Segment.
  • 2. Introducing Segment The main tool we’ll be using is Segment, a little JavaScript class (without dependencies) for drawing and animating SVG path strokes. Using Segment is pretty straightforward: <!-- Add the segment script(less than 2kb) --> <scriptsrc="/dist/segment.min.js"></script> <!-- Define a path somewhere --> <svg> <path id="my-path" ...> </svg> <script> // Initializea new Segment with the path var myPath = document.getElementById("my-path"), segment = new Segment(myPath); // Draw a segment of a stroke at the time you want // Syntax: .draw(begin, end[, duration,options]) segment.draw("25%", "75% - 10", 1); /* Full examplewith all possibleoptions */ // Define a normalized easingfunction (t parameter will be in the range [0, 1]) function cubicIn(t) { return t * t * t; } // Define a callback function function done() { alert("Done!"); } // Draw the complete path segment.draw(0, "100%", 1, {delay: 1, easing:cubicIn,callback:done}); </script> To learn more you can play with the demo and check out the documentation on GitHub. Also, if you want to understand how Segment works, you can read more about it in this article. It is important to note that Segment does not include any easing function (except the default linear one), so we will be using the excellent d3-ease library for this. Drawing It’s a very quick animation, but if we analyze the animation frame by frame, we can draw each path. The result is something like this:
  • 3. Created from the following code we’ve developed piece by piece: <svg width="100px" height="100px"> <path d="M 30 40 L 70 40 C 90 40 90 75 60 85 A 40 40 0 0 1 20 20 L 80 80"></path> <path d="M 30 50 L 70 50"></path> <path d="M 70 60 L 30 60 C 10 60 10 20 40 15 A 40 38 0 1 1 20 80 L 80 20"></path> </svg> Now we need to add the proper CSS styles to the paths to achieve the desired effect, and an id to access them easily from our script. This is the HTML structure we’ll be using: <!-- Wrapper --> <div id="menu-icon-wrapper" class="menu-icon-wrapper"> <!-- SVG element with paths --> <svg width="100px" height="100px"> <path id="pathA" d="M 30 40 L 70 40 C 90 40 90 75 60 85 A 40 40 0 0 1 20 20 L 80 80"/> <path id="pathB" d="M 30 50 L 70 50"/> <path id="pathC" d="M 70 60 L 30 60 C 10 60 10 20 40 15 A 40 38 0 1 1 20 80 L 80 20"/> </svg> <!-- Trigger to perform the animations --> <button id="menu-icon-trigger" class="menu-icon-trigger"></button> </div> And the CSS styles:
  • 4. // The wrapper was defined with a fixed width and height // Note, that the pointer-events property is set to 'none'. // We don't need any pointer events in the entire element. .menu-icon-wrapper{ position:relative; display:inline-block; width: 34px; height: 34px; pointer-events: none; transition:0.1s; } // To perform the scaled transformfor the second demo .menu-icon-wrapper.scaled{ transform: scale(0.5); } // Adjustingthe position of the SVG element .menu-icon-wrapper svg{ position:absolute; top: -33px; left: -33px; } // Defining the styles for the path elements .menu-icon-wrapper svg path{ stroke: #fff; stroke-width: 6px; stroke-linecap:round; fill:transparent; } // Setting the pointer-events property to 'auto', // and allowingonly events for the trigger element .menu-icon-wrapper .menu-icon-trigger{ position:relative; width: 100%; height: 100%; cursor:pointer; pointer-events: auto; background: none; border: none; margin: 0; padding:0; } Animating With the SVG code ready, our task now is to figure out or to guess the easing functions used in each section of the animation, and to achieve a proper synchronization, always guided by the animated GIF. Let’s see how to animate the top and bottom bars of the hamburger icon. First,
  • 5. we need to initialize a segment for each bar with the initial begin and end values. Because we don’t have the information at hand but only the visual animation of the GIF, this is a trial and error process until we find the right values. var pathA = document.getElementById('pathA'), pathC = document.getElementById('pathC'), segmentA = new Segment(pathA, 8, 32), segmentC = new Segment(pathC, 8, 32); With that we are ready to animate, always keeping the same length (end - begin = 24) during the whole animation. Analyzing the animation sequence, we can see that the first part starts with a linear easing function, and ends with an elastic one. We’ll be using functions that receive the segment as a parameter to reuse the same function with the top and bottom bars, because they will be animated in the same way. [Demo/Download]