Skip to content

Instantly share code, notes, and snippets.

@rbrunt
Last active December 27, 2015 23:19
Show Gist options
  • Select an option

  • Save rbrunt/7405475 to your computer and use it in GitHub Desktop.

Select an option

Save rbrunt/7405475 to your computer and use it in GitHub Desktop.
Animated SVG Logo for redesign of my website. Thinking that the animation could be used as a loading indicator... http://bl.ocks.org/rbrunt/7405475
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<style>
body {
width: 100%;
margin: 0;
/*background:#000;*/
}
#logocontainer svg {
width: auto;
height: 100%;
display: block;
margin: 0 auto;
}
#logocontainer{
margin: 0 auto;
display: inline;
}
svg #r {
stroke: #049cd7;
}
svg #b {
stroke: #46a546;
}
path#r, path#b {
stroke-width: 5;
stroke-linecap: square;
fill:none;
}
</style>
<script>
function animateIn() {
var rpath = document.querySelector("#r");
var rlength = rpath.getTotalLength();
// Clear any previous transition
rpath.style.transition = rpath.style.transition = 'none';
// Set up the starting positions
//rpath.style.strokeDasharray = rlength + ' ' + rlength;
if (firefox) {
rpath.style.strokeDasharray = (rlength + 5) + ' ' + (rlength + 10);
if (fixFirefox) {
rpath.style.strokeDashoffset = -(rlength + 5) / 5; // Hack for firefox bug that makes strokeDashOffset depend on width of path
} else {
rpath.style.strokeDashoffset = -rlength;
}
} else {
rpath.style.strokeDasharray = rlength + ' ' + rlength;
rpath.style.strokeDashoffset = -rlength; // Chrome deals with it fine...
}
// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
rpath.getBoundingClientRect();
// Define our transition
var bpath = document.querySelector("#b");
var blength = bpath.getTotalLength();
// Clear any previous transition
bpath.style.transition = bpath.style.transition = 'none';
// Set up the starting positions
//bpath.style.strokeDasharray = blength + ' ' + blength;
if (firefox) {
bpath.style.strokeDasharray = (blength + 5) + ' ' + (blength + 10);
if (fixFirefox) {
bpath.style.strokeDashoffset = -(blength + 5) / 5; // Hack for firefox bug that makes strokeDashOffset depend on width of path
} else {
bpath.style.strokeDashoffset = -blength;
}
} else {
bpath.style.strokeDasharray = blength + ' ' + blength;
bpath.style.strokeDashoffset = -blength;
}
// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
bpath.getBoundingClientRect();
var totalAnimationTime = 1; //total time in seconds for animation
var bDelay = totalAnimationTime / 100;
var rAnimationTime = totalAnimationTime * rlength / blength;
var bAnimationTime = totalAnimationTime - bDelay - rAnimationTime;
rpath.style.transition = 'stroke-dashoffset ' + rAnimationTime + 's ease-in';
bpath.style.transition = 'stroke-dashoffset ' + bAnimationTime + 's ease-out ' + (rAnimationTime + bDelay) + 's';
// path.style.transition = path.style.transition = 'stroke-dashoffset 2s ease-in-out 2s';
// Go!
rpath.style.strokeDashoffset = '0';
bpath.style.strokeDashoffset = '0';
setTimeout(animateOut, totalAnimationTime * 1000 + 250);
}
function animateOut() {
var rpath = document.querySelector("#r");
var rlength = rpath.getTotalLength();
// Clear any previous transition
rpath.style.transition = rpath.style.transition = 'none';
// Set up the starting positions
//rpath.style.strokeDasharray = rlength + ' ' + rlength;
if (firefox) {
rpath.style.strokeDasharray = (rlength + 5) + ' ' + (rlength + 10);
} else {
rpath.style.strokeDasharray = rlength + ' ' + rlength;
}
// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
rpath.getBoundingClientRect();
// Define our transition
var bpath = document.querySelector("#b");
var blength = bpath.getTotalLength();
// Clear any previous transition
bpath.style.transition = bpath.style.transition = 'none';
// Set up the starting positions
//bpath.style.strokeDasharray = blength + ' ' + blength;
if (firefox) {
bpath.style.strokeDasharray = (blength + 5) + ' ' + (blength + 10);
} else {
bpath.style.strokeDasharray = blength + ' ' + blength;
}
// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
bpath.getBoundingClientRect();
var totalAnimationTime = 1; //total time in seconds for animation
var bDelay = totalAnimationTime / 100;
var rAnimationTime = totalAnimationTime * rlength / blength;
var bAnimationTime = totalAnimationTime - bDelay - rAnimationTime;
rpath.style.transition = 'stroke-dashoffset ' + rAnimationTime + 's ease-out ' + (bDelay + bAnimationTime) + 's';
bpath.style.transition = 'stroke-dashoffset ' + bAnimationTime + 's ease-in ';
// path.style.transition = path.style.transition = 'stroke-dashoffset 2s ease-in-out 2s';
// Go!
if (fixFirefox) {
rpath.style.strokeDashoffset = -(rlength + 5) / 5;
bpath.style.strokeDashoffset = -(blength + 5) / 5;
} else {
rpath.style.strokeDashoffset = -rlength;
bpath.style.strokeDashoffset = -blength;
}
setTimeout(animateIn, totalAnimationTime * 1000 + 250);
}
var fixFirefox = false;
var firefox = false;
window.onload = function () {
if (navigator.userAgent.indexOf("Firefox") > 0) {
firefox = true;
if (navigator.userAgent.indexOf("Windows") > 0) {
fixFirefox = true;
}
}
animateIn();
}
</script>
</head>
<div id="logocontainer">
<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 55 65"><path d="M388.976 583.651h-5.492c-3.213 0-6.134 1.178-8.344 3.156-2.553 2.285-4.156 5.632-4.156 9.344v17.5" transform="translate(-368.469 -551.156)" id="r"/><path d="M395.976 553.651v47.5c0 6.925 5.575 12.5 12.5 12.5s12.5-5.575 12.5-12.5v-5c0-6.925-5.575-12.5-12.5-12.5l-5.5-.013" transform="translate(-368.469 -551.156)" id="b"/></svg>
</div>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment