path2d-polyfill
Advanced tools
Comparing version 0.4.2 to 1.0.0
@@ -13,4 +13,4 @@ var ARG_LENGTH = { | ||
}; | ||
var SEGMENT_PATTERN = /([astvzqmhlc])([^astvzqmhlc]*)/ig; | ||
var NUMBER = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/ig; | ||
var SEGMENT_PATTERN = /([astvzqmhlc])([^astvzqmhlc]*)/gi; | ||
var NUMBER = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi; | ||
@@ -97,19 +97,32 @@ function parseValues(args) { | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
if (Array.isArray(arr)) return _arrayLikeToArray(arr); | ||
} | ||
function _iterableToArray(iter) { | ||
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance"); | ||
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
@@ -159,12 +172,10 @@ | ||
/** | ||
* Crates a Path2D polyfill object | ||
* @constructor | ||
* @ignore | ||
* @param {String} path | ||
*/ | ||
* Crates a Path2D polyfill object | ||
* @constructor | ||
* @ignore | ||
* @param {String} path | ||
*/ | ||
var Path2D = | ||
/*#__PURE__*/ | ||
function () { | ||
var Path2D = /*#__PURE__*/function () { | ||
function Path2D(path) { | ||
@@ -563,4 +574,2 @@ _classCallCheck(this, Path2D); | ||
default: // throw new Error(`${pathType} is not implemented`); ? | ||
} | ||
@@ -567,0 +576,0 @@ |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).path2dPolyfill={})}(this,function(e){"use strict";var i={a:7,c:6,h:1,l:2,m:2,q:4,s:4,t:2,v:1,z:0},a=/([astvzqmhlc])([^astvzqmhlc]*)/gi,c=/-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi;var n=function(e){var o=[],t=String(e).trim();return"M"!==t[0]&&"m"!==t[0]||t.replace(a,function(e,t,a){var n=t.toLowerCase(),r=function(e){var t=e.match(c);return t?t.map(Number):[]}(a),s=t;if("m"===n&&2<r.length&&(o.push([s].concat(r.splice(0,2))),n="l",s="m"===s?"l":"L"),r.length<i[n])return"";for(o.push([s].concat(r.splice(0,i[n])));r.length>=i[n]&&r.length&&i[n];)o.push([s].concat(r.splice(0,i[n])));return""}),o};function o(e,t){for(var a=0;a<t.length;a++){var n=t[a];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function l(e){return function(e){if(Array.isArray(e)){for(var t=0,a=new Array(e.length);t<e.length;t++)a[t]=e[t];return a}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function L(e,t){var a=e.x*Math.cos(t)-e.y*Math.sin(t),n=e.y*Math.cos(t)+e.x*Math.sin(t);e.x=a,e.y=n}function O(e,t){e.x*=t,e.y*=t}var t=function(e){if(void 0!==e&&e.CanvasRenderingContext2D&&(!e.Path2D||!function(e){var t=e.document.createElement("canvas").getContext("2d"),a=new e.Path2D("M0 0 L1 1");return t.strokeStyle="red",t.lineWidth=1,t.stroke(a),255===t.getImageData(0,0,1,1).data[0]}(e))){var t=function(){function a(e){var t;(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")})(this,a),this.segments=[],e&&e instanceof a?(t=this.segments).push.apply(t,l(e.segments)):e&&(this.segments=n(e))}return function(e,t,a){t&&o(e.prototype,t),a&&o(e,a)}(a,[{key:"addPath",value:function(e){var t;e&&e instanceof a&&(t=this.segments).push.apply(t,l(e.segments))}},{key:"moveTo",value:function(e,t){this.segments.push(["M",e,t])}},{key:"lineTo",value:function(e,t){this.segments.push(["L",e,t])}},{key:"arc",value:function(e,t,a,n,r,s){this.segments.push(["AC",e,t,a,n,r,!!s])}},{key:"arcTo",value:function(e,t,a,n,r){this.segments.push(["AT",e,t,a,n,r])}},{key:"ellipse",value:function(e,t,a,n,r,s,o,i){this.segments.push(["E",e,t,a,n,r,s,o,!!i])}},{key:"closePath",value:function(){this.segments.push(["Z"])}},{key:"bezierCurveTo",value:function(e,t,a,n,r,s){this.segments.push(["C",e,t,a,n,r,s])}},{key:"quadraticCurveTo",value:function(e,t,a,n){this.segments.push(["Q",e,t,a,n])}},{key:"rect",value:function(e,t,a,n){this.segments.push(["R",e,t,a,n])}}]),a}(),r=e.CanvasRenderingContext2D.prototype.fill,a=e.CanvasRenderingContext2D.prototype.stroke;e.CanvasRenderingContext2D.prototype.fill=function(){for(var e=arguments.length,t=new Array(e),a=0;a<e;a++)t[a]=arguments[a];var n="nonzero";0===t.length||1===t.length&&"string"==typeof t[0]?r.apply(this,t):(2===arguments.length&&(n=t[1]),s(this,t[0].segments),r.call(this,n))},e.CanvasRenderingContext2D.prototype.stroke=function(e){e&&s(this,e.segments),a.call(this)},e.Path2D=t}function s(e,t){var a,n,r,s,o,i,c,l,u,h,f,y,p,v,g,m,d,b,x,k,C,T,P,M,w,q,A,z,D,R={x:0,y:0},j={x:0,y:0};e.beginPath();for(var S=0;S<t.length;++S){var E=t[S];switch("S"!==(k=E[0])&&"s"!==k&&"C"!==k&&"c"!==k&&(P=T=null),"T"!==k&&"t"!==k&&"Q"!==k&&"q"!==k&&(w=M=null),k){case"m":case"M":"m"===k?(f+=E[1],p+=E[2]):(f=E[1],p=E[2]),"M"!==k&&R||(R={x:f,y:p}),e.moveTo(f,p);break;case"l":f+=E[1],p+=E[2],e.lineTo(f,p);break;case"L":f=E[1],p=E[2],e.lineTo(f,p);break;case"H":f=E[1],e.lineTo(f,p);break;case"h":f+=E[1],e.lineTo(f,p);break;case"V":p=E[1],e.lineTo(f,p);break;case"v":p+=E[1],e.lineTo(f,p);break;case"a":case"A":"a"===k?(f+=E[6],p+=E[7]):(f=E[6],p=E[7]),m=E[1],d=E[2],c=E[3]*Math.PI/180,r=!!E[4],s=!!E[5],o={x:f,y:p},L(i={x:(j.x-o.x)/2,y:(j.y-o.y)/2},-c),1<(l=i.x*i.x/(m*m)+i.y*i.y/(d*d))&&(m*=l=Math.sqrt(l),d*=l),u=m*m*d*d,h=m*m*i.y*i.y+d*d*i.x*i.x,O(C={x:m*i.y/d,y:-d*i.x/m},s!=r?Math.sqrt((u-h)/h)||0:-Math.sqrt((u-h)/h)||0),n=Math.atan2((i.y-C.y)/d,(i.x-C.x)/m),a=Math.atan2(-(i.y+C.y)/d,-(i.x+C.x)/m),L(C,c),A=C,z=(o.x+j.x)/2,D=(o.y+j.y)/2,A.x+=z,A.y+=D,e.save(),e.translate(C.x,C.y),e.rotate(c),e.scale(m,d),e.arc(0,0,1,n,a,!s),e.restore();break;case"C":T=E[3],P=E[4],f=E[5],p=E[6],e.bezierCurveTo(E[1],E[2],T,P,f,p);break;case"c":e.bezierCurveTo(E[1]+f,E[2]+p,E[3]+f,E[4]+p,E[5]+f,E[6]+p),T=E[3]+f,P=E[4]+p,f+=E[5],p+=E[6];break;case"S":null!==T&&null!==T||(T=f,P=p),e.bezierCurveTo(2*f-T,2*p-P,E[1],E[2],E[3],E[4]),T=E[1],P=E[2],f=E[3],p=E[4];break;case"s":null!==T&&null!==T||(T=f,P=p),e.bezierCurveTo(2*f-T,2*p-P,E[1]+f,E[2]+p,E[3]+f,E[4]+p),T=E[1]+f,P=E[2]+p,f+=E[3],p+=E[4];break;case"Q":M=E[1],w=E[2],f=E[3],p=E[4],e.quadraticCurveTo(M,w,f,p);break;case"q":M=E[1]+f,w=E[2]+p,f+=E[3],p+=E[4],e.quadraticCurveTo(M,w,f,p);break;case"T":null!==M&&null!==M||(M=f,w=p),M=2*f-M,w=2*p-w,f=E[1],p=E[2],e.quadraticCurveTo(M,w,f,p);break;case"t":null!==M&&null!==M||(M=f,w=p),M=2*f-M,w=2*p-w,f+=E[1],p+=E[2],e.quadraticCurveTo(M,w,f,p);break;case"z":case"Z":f=R.x,p=R.y,R=void 0,e.closePath();break;case"AC":f=E[1],p=E[2],g=E[3],n=E[4],a=E[5],q=E[6],e.arc(f,p,g,n,a,q);break;case"AT":y=E[1],v=E[2],f=E[3],p=E[4],g=E[5],e.arcTo(y,v,f,p,g);break;case"E":f=E[1],p=E[2],m=E[3],d=E[4],c=E[5],n=E[6],a=E[7],q=E[8],e.save(),e.translate(f,p),e.rotate(c),e.scale(m,d),e.arc(0,0,1,n,a,q),e.restore();break;case"R":f=E[1],p=E[2],b=E[3],x=E[4],R={x:f,y:p},e.rect(f,p,b,x)}j.x=f,j.y=p}}};"undefined"!=typeof window&&t(window);var r={path2dPolyfill:t,parsePath:n},s=r.path2dPolyfill,u=r.parsePath;e.default=r,e.parsePath=u,e.path2dPolyfill=s,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).path2dPolyfill={})}(this,(function(e){"use strict";var t={a:7,c:6,h:1,l:2,m:2,q:4,s:4,t:2,v:1,z:0},n=/([astvzqmhlc])([^astvzqmhlc]*)/gi,a=/-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi;var r=function(e){var r=[],s=String(e).trim();return"M"!==s[0]&&"m"!==s[0]||s.replace(n,(function(e,n,s){var o=n.toLowerCase(),i=function(e){var t=e.match(a);return t?t.map(Number):[]}(s),l=n;if("m"===o&&i.length>2&&(r.push([l].concat(i.splice(0,2))),o="l",l="m"===l?"l":"L"),i.length<t[o])return"";for(r.push([l].concat(i.splice(0,t[o])));i.length>=t[o]&&i.length&&t[o];)r.push([l].concat(i.splice(0,t[o])));return""})),r};function s(e,t){for(var n=0;n<t.length;n++){var a=t[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}function o(e){return function(e){if(Array.isArray(e))return i(e)}(e)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(e)||function(e,t){if(!e)return;if("string"==typeof e)return i(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return i(e,t)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,a=new Array(t);n<t;n++)a[n]=e[n];return a}function l(e,t){var n=e.x*Math.cos(t)-e.y*Math.sin(t),a=e.y*Math.cos(t)+e.x*Math.sin(t);e.x=n,e.y=a}function c(e,t){e.x*=t,e.y*=t}var u=function(e){if(void 0!==e&&e.CanvasRenderingContext2D&&(!e.Path2D||!function(e){var t=e.document.createElement("canvas").getContext("2d"),n=new e.Path2D("M0 0 L1 1");return t.strokeStyle="red",t.lineWidth=1,t.stroke(n),255===t.getImageData(0,0,1,1).data[0]}(e))){var t=function(){function e(t){var n;(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.segments=[],t&&t instanceof e)?(n=this.segments).push.apply(n,o(t.segments)):t&&(this.segments=r(t))}var t,n,a;return t=e,(n=[{key:"addPath",value:function(t){var n;t&&t instanceof e&&(n=this.segments).push.apply(n,o(t.segments))}},{key:"moveTo",value:function(e,t){this.segments.push(["M",e,t])}},{key:"lineTo",value:function(e,t){this.segments.push(["L",e,t])}},{key:"arc",value:function(e,t,n,a,r,s){this.segments.push(["AC",e,t,n,a,r,!!s])}},{key:"arcTo",value:function(e,t,n,a,r){this.segments.push(["AT",e,t,n,a,r])}},{key:"ellipse",value:function(e,t,n,a,r,s,o,i){this.segments.push(["E",e,t,n,a,r,s,o,!!i])}},{key:"closePath",value:function(){this.segments.push(["Z"])}},{key:"bezierCurveTo",value:function(e,t,n,a,r,s){this.segments.push(["C",e,t,n,a,r,s])}},{key:"quadraticCurveTo",value:function(e,t,n,a){this.segments.push(["Q",e,t,n,a])}},{key:"rect",value:function(e,t,n,a){this.segments.push(["R",e,t,n,a])}}])&&s(t.prototype,n),a&&s(t,a),e}(),n=e.CanvasRenderingContext2D.prototype.fill,a=e.CanvasRenderingContext2D.prototype.stroke;e.CanvasRenderingContext2D.prototype.fill=function(){for(var e=arguments.length,t=new Array(e),a=0;a<e;a++)t[a]=arguments[a];var r="nonzero";if(0===t.length||1===t.length&&"string"==typeof t[0])n.apply(this,t);else{2===arguments.length&&(r=t[1]);var s=t[0];i(this,s.segments),n.call(this,r)}},e.CanvasRenderingContext2D.prototype.stroke=function(e){e?(i(this,e.segments),a.call(this)):a.call(this)},e.Path2D=t}function i(e,t){var n,a,r,s,o,i,u,h,f,y,p,v,m,d,g,b,x,k,C,T,M,P,w,q,A,z,D,S,j,R={x:0,y:0},E={x:0,y:0};e.beginPath();for(var I=0;I<t.length;++I){var L=t[I];switch("S"!==(T=L[0])&&"s"!==T&&"C"!==T&&"c"!==T&&(P=null,w=null),"T"!==T&&"t"!==T&&"Q"!==T&&"q"!==T&&(q=null,A=null),T){case"m":case"M":"m"===T?(p+=L[1],m+=L[2]):(p=L[1],m=L[2]),"M"!==T&&R||(R={x:p,y:m}),e.moveTo(p,m);break;case"l":p+=L[1],m+=L[2],e.lineTo(p,m);break;case"L":p=L[1],m=L[2],e.lineTo(p,m);break;case"H":p=L[1],e.lineTo(p,m);break;case"h":p+=L[1],e.lineTo(p,m);break;case"V":m=L[1],e.lineTo(p,m);break;case"v":m+=L[1],e.lineTo(p,m);break;case"a":case"A":"a"===T?(p+=L[6],m+=L[7]):(p=L[6],m=L[7]),b=L[1],x=L[2],u=L[3]*Math.PI/180,r=!!L[4],s=!!L[5],o={x:p,y:m},l(i={x:(E.x-o.x)/2,y:(E.y-o.y)/2},-u),(h=i.x*i.x/(b*b)+i.y*i.y/(x*x))>1&&(b*=h=Math.sqrt(h),x*=h),f=b*b*x*x,y=b*b*i.y*i.y+x*x*i.x*i.x,c(M={x:b*i.y/x,y:-x*i.x/b},s!==r?Math.sqrt((f-y)/y)||0:-Math.sqrt((f-y)/y)||0),a=Math.atan2((i.y-M.y)/x,(i.x-M.x)/b),n=Math.atan2(-(i.y+M.y)/x,-(i.x+M.x)/b),l(M,u),D=M,S=(o.x+E.x)/2,j=(o.y+E.y)/2,D.x+=S,D.y+=j,e.save(),e.translate(M.x,M.y),e.rotate(u),e.scale(b,x),e.arc(0,0,1,a,n,!s),e.restore();break;case"C":P=L[3],w=L[4],p=L[5],m=L[6],e.bezierCurveTo(L[1],L[2],P,w,p,m);break;case"c":e.bezierCurveTo(L[1]+p,L[2]+m,L[3]+p,L[4]+m,L[5]+p,L[6]+m),P=L[3]+p,w=L[4]+m,p+=L[5],m+=L[6];break;case"S":null!==P&&null!==P||(P=p,w=m),e.bezierCurveTo(2*p-P,2*m-w,L[1],L[2],L[3],L[4]),P=L[1],w=L[2],p=L[3],m=L[4];break;case"s":null!==P&&null!==P||(P=p,w=m),e.bezierCurveTo(2*p-P,2*m-w,L[1]+p,L[2]+m,L[3]+p,L[4]+m),P=L[1]+p,w=L[2]+m,p+=L[3],m+=L[4];break;case"Q":q=L[1],A=L[2],p=L[3],m=L[4],e.quadraticCurveTo(q,A,p,m);break;case"q":q=L[1]+p,A=L[2]+m,p+=L[3],m+=L[4],e.quadraticCurveTo(q,A,p,m);break;case"T":null!==q&&null!==q||(q=p,A=m),q=2*p-q,A=2*m-A,p=L[1],m=L[2],e.quadraticCurveTo(q,A,p,m);break;case"t":null!==q&&null!==q||(q=p,A=m),q=2*p-q,A=2*m-A,p+=L[1],m+=L[2],e.quadraticCurveTo(q,A,p,m);break;case"z":case"Z":p=R.x,m=R.y,R=void 0,e.closePath();break;case"AC":p=L[1],m=L[2],g=L[3],a=L[4],n=L[5],z=L[6],e.arc(p,m,g,a,n,z);break;case"AT":v=L[1],d=L[2],p=L[3],m=L[4],g=L[5],e.arcTo(v,d,p,m,g);break;case"E":p=L[1],m=L[2],b=L[3],x=L[4],u=L[5],a=L[6],n=L[7],z=L[8],e.save(),e.translate(p,m),e.rotate(u),e.scale(b,x),e.arc(0,0,1,a,n,z),e.restore();break;case"R":p=L[1],m=L[2],k=L[3],C=L[4],R={x:p,y:m},e.rect(p,m,k,C)}E.x=p,E.y=m}}};"undefined"!=typeof window&&u(window);var h={path2dPolyfill:u,parsePath:r},f=h.path2dPolyfill,y=h.parsePath;e.default=h,e.parsePath=y,e.path2dPolyfill=f,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "path2d-polyfill", | ||
"version": "0.4.2", | ||
"version": "1.0.0", | ||
"description": "Polyfills Path2D api for canvas rendering", | ||
@@ -35,14 +35,18 @@ "scripts": { | ||
"@babel/preset-env": "^7.5.5", | ||
"@rollup/plugin-babel": "^5.0.2", | ||
"@rollup/plugin-commonjs": "^12.0.0", | ||
"chai": "^4.2.0", | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.2.0", | ||
"nyc": "^14.1.1", | ||
"rollup": "^1.20.2", | ||
"rollup-plugin-babel": "^4.3.3", | ||
"rollup-plugin-commonjs": "^9.3.4", | ||
"rollup-plugin-live-server": "^1.0.3", | ||
"rollup-plugin-uglify": "^6.0.2", | ||
"sinon": "^7.4.1", | ||
"eslint": "^7.1.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-prettier": "^3.1.3", | ||
"mocha": "^7.2.0", | ||
"nyc": "^15.0.1", | ||
"prettier": "2.0.5", | ||
"rollup": "^2.10.9", | ||
"rollup-plugin-livereload": "^1.3.0", | ||
"rollup-plugin-serve": "^1.0.1", | ||
"rollup-plugin-terser": "6.1.0", | ||
"sinon": "^9.0.2", | ||
"sinon-chai": "^3.3.0" | ||
} | ||
} |
@@ -10,2 +10,3 @@ # path2d-polyfill | ||
## Usage | ||
``` | ||
@@ -16,2 +17,3 @@ npm install --save path2d-polyfill | ||
include directly in html-page | ||
```html | ||
@@ -23,11 +25,18 @@ <html> | ||
<body> | ||
<script lang="javascript" src='node_modules/path2d-polyfill/path2d-polyfill.js'></script> | ||
<script | ||
lang="javascript" | ||
src="node_modules/path2d-polyfill/path2d-polyfill.js" | ||
></script> | ||
</body> | ||
</html> | ||
``` | ||
or import with module bundler e.g. webpack *before* you try to use the feature | ||
or import with module bundler e.g. webpack _before_ you try to use the feature | ||
```javascript | ||
require('path2d-polyfill'); | ||
``` | ||
or if you use transpiler to support es2015+ modules | ||
```javascript | ||
@@ -38,16 +47,18 @@ import 'path2d-polyfill'; | ||
## Support table | ||
| Method | Supported | | ||
| ------------- |:-------------:| | ||
| constructor(SVGPath) | Yes | | ||
| addPath() | Yes | | ||
| closePath() | Yes | | ||
| moveTo() | Yes | | ||
| lineTo() | Yes | | ||
| bezierCurveTo() | Yes | | ||
| quadraticCurveTo() | Yes | | ||
| arc() | Yes | | ||
| ellipse() | Yes | | ||
| rect() | Yes | | ||
You can render SVG paths like this | ||
| Method | Supported | | ||
| -------------------- | :-------: | | ||
| constructor(SVGPath) | Yes | | ||
| addPath() | Yes | | ||
| closePath() | Yes | | ||
| moveTo() | Yes | | ||
| lineTo() | Yes | | ||
| bezierCurveTo() | Yes | | ||
| quadraticCurveTo() | Yes | | ||
| arc() | Yes | | ||
| ellipse() | Yes | | ||
| rect() | Yes | | ||
Example of usage | ||
```javascript | ||
@@ -58,10 +69,15 @@ ctx.fill(new Path2D('M 80 80 A 45 45 0 0 0 125 125 L 125 80 Z')); | ||
## See it in action | ||
Clone this repo and run the following | ||
``` | ||
npm install | ||
npm start | ||
yarn | ||
yarn start | ||
``` | ||
open http://localhost:8080 to see the example page. | ||
open http://localhost:10001 to see the example page. | ||
## Contributing | ||
Recommended to use vscode with the prettier extension and use "format on save" option |
@@ -14,5 +14,5 @@ const ARG_LENGTH = { | ||
const SEGMENT_PATTERN = /([astvzqmhlc])([^astvzqmhlc]*)/ig; | ||
const SEGMENT_PATTERN = /([astvzqmhlc])([^astvzqmhlc]*)/gi; | ||
const NUMBER = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/ig; | ||
const NUMBER = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi; | ||
@@ -66,3 +66,7 @@ function parseValues(args) { | ||
// "M 100 200 L 200 100 -100 -200" instead). | ||
while (theArgs.length >= ARG_LENGTH[type] && theArgs.length && ARG_LENGTH[type]) { | ||
while ( | ||
theArgs.length >= ARG_LENGTH[type] && | ||
theArgs.length && | ||
ARG_LENGTH[type] | ||
) { | ||
data.push([theCommand].concat(theArgs.splice(0, ARG_LENGTH[type]))); | ||
@@ -69,0 +73,0 @@ } |
@@ -19,4 +19,4 @@ const parsePath = require('./parse-path'); | ||
function rotatePoint(point, angle) { | ||
const nx = (point.x * Math.cos(angle)) - (point.y * Math.sin(angle)); | ||
const ny = (point.y * Math.cos(angle)) + (point.x * Math.sin(angle)); | ||
const nx = point.x * Math.cos(angle) - point.y * Math.sin(angle); | ||
const ny = point.y * Math.cos(angle) + point.x * Math.sin(angle); | ||
point.x = nx; | ||
@@ -45,7 +45,7 @@ point.y = ny; | ||
/** | ||
* Crates a Path2D polyfill object | ||
* @constructor | ||
* @ignore | ||
* @param {String} path | ||
*/ | ||
* Crates a Path2D polyfill object | ||
* @constructor | ||
* @ignore | ||
* @param {String} path | ||
*/ | ||
class Path2D { | ||
@@ -143,3 +143,8 @@ constructor(path) { | ||
// Reset control point if command is not cubic | ||
if (pathType !== 'S' && pathType !== 's' && pathType !== 'C' && pathType !== 'c') { | ||
if ( | ||
pathType !== 'S' && | ||
pathType !== 's' && | ||
pathType !== 'C' && | ||
pathType !== 'c' | ||
) { | ||
cpx = null; | ||
@@ -149,3 +154,8 @@ cpy = null; | ||
if (pathType !== 'T' && pathType !== 't' && pathType !== 'Q' && pathType !== 'q') { | ||
if ( | ||
pathType !== 'T' && | ||
pathType !== 't' && | ||
pathType !== 'Q' && | ||
pathType !== 'q' | ||
) { | ||
qcpx = null; | ||
@@ -224,4 +234,5 @@ qcpy = null; | ||
// radius correction | ||
lambda = ((midPoint.x * midPoint.x) / (rx * rx)) | ||
+ ((midPoint.y * midPoint.y) / (ry * ry)); | ||
lambda = | ||
(midPoint.x * midPoint.x) / (rx * rx) + | ||
(midPoint.y * midPoint.y) / (ry * ry); | ||
if (lambda > 1) { | ||
@@ -238,4 +249,5 @@ lambda = Math.sqrt(lambda); | ||
t1 = rx * rx * ry * ry; | ||
t2 = (rx * rx * midPoint.y * midPoint.y) | ||
+ (ry * ry * midPoint.x * midPoint.x); | ||
t2 = | ||
rx * rx * midPoint.y * midPoint.y + | ||
ry * ry * midPoint.x * midPoint.x; | ||
if (sweepFlag !== largeArcFlag) { | ||
@@ -249,6 +261,8 @@ scalePoint(centerPoint, Math.sqrt((t1 - t2) / t2) || 0); | ||
(midPoint.y - centerPoint.y) / ry, | ||
(midPoint.x - centerPoint.x) / rx); | ||
(midPoint.x - centerPoint.x) / rx | ||
); | ||
endAngle = Math.atan2( | ||
-(midPoint.y + centerPoint.y) / ry, | ||
-(midPoint.x + centerPoint.x) / rx); | ||
-(midPoint.x + centerPoint.x) / rx | ||
); | ||
@@ -259,3 +273,4 @@ rotatePoint(centerPoint, angle); | ||
(endPoint.x + currentPoint.x) / 2, | ||
(endPoint.y + currentPoint.y) / 2); | ||
(endPoint.y + currentPoint.y) / 2 | ||
); | ||
@@ -283,3 +298,4 @@ canvas.save(); | ||
s[5] + x, | ||
s[6] + y); | ||
s[6] + y | ||
); | ||
cpx = s[3] + x; // Last control point | ||
@@ -297,8 +313,9 @@ cpy = s[4] + y; | ||
canvas.bezierCurveTo( | ||
(2 * x) - cpx, | ||
(2 * y) - cpy, | ||
2 * x - cpx, | ||
2 * y - cpy, | ||
s[1], | ||
s[2], | ||
s[3], | ||
s[4]); | ||
s[4] | ||
); | ||
cpx = s[1]; // last control point | ||
@@ -316,8 +333,9 @@ cpy = s[2]; | ||
canvas.bezierCurveTo( | ||
(2 * x) - cpx, | ||
(2 * y) - cpy, | ||
2 * x - cpx, | ||
2 * y - cpy, | ||
s[1] + x, | ||
s[2] + y, | ||
s[3] + x, | ||
s[4] + y); | ||
s[4] + y | ||
); | ||
cpx = s[1] + x; // last control point | ||
@@ -347,4 +365,4 @@ cpy = s[2] + y; | ||
} | ||
qcpx = (2 * x) - qcpx; // last control point | ||
qcpy = (2 * y) - qcpy; | ||
qcpx = 2 * x - qcpx; // last control point | ||
qcpy = 2 * y - qcpy; | ||
x = s[1]; | ||
@@ -359,4 +377,4 @@ y = s[2]; | ||
} | ||
qcpx = (2 * x) - qcpx; // last control point | ||
qcpy = (2 * y) - qcpy; | ||
qcpx = 2 * x - qcpx; // last control point | ||
qcpy = 2 * y - qcpy; | ||
x += s[1]; | ||
@@ -415,3 +433,3 @@ y += s[2]; | ||
default: | ||
// throw new Error(`${pathType} is not implemented`); ? | ||
// throw new Error(`${pathType} is not implemented`); ? | ||
} | ||
@@ -426,3 +444,6 @@ | ||
let fillRule = 'nonzero'; | ||
if (args.length === 0 || (args.length === 1 && typeof args[0] === 'string')) { | ||
if ( | ||
args.length === 0 || | ||
(args.length === 1 && typeof args[0] === 'string') | ||
) { | ||
cFill.apply(this, args); | ||
@@ -429,0 +450,0 @@ return; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
94270
1046
0
79
17