svg-path-utils
Version:
Some utils for SVG's path data like path data generation, inverse path data calculation, ...
1 lines • 1.99 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n.svg_path_utils=n.svg_path_utils||{})}(this,function(n){"use strict";var t=function(){var n={};return n.M=function(n,t){return"M"+n+","+t},n.m=function(n,t){return"m"+n+","+t},n.L=function(n,t){return"L"+n+","+t},n.l=function(n,t){return"l"+n+","+t},n.H=function(n){return"H"+n},n.h=function(n){return"h"+n},n.V=function(n){return"V"+n},n.v=function(n){return"v"+n},n.C=function(n,t,r,e,u,o){return"C"+n+","+t+" "+r+","+e+" "+u+","+o},n.c=function(n,t,r,e,u,o){return"c"+n+","+t+" "+r+","+e+" "+u+","+o},n.S=function(n,t,r,e){return"S"+n+","+t+" "+r+","+e},n.s=function(n,t,r,e){return"s"+n+","+t+" "+r+","+e},n.Q=function(n,t,r,e){return"Q"+n+","+t+" "+r+","+e},n.q=function(n,t,r,e){return"q"+n+","+t+" "+r+","+e},n.T=function(n,t){return"T"+n+","+t},n.t=function(n,t){return"t"+n+","+t},n.Z=function(){return"Z"},n.z=function(){return"z"},n.angle=function(n,t){return 180*Math.atan2(t.y-n.y,t.x-n.x)/Math.PI},n.parse=function(t){var r,e=t.replace(/[\d,\-\s]+/g,"").split(""),u=[],o=t.replace(/[A-Za-z,]+/g," ").trim().replace(/\s\s+/g," ").split(" "),i=-1;return e.forEach(function(t){if("function"==typeof(r=n[t]))if(1===r.length)u.push({x:+o[++i]});else for(var e=-1,f=r.length/2;++e<f;)u.push({x:+o[++i],y:+o[++i]})}),{operators:e,points:u}},n.generate=function(t){var r,e=t.points.slice(),u=[];return t.operators.forEach(function(t){if("function"==typeof(r=n[t])){var o=[];if(1===r.length)o.push(e.shift().x);else for(var i=-1,f=r.length/2;++i<f;){var s=e.shift();o.push(s.x,s.y)}u.push(r.apply(null,o))}}),u.join(" ")},n.inversePath=function(t){var r,e,u=n.parse(t),o=u.operators.reverse(),i=u.points.reverse();return r=o.pop(),"z"===(e=o[0]).toLowerCase()&&(o.push(e),o.shift()),o.unshift(r),n.generate({operators:o,points:i})},n.join=function(){return arguments.length?Array.prototype.join.call(arguments," "):void 0},n};n.SVGPathUtils=t});