Skip to content

Instantly share code, notes, and snippets.

@iampeterbanjo
Created January 27, 2015 07:02
Show Gist options
  • Save iampeterbanjo/066794ee9195d6bcd455 to your computer and use it in GitHub Desktop.
Save iampeterbanjo/066794ee9195d6bcd455 to your computer and use it in GitHub Desktop.
Animating SVG paths using Snap SVG
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