path2d-polyfill
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -251,5 +251,2 @@ var ARG_LENGTH = { | ||
var cFill = window.CanvasRenderingContext2D.prototype.fill; | ||
var cStroke = window.CanvasRenderingContext2D.prototype.stroke; | ||
function buildPath(canvas, segments) { | ||
@@ -579,2 +576,5 @@ var endAngle; | ||
var cFill = window.CanvasRenderingContext2D.prototype.fill; | ||
var cStroke = window.CanvasRenderingContext2D.prototype.stroke; | ||
window.CanvasRenderingContext2D.prototype.fill = function fill() { | ||
@@ -611,2 +611,23 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
var cIsPointInPath = window.CanvasRenderingContext2D.prototype.isPointInPath; | ||
window.CanvasRenderingContext2D.prototype.isPointInPath = function isPointInPath() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
// let fillRule = 'nonzero'; | ||
if (args[0].constructor.name === 'Path2D') { | ||
// first argument is a Path2D object | ||
var x = args[1]; | ||
var y = args[2]; | ||
var fillRule = args[3] || 'nonzero'; | ||
var path = args[0]; | ||
buildPath(this, path.segments); | ||
return cIsPointInPath.apply(this, [x, y, fillRule]); | ||
} else { | ||
return cIsPointInPath.apply(this, args); | ||
} | ||
}; | ||
window.Path2D = Path2D; | ||
@@ -613,0 +634,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="undefined"!=typeof globalThis?globalThis: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,v,p,m,g,b,d,x,k,C,T,M,w,q,A,P,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&&(w=null,q=null),"T"!==T&&"t"!==T&&"Q"!==T&&"q"!==T&&(A=null,P=null),T){case"m":case"M":"m"===T?(v+=L[1],m+=L[2]):(v=L[1],m=L[2]),"M"!==T&&R||(R={x:v,y:m}),e.moveTo(v,m);break;case"l":v+=L[1],m+=L[2],e.lineTo(v,m);break;case"L":v=L[1],m=L[2],e.lineTo(v,m);break;case"H":v=L[1],e.lineTo(v,m);break;case"h":v+=L[1],e.lineTo(v,m);break;case"V":m=L[1],e.lineTo(v,m);break;case"v":m+=L[1],e.lineTo(v,m);break;case"a":case"A":"a"===T?(v+=L[6],m+=L[7]):(v=L[6],m=L[7]),d=L[1],x=L[2],u=L[3]*Math.PI/180,r=!!L[4],s=!!L[5],o={x:v,y:m},l(i={x:(E.x-o.x)/2,y:(E.y-o.y)/2},-u),(h=i.x*i.x/(d*d)+i.y*i.y/(x*x))>1&&(d*=h=Math.sqrt(h),x*=h),f=d*d*x*x,y=d*d*i.y*i.y+x*x*i.x*i.x,c(M={x:d*i.y/x,y:-x*i.x/d},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)/d),n=Math.atan2(-(i.y+M.y)/x,-(i.x+M.x)/d),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(d,x),e.arc(0,0,1,a,n,!s),e.restore();break;case"C":w=L[3],q=L[4],v=L[5],m=L[6],e.bezierCurveTo(L[1],L[2],w,q,v,m);break;case"c":e.bezierCurveTo(L[1]+v,L[2]+m,L[3]+v,L[4]+m,L[5]+v,L[6]+m),w=L[3]+v,q=L[4]+m,v+=L[5],m+=L[6];break;case"S":null!==w&&null!==w||(w=v,q=m),e.bezierCurveTo(2*v-w,2*m-q,L[1],L[2],L[3],L[4]),w=L[1],q=L[2],v=L[3],m=L[4];break;case"s":null!==w&&null!==w||(w=v,q=m),e.bezierCurveTo(2*v-w,2*m-q,L[1]+v,L[2]+m,L[3]+v,L[4]+m),w=L[1]+v,q=L[2]+m,v+=L[3],m+=L[4];break;case"Q":A=L[1],P=L[2],v=L[3],m=L[4],e.quadraticCurveTo(A,P,v,m);break;case"q":A=L[1]+v,P=L[2]+m,v+=L[3],m+=L[4],e.quadraticCurveTo(A,P,v,m);break;case"T":null!==A&&null!==A||(A=v,P=m),A=2*v-A,P=2*m-P,v=L[1],m=L[2],e.quadraticCurveTo(A,P,v,m);break;case"t":null!==A&&null!==A||(A=v,P=m),A=2*v-A,P=2*m-P,v+=L[1],m+=L[2],e.quadraticCurveTo(A,P,v,m);break;case"z":case"Z":v=R.x,m=R.y,R=void 0,e.closePath();break;case"AC":v=L[1],m=L[2],b=L[3],a=L[4],n=L[5],z=L[6],e.arc(v,m,b,a,n,z);break;case"AT":p=L[1],g=L[2],v=L[3],m=L[4],b=L[5],e.arcTo(p,g,v,m,b);break;case"E":v=L[1],m=L[2],d=L[3],x=L[4],u=L[5],a=L[6],n=L[7],z=L[8],e.save(),e.translate(v,m),e.rotate(u),e.scale(d,x),e.arc(0,0,1,a,n,z),e.restore();break;case"R":v=L[1],m=L[2],k=L[3],C=L[4],R={x:v,y:m},e.rect(v,m,k,C)}E.x=v,E.y=m}}};"undefined"!=typeof window&&u(window);var h={path2dPolyfill:u,parsePath:r};e.default=h,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="undefined"!=typeof globalThis?globalThis: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];u(this,s.segments),n.call(this,r)}},e.CanvasRenderingContext2D.prototype.stroke=function(e){e?(u(this,e.segments),a.call(this)):a.call(this)};var i=e.CanvasRenderingContext2D.prototype.isPointInPath;e.CanvasRenderingContext2D.prototype.isPointInPath=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];if("Path2D"===t[0].constructor.name){var a=t[1],r=t[2],s=t[3]||"nonzero",o=t[0];return u(this,o.segments),i.apply(this,[a,r,s])}return i.apply(this,t)},e.Path2D=t}function u(e,t){var n,a,r,s,o,i,u,h,f,y,p,v,g,m,d,b,x,k,C,T,P,M,w,A,q,D,z,R,S,j={x:0,y:0},I={x:0,y:0};e.beginPath();for(var E=0;E<t.length;++E){var L=t[E];switch("S"!==(T=L[0])&&"s"!==T&&"C"!==T&&"c"!==T&&(M=null,w=null),"T"!==T&&"t"!==T&&"Q"!==T&&"q"!==T&&(A=null,q=null),T){case"m":case"M":"m"===T?(p+=L[1],g+=L[2]):(p=L[1],g=L[2]),"M"!==T&&j||(j={x:p,y:g}),e.moveTo(p,g);break;case"l":p+=L[1],g+=L[2],e.lineTo(p,g);break;case"L":p=L[1],g=L[2],e.lineTo(p,g);break;case"H":p=L[1],e.lineTo(p,g);break;case"h":p+=L[1],e.lineTo(p,g);break;case"V":g=L[1],e.lineTo(p,g);break;case"v":g+=L[1],e.lineTo(p,g);break;case"a":case"A":"a"===T?(p+=L[6],g+=L[7]):(p=L[6],g=L[7]),b=L[1],x=L[2],u=L[3]*Math.PI/180,r=!!L[4],s=!!L[5],o={x:p,y:g},l(i={x:(I.x-o.x)/2,y:(I.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(P={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-P.y)/x,(i.x-P.x)/b),n=Math.atan2(-(i.y+P.y)/x,-(i.x+P.x)/b),l(P,u),z=P,R=(o.x+I.x)/2,S=(o.y+I.y)/2,z.x+=R,z.y+=S,e.save(),e.translate(P.x,P.y),e.rotate(u),e.scale(b,x),e.arc(0,0,1,a,n,!s),e.restore();break;case"C":M=L[3],w=L[4],p=L[5],g=L[6],e.bezierCurveTo(L[1],L[2],M,w,p,g);break;case"c":e.bezierCurveTo(L[1]+p,L[2]+g,L[3]+p,L[4]+g,L[5]+p,L[6]+g),M=L[3]+p,w=L[4]+g,p+=L[5],g+=L[6];break;case"S":null!==M&&null!==M||(M=p,w=g),e.bezierCurveTo(2*p-M,2*g-w,L[1],L[2],L[3],L[4]),M=L[1],w=L[2],p=L[3],g=L[4];break;case"s":null!==M&&null!==M||(M=p,w=g),e.bezierCurveTo(2*p-M,2*g-w,L[1]+p,L[2]+g,L[3]+p,L[4]+g),M=L[1]+p,w=L[2]+g,p+=L[3],g+=L[4];break;case"Q":A=L[1],q=L[2],p=L[3],g=L[4],e.quadraticCurveTo(A,q,p,g);break;case"q":A=L[1]+p,q=L[2]+g,p+=L[3],g+=L[4],e.quadraticCurveTo(A,q,p,g);break;case"T":null!==A&&null!==A||(A=p,q=g),A=2*p-A,q=2*g-q,p=L[1],g=L[2],e.quadraticCurveTo(A,q,p,g);break;case"t":null!==A&&null!==A||(A=p,q=g),A=2*p-A,q=2*g-q,p+=L[1],g+=L[2],e.quadraticCurveTo(A,q,p,g);break;case"z":case"Z":p=j.x,g=j.y,j=void 0,e.closePath();break;case"AC":p=L[1],g=L[2],d=L[3],a=L[4],n=L[5],D=L[6],e.arc(p,g,d,a,n,D);break;case"AT":v=L[1],m=L[2],p=L[3],g=L[4],d=L[5],e.arcTo(v,m,p,g,d);break;case"E":p=L[1],g=L[2],b=L[3],x=L[4],u=L[5],a=L[6],n=L[7],D=L[8],e.save(),e.translate(p,g),e.rotate(u),e.scale(b,x),e.arc(0,0,1,a,n,D),e.restore();break;case"R":p=L[1],g=L[2],k=L[3],C=L[4],j={x:p,y:g},e.rect(p,g,k,C)}I.x=p,I.y=g}}};"undefined"!=typeof window&&u(window);var h={path2dPolyfill:u,parsePath:r};e.default=h,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "path2d-polyfill", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Polyfills Path2D api for canvas rendering", | ||
@@ -5,0 +5,0 @@ "scripts": { |
# path2d-polyfill | ||
![Build, Test and maybe Publish](https://github.com/nilzona/path2d-polyfill/workflows/Build,%20Test%20and%20maybe%20Publish/badge.svg) | ||
![master](https://github.com/nilzona/path2d-polyfill/workflows/Build,%20Test%20and%20maybe%20Publish/badge.svg) | ||
@@ -5,0 +5,0 @@ Polyfills Path2D api for rendering SVG paths in canvas |
@@ -102,5 +102,2 @@ const parsePath = require('./parse-path'); | ||
const cFill = window.CanvasRenderingContext2D.prototype.fill; | ||
const cStroke = window.CanvasRenderingContext2D.prototype.stroke; | ||
function buildPath(canvas, segments) { | ||
@@ -429,2 +426,5 @@ let endAngle; | ||
const cFill = window.CanvasRenderingContext2D.prototype.fill; | ||
const cStroke = window.CanvasRenderingContext2D.prototype.stroke; | ||
window.CanvasRenderingContext2D.prototype.fill = function fill(...args) { | ||
@@ -456,2 +456,22 @@ let fillRule = 'nonzero'; | ||
const cIsPointInPath = | ||
window.CanvasRenderingContext2D.prototype.isPointInPath; | ||
window.CanvasRenderingContext2D.prototype.isPointInPath = function isPointInPath( | ||
...args | ||
) { | ||
// let fillRule = 'nonzero'; | ||
if (args[0].constructor.name === 'Path2D') { | ||
// first argument is a Path2D object | ||
const x = args[1]; | ||
const y = args[2]; | ||
const fillRule = args[3] || 'nonzero'; | ||
const path = args[0]; | ||
buildPath(this, path.segments); | ||
return cIsPointInPath.apply(this, [x, y, fillRule]); | ||
} else { | ||
return cIsPointInPath.apply(this, args); | ||
} | ||
}; | ||
window.Path2D = Path2D; | ||
@@ -458,0 +478,0 @@ } |
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
98054
1080