path2d-polyfill
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -0,1 +1,3 @@ | ||
## [1.2.3](https://github.com/nilzona/path2d-polyfill/compare/v1.2.2...v1.2.3) (2022-10-11) | ||
## [1.2.2](https://github.com/nilzona/path2d-polyfill/compare/v1.2.1...v1.2.2) (2022-07-23) | ||
@@ -2,0 +4,0 @@ |
@@ -15,3 +15,2 @@ var ARG_LENGTH = { | ||
var NUMBER = /-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/gi; | ||
function parseValues(args) { | ||
@@ -21,2 +20,3 @@ var numbers = args.match(NUMBER); | ||
} | ||
/** | ||
@@ -33,17 +33,15 @@ * parse an svg path data string. Generates an Array | ||
*/ | ||
function parse(path) { | ||
var data = []; | ||
var p = String(path).trim(); // A path data segment (if there is one) must begin with a "moveto" command | ||
var p = String(path).trim(); | ||
// A path data segment (if there is one) must begin with a "moveto" command | ||
if (p[0] !== "M" && p[0] !== "m") { | ||
return data; | ||
} | ||
p.replace(SEGMENT_PATTERN, function (_, command, args) { | ||
var type = command.toLowerCase(); | ||
var theArgs = parseValues(args); | ||
var theCommand = command; // overloaded moveTo | ||
var theCommand = command; | ||
// overloaded moveTo | ||
if (type === "m" && theArgs.length > 2) { | ||
@@ -53,18 +51,17 @@ data.push([theCommand].concat(theArgs.splice(0, 2))); | ||
theCommand = theCommand === "m" ? "l" : "L"; | ||
} // Ignore invalid commands | ||
} | ||
// Ignore invalid commands | ||
if (theArgs.length < ARG_LENGTH[type]) { | ||
return ""; | ||
} | ||
data.push([theCommand].concat(theArgs.splice(0, ARG_LENGTH[type]))); | ||
data.push([theCommand].concat(theArgs.splice(0, ARG_LENGTH[type]))); // The command letter can be eliminated on subsequent commands if the | ||
// The command letter can be eliminated on subsequent commands if the | ||
// same command is used multiple times in a row (e.g., you can drop the | ||
// second "L" in "M 100 200 L 200 100 L -100 -200" and use | ||
// "M 100 200 L 200 100 -100 -200" instead). | ||
while (theArgs.length >= ARG_LENGTH[type] && theArgs.length && ARG_LENGTH[type]) { | ||
data.push([theCommand].concat(theArgs.splice(0, ARG_LENGTH[type]))); | ||
} | ||
return ""; | ||
@@ -74,3 +71,2 @@ }); | ||
} | ||
var parsePath$2 = parse; | ||
@@ -83,3 +79,2 @@ | ||
} | ||
function _defineProperties(target, props) { | ||
@@ -94,3 +89,2 @@ for (var i = 0; i < props.length; i++) { | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
@@ -104,15 +98,11 @@ if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
} | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) return _arrayLikeToArray(arr); | ||
} | ||
function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
@@ -126,11 +116,7 @@ if (!o) return; | ||
} | ||
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() { | ||
@@ -141,2 +127,3 @@ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
var parsePath$1 = parsePath$2; | ||
/** | ||
@@ -146,3 +133,2 @@ * Work around for https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8438884/ | ||
*/ | ||
function supportsSvgPathArgument(window) { | ||
@@ -165,3 +151,2 @@ var canvas = window.document.createElement("canvas"); | ||
} | ||
function translatePoint(point, dx, dy) { | ||
@@ -171,3 +156,2 @@ point.x += dx; | ||
} | ||
function scalePoint(point, s) { | ||
@@ -177,3 +161,2 @@ point.x *= s; | ||
} | ||
function polyFillPath2D(window) { | ||
@@ -183,6 +166,6 @@ if (typeof window === "undefined" || !window.CanvasRenderingContext2D) { | ||
} | ||
if (window.Path2D && supportsSvgPathArgument(window)) { | ||
return; | ||
} | ||
/** | ||
@@ -194,13 +177,8 @@ * Crates a Path2D polyfill object | ||
*/ | ||
var Path2D = /*#__PURE__*/function () { | ||
function Path2D(path) { | ||
_classCallCheck(this, Path2D); | ||
this.segments = []; | ||
if (path && path instanceof Path2D) { | ||
var _this$segments; | ||
(_this$segments = this.segments).push.apply(_this$segments, _toConsumableArray(path.segments)); | ||
@@ -211,3 +189,2 @@ } else if (path) { | ||
} | ||
_createClass(Path2D, [{ | ||
@@ -218,3 +195,2 @@ key: "addPath", | ||
var _this$segments2; | ||
(_this$segments2 = this.segments).push.apply(_this$segments2, _toConsumableArray(path.segments)); | ||
@@ -269,6 +245,4 @@ } | ||
}]); | ||
return Path2D; | ||
}(); | ||
function buildPath(canvas, segments) { | ||
@@ -310,7 +284,7 @@ var endAngle; | ||
canvas.beginPath(); | ||
for (var i = 0; i < segments.length; ++i) { | ||
var s = segments[i]; | ||
pathType = s[0]; // Reset control point if command is not cubic | ||
pathType = s[0]; | ||
// Reset control point if command is not cubic | ||
if (pathType !== "S" && pathType !== "s" && pathType !== "C" && pathType !== "c") { | ||
@@ -320,3 +294,2 @@ cpx = null; | ||
} | ||
if (pathType !== "T" && pathType !== "t" && pathType !== "Q" && pathType !== "q") { | ||
@@ -326,3 +299,2 @@ qcpx = null; | ||
} | ||
switch (pathType) { | ||
@@ -338,3 +310,2 @@ case "m": | ||
} | ||
if (pathType === "M" || !startPoint) { | ||
@@ -346,6 +317,4 @@ startPoint = { | ||
} | ||
canvas.moveTo(x, y); | ||
break; | ||
case "l": | ||
@@ -356,3 +325,2 @@ x += s[1]; | ||
break; | ||
case "L": | ||
@@ -363,3 +331,2 @@ x = s[1]; | ||
break; | ||
case "H": | ||
@@ -369,3 +336,2 @@ x = s[1]; | ||
break; | ||
case "h": | ||
@@ -375,3 +341,2 @@ x += s[1]; | ||
break; | ||
case "V": | ||
@@ -381,3 +346,2 @@ y = s[1]; | ||
break; | ||
case "v": | ||
@@ -387,3 +351,2 @@ y += s[1]; | ||
break; | ||
case "a": | ||
@@ -398,7 +361,4 @@ case "A": | ||
} | ||
rx = s[1]; // rx | ||
ry = s[2]; // ry | ||
angle = s[3] * Math.PI / 180; | ||
@@ -410,4 +370,6 @@ largeArcFlag = !!s[4]; | ||
y: y | ||
}; // https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes | ||
}; | ||
// https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes | ||
midPoint = { | ||
@@ -417,6 +379,6 @@ x: (currentPoint.x - endPoint.x) / 2, | ||
}; | ||
rotatePoint(midPoint, -angle); // radius correction | ||
rotatePoint(midPoint, -angle); | ||
// radius correction | ||
lambda = midPoint.x * midPoint.x / (rx * rx) + midPoint.y * midPoint.y / (ry * ry); | ||
if (lambda > 1) { | ||
@@ -427,3 +389,2 @@ lambda = Math.sqrt(lambda); | ||
} | ||
centerPoint = { | ||
@@ -435,3 +396,2 @@ x: rx * midPoint.y / ry, | ||
t2 = rx * rx * midPoint.y * midPoint.y + ry * ry * midPoint.x * midPoint.x; | ||
if (sweepFlag !== largeArcFlag) { | ||
@@ -442,3 +402,2 @@ scalePoint(centerPoint, Math.sqrt((t1 - t2) / t2) || 0); | ||
} | ||
startAngle = Math.atan2((midPoint.y - centerPoint.y) / ry, (midPoint.x - centerPoint.x) / rx); | ||
@@ -455,6 +414,4 @@ endAngle = Math.atan2(-(midPoint.y + centerPoint.y) / ry, -(midPoint.x + centerPoint.x) / rx); | ||
break; | ||
case "C": | ||
cpx = s[3]; // Last control point | ||
cpy = s[4]; | ||
@@ -465,7 +422,5 @@ x = s[5]; | ||
break; | ||
case "c": | ||
canvas.bezierCurveTo(s[1] + x, s[2] + y, s[3] + x, s[4] + y, s[5] + x, s[6] + y); | ||
cpx = s[3] + x; // Last control point | ||
cpy = s[4] + y; | ||
@@ -475,3 +430,2 @@ x += s[5]; | ||
break; | ||
case "S": | ||
@@ -482,6 +436,4 @@ if (cpx === null || cpy === null) { | ||
} | ||
canvas.bezierCurveTo(2 * x - cpx, 2 * y - cpy, s[1], s[2], s[3], s[4]); | ||
cpx = s[1]; // last control point | ||
cpy = s[2]; | ||
@@ -491,3 +443,2 @@ x = s[3]; | ||
break; | ||
case "s": | ||
@@ -498,6 +449,4 @@ if (cpx === null || cpy === null) { | ||
} | ||
canvas.bezierCurveTo(2 * x - cpx, 2 * y - cpy, s[1] + x, s[2] + y, s[3] + x, s[4] + y); | ||
cpx = s[1] + x; // last control point | ||
cpy = s[2] + y; | ||
@@ -507,6 +456,4 @@ x += s[3]; | ||
break; | ||
case "Q": | ||
qcpx = s[1]; // last control point | ||
qcpy = s[2]; | ||
@@ -517,6 +464,4 @@ x = s[3]; | ||
break; | ||
case "q": | ||
qcpx = s[1] + x; // last control point | ||
qcpy = s[2] + y; | ||
@@ -527,3 +472,2 @@ x += s[3]; | ||
break; | ||
case "T": | ||
@@ -534,5 +478,3 @@ if (qcpx === null || qcpy === null) { | ||
} | ||
qcpx = 2 * x - qcpx; // last control point | ||
qcpy = 2 * y - qcpy; | ||
@@ -543,3 +485,2 @@ x = s[1]; | ||
break; | ||
case "t": | ||
@@ -550,5 +491,3 @@ if (qcpx === null || qcpy === null) { | ||
} | ||
qcpx = 2 * x - qcpx; // last control point | ||
qcpy = 2 * y - qcpy; | ||
@@ -559,3 +498,2 @@ x += s[1]; | ||
break; | ||
case "z": | ||
@@ -568,3 +506,2 @@ case "Z": | ||
break; | ||
case "AC": | ||
@@ -580,3 +517,2 @@ // arc | ||
break; | ||
case "AT": | ||
@@ -591,3 +527,2 @@ // arcTo | ||
break; | ||
case "E": | ||
@@ -610,3 +545,2 @@ // ellipse | ||
break; | ||
case "R": | ||
@@ -624,3 +558,3 @@ // rect | ||
break; | ||
// throw new Error(`${pathType} is not implemented`); ? | ||
} | ||
@@ -632,6 +566,4 @@ | ||
} | ||
var cFill = window.CanvasRenderingContext2D.prototype.fill; | ||
var cStroke = window.CanvasRenderingContext2D.prototype.stroke; | ||
window.CanvasRenderingContext2D.prototype.fill = function fill() { | ||
@@ -641,5 +573,3 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
} | ||
var fillRule = "nonzero"; | ||
if (args.length === 0 || args.length === 1 && typeof args[0] === "string") { | ||
@@ -649,7 +579,5 @@ cFill.apply(this, args); | ||
} | ||
if (arguments.length === 2) { | ||
fillRule = args[1]; | ||
} | ||
var path = args[0]; | ||
@@ -659,3 +587,2 @@ buildPath(this, path.segments); | ||
}; | ||
window.CanvasRenderingContext2D.prototype.stroke = function stroke(path) { | ||
@@ -666,9 +593,6 @@ if (!path) { | ||
} | ||
buildPath(this, path.segments); | ||
cStroke.call(this); | ||
}; | ||
var cIsPointInPath = window.CanvasRenderingContext2D.prototype.isPointInPath; | ||
window.CanvasRenderingContext2D.prototype.isPointInPath = function isPointInPath() { | ||
@@ -678,3 +602,2 @@ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
} | ||
// let fillRule = 'nonzero'; | ||
@@ -693,6 +616,4 @@ if (args[0].constructor.name === "Path2D") { | ||
}; | ||
window.Path2D = Path2D; | ||
} | ||
var path2dPolyfill$1 = polyFillPath2D; | ||
@@ -702,7 +623,5 @@ | ||
var path2dPolyfill = path2dPolyfill$1; | ||
if (typeof window !== "undefined") { | ||
path2dPolyfill(window); | ||
} | ||
var src = { | ||
@@ -709,0 +628,0 @@ path2dPolyfill: path2dPolyfill, |
{ | ||
"name": "path2d-polyfill", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "Polyfills Path2D api for canvas rendering", | ||
@@ -37,11 +37,11 @@ "scripts": { | ||
"devDependencies": { | ||
"@babel/core": "^7.18.9", | ||
"@babel/preset-env": "^7.18.9", | ||
"@commitlint/cli": "^17.0.3", | ||
"@commitlint/config-conventional": "^17.0.3", | ||
"@release-it/conventional-changelog": "^5.0.0", | ||
"@rollup/plugin-babel": "^5.3.1", | ||
"@rollup/plugin-commonjs": "^22.0.1", | ||
"@babel/core": "^7.19.3", | ||
"@babel/preset-env": "^7.19.4", | ||
"@commitlint/cli": "^17.1.2", | ||
"@commitlint/config-conventional": "^17.1.0", | ||
"@release-it/conventional-changelog": "^5.1.1", | ||
"@rollup/plugin-babel": "^6.0.0", | ||
"@rollup/plugin-commonjs": "^23.0.0", | ||
"chai": "^4.3.6", | ||
"eslint": "^8.20.0", | ||
"eslint": "^8.25.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
@@ -52,10 +52,10 @@ "eslint-plugin-prettier": "^4.2.1", | ||
"mocha": "^10.0.0", | ||
"nyc": "^15.0.1", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.7.1", | ||
"release-it": "^15.1.3", | ||
"rollup": "^2.77.0", | ||
"release-it": "^15.5.0", | ||
"rollup": "^2.79.1", | ||
"rollup-plugin-livereload": "^2.0.5", | ||
"rollup-plugin-serve": "^2.0.0", | ||
"rollup-plugin-serve": "^2.0.1", | ||
"rollup-plugin-terser": "7.0.2", | ||
"sinon": "^14.0.0", | ||
"sinon": "^14.0.1", | ||
"sinon-chai": "^3.7.0" | ||
@@ -62,0 +62,0 @@ }, |
@@ -11,3 +11,3 @@ # path2d-polyfill | ||
Load from cdn | ||
Add this script tag to your page to enable the feature. | ||
@@ -24,3 +24,3 @@ ```html | ||
and import with module bundler e.g. webpack _before_ you try to use the feature | ||
and import with module bundler e.g. webpack _before_ using the feature | ||
@@ -72,2 +72,2 @@ ```javascript | ||
Recommended to use vscode with the prettier extension and use "format on save" option | ||
Recommended to use vscode with the prettier extension to keep formatting intact. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1039
105761