Last active
October 5, 2016 09:52
-
-
Save cgarnier/4799d76603b14c0d19184d4c8c081c10 to your computer and use it in GitHub Desktop.
Revisions
-
cgarnier revised this gist
Oct 5, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /** * Animate a dashed svg path * @param el DOM path element * @param width Width of the dashes * @param space Space between the dashes -
cgarnier renamed this gist
Oct 5, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
cgarnier created this gist
Oct 5, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ /** * Animate a dashed line * @param el DOM path element * @param width Width of the dashes * @param space Space between the dashes */ animateLine (el, width, space) { let total = Math.floor(el.getTotalLength()) let segment = [width, space] console.log('total,', total) var line = { step: 0, dashes: [0] } TweenMax.to(line, 2, { step: total, onUpdate: () => { let dashNumber = line.dashes.reduce((a, b) => a + b, 0) let missing = Math.floor(line.step) - dashNumber let stuffing = 1 + total - (dashNumber + missing) let index = line.dashes.length - 1 while (missing > 0) { if (line.dashes[ index ] === segment[index % 2]) { line.dashes.push(0) index++ } line.dashes[ index ]++ missing-- } let finalArray = [].concat(line.dashes) if (index % 2 === 0) { finalArray.push(stuffing) } else { finalArray = finalArray.concat([width, Math.max(0, stuffing - width)]) } el.setAttribute('stroke-dasharray', finalArray.join(',')) } }) }