Last active
October 30, 2016 19:12
-
-
Save iegik/769625f374a2c825e52268c06296295d to your computer and use it in GitHub Desktop.
SVG path data to array of objects
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
| var d = 'M100,35c0-13-11-25-25-25s-25,12-25,18c0-5-11-18-25-18s-25,11-25,25c0,20,36,49,50,62,14-14,50-43,50-62z'; | |
| var pd = new PathData(); | |
| pd.fromString(d); | |
| pd.scale(2) | |
| pd.toString() |
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 PathData(d){ | |
| this._changed = false; | |
| this.text = d; | |
| this.data = this.fromString(d) | |
| return this; | |
| } | |
| PathData.prototype.fromString = (d)=>d.replace(/\B([A-z])/g,';$1').split(';') | |
| .map((p)=>( | |
| a=p.replace(/([a-zA-Z])/g, '$1:').replace(/(\d)(\-)/g,'$1,$2').split(':'), | |
| o={}, | |
| o[a[0]]=a[1] | |
| .split(',').map(Number), | |
| o | |
| )); | |
| PathData.prototype.toString = function(){ | |
| return this.data.map((p)=>( | |
| m = Object.keys(p)[0], | |
| m + p[m] | |
| )).join(''); | |
| } | |
| PathData.prototype.scale = function (x,y){ | |
| y=y||x; | |
| if(this.changed = typeof x !== 'undefined' || (x === 1 && y === 1)){ | |
| this.data = this.data.map((p)=>( | |
| m = Object.keys(p)[0], | |
| p[m]=p[m].map((z)=>(z*x)), | |
| p | |
| )); | |
| } | |
| return this; | |
| }; | |
| module.exports = PathData; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment