Created
January 27, 2015 07:02
-
-
Save iampeterbanjo/066794ee9195d6bcd455 to your computer and use it in GitHub Desktop.
Animating SVG paths using Snap SVG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function revealPath (path, options) { | |
var revealedAttributes = {'stroke-dashoffset': 0} | |
, options = options || {} | |
, easing = mina[options.ease] || mina.easein | |
, duration = options.duration || 1000 | |
path.animate(revealedAttributes, duration, easing, options.afterRevealPath) | |
} | |
function getHiddenAttributes (path) { | |
var length = path.getTotalLength() | |
return { | |
'stroke-dasharray': length + ' ' + length | |
, 'stroke-dashoffset': length | |
} | |
} | |
function processPaths (paths, options) { | |
var options = options || {} | |
, callback = options.afterProcessPaths | |
, path | |
for(var i =0; i < paths.length; i++){ | |
path = Snap(paths[i]) | |
path.attr(getHiddenAttributes(path)) | |
revealPath(path, options) | |
} | |
if(typeof callback == 'function'){ | |
callback.call() | |
} | |
} | |
function animateAllSvgPaths (selector, options) { | |
var svg = Snap(selector) | |
, allPaths = svg.selectAll('path') | |
processPaths(allPaths, options) | |
} | |
function animateAllPaths (options) { | |
var paths = document.querySelectorAll('path') | |
processPaths(paths, options) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment