javascript - Animate the drawing of a dashed svg line -
i'm trying animate arrow dashed line, ------->, horizontal , vertical path, using svg , css animations.
i've tried couple of different things, animating height , width, using bottom , top, each way has had doesn't quite or work well.
i managed path drawn svg, animation remove dashes , draw solid line.
no animation: http://jsfiddle.net/ehan4/2/
<svg version="1.1" id="layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="252px" height="396px" viewbox="0 0 252 396" enable-background="new 0 0 252 396" xml:space="preserve"> <path stroke="#000" stroke-width="1.5" fill="none" d="m50.887,170.063v-53.488h55.814" stroke-dasharray="5,5" stroke-dashoffset="0.00" />
with animation: http://jsfiddle.net/ehan4/1/
<svg version="1.1" id="layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="252px" height="396px" viewbox="0 0 252 396" enable-background="new 0 0 252 396" xml:space="preserve"> <path stroke="#000" stroke-width="1.5" fill="none" d="m50.887,170.063v-53.488h55.814" stroke-dasharray="5,5" stroke-dashoffset="0.00" />
var path = document.queryselector('path'); var length = path.gettotallength(); path.style.transition = path.style.webkittransition = 'none'; path.style.strokedasharray = length + ' ' + length; path.style.strokedashoffset = length; path.getboundingclientrect(); path.style.transition = path.style.webkittransition = 'stroke-dashoffset 2s ease-in-out'; path.style.strokedashoffset = '0';
any thoughts or ideas appreciated!
z
this function can animate drawing of dashed path:
function drawpath(path, options) { options = options || {} var duration = options.duration || 5000 var easing = options.easing || 'ease-in-out' var reverse = options.reverse || false var undraw = options.undraw || false var callback = options.callback || function () {} var length = path.gettotallength() var dashoffsetstates = [length, 0] if (reverse) { dashoffsetstates = [length, 2 * length] } if (undraw) { dashoffsetstates.reverse() } // clear previous transition path.style.transition = path.style.webkittransition = 'none'; var dasharray = path.style.strokedasharray || path.getattribute("stroke-dasharray"); if (dasharray != '') { // dashed path case // repeats dash pattern many times needed cover path length var dashlength = dasharray.split(/[\s,]/).map(function (a) { return parsefloat(a) || 0 }).reduce(function (a, b) { return + b }) var dashcount = length / dashlength + 1 var = new array(math.ceil(dashcount)).join(dasharray + " ") path.style.strokedasharray = + '0' + ' ' + length } else { // non dashed path case path.style.strokedasharray = length + ' ' + length; } path.style.strokedashoffset = dashoffsetstates[0]; path.getboundingclientrect(); path.style.transition = path.style.webkittransition = 'stroke-dashoffset ' + duration + 'ms ' + easing; path.style.strokedashoffset = dashoffsetstates[1] settimeout(function() { path.style.strokedasharray = dasharray //set original dash array callback() }, duration) }
it repeats dash pattern many times needed cover path length , adds empty dash length of path. animates dash offset, in example.
working demo: http://jsfiddle.net/u88bm18b/
Comments
Post a Comment