Created
May 20, 2015 20:04
-
-
Save caiodv/3eb073a9babf37e6e572 to your computer and use it in GitHub Desktop.
Exports Illustrator bezier curves to JSON
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
main(); | |
function main(){ | |
var doc = app.activeDocument; | |
jsonText = '{"paths":[' | |
for(var i=0;i<doc.pathItems.length;i++){ | |
var _path = doc.pathItems[i]; | |
var _points = _path.pathPoints; | |
jsonText += '{"bezier":[' | |
for (var j = 0; j < _points.length; j++) { | |
// jsonText += '{"x":'+_points[j].anchor[0]+',"y":'+_points[j].anchor[1]+'}' | |
if(j == 0){ | |
jsonText += '{"x":'+_path.pathPoints[j].anchor[0]+',"y":'+(_path.pathPoints[j].anchor[1]*-1)+'}'; | |
} | |
else{ | |
jsonText += '{"x":'+_path.pathPoints[j].anchor[0]+',"y":'+(_path.pathPoints[j].anchor[1]*-1)+','; | |
jsonText += '"curves":[' | |
+_path.pathPoints[j-1].rightDirection[0] + ", " | |
+(_path.pathPoints[j-1].rightDirection[1]*-1) + ", " | |
+_path.pathPoints[j].leftDirection[0] + ", " | |
+(_path.pathPoints[j].leftDirection[1]*-1) + ", " | |
+_path.pathPoints[j].anchor[0] + ", " | |
+(_path.pathPoints[j].anchor[1]*-1) | |
+']}'; | |
} | |
// jsonText += _path.pathPoints[j].rightDirection[0]+", "+(_path.pathPoints[j].rightDirection[1]*-1)+", "+_path.pathPoints[0].leftDirection[0]+", "+(_path.pathPoints[0].leftDirection[1]*-1)+", "+_path.pathPoints[0].anchor[0]+", "+(_path.pathPoints[0].anchor[1]*-1)+"]"; | |
if(j != _points.length - 1){ | |
jsonText += ',' | |
} | |
} | |
jsonText += ']}' | |
if(i != doc.pathItems.length - 1){ | |
jsonText += ',' | |
} | |
} | |
jsonText += ']}' | |
var name = decodeURI(doc.name); | |
name = name.substring(0, name.indexOf(".")); | |
var file = new File(doc.path.toString() + "/" + name + ".json"); | |
file.remove(); | |
file.open("w", "TEXT"); | |
file.lineFeed = "\n"; | |
file.write(jsonText); | |
file.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment