From c0c67df9699315223ac90732b8870dfbe61339d8 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sat, 14 Aug 2021 20:10:49 +0200 Subject: [PATCH 1/7] draft of the function --- src/util/path.js | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/util/path.js b/src/util/path.js index b6479352b4c..cb90e263038 100644 --- a/src/util/path.js +++ b/src/util/path.js @@ -780,6 +780,36 @@ path.push(['L', p1.x + multSignX * correction, p1.y + multSignY * correction]); return path; } + /** + * Transform a path by transforming each segment. + * it has to be a simplified path or it won't work. + * WARNING: this depends from pathOffset for correct operation + * @param {Array} path fabricJS parsed path commands + * @param {Array} transform matrix that represent the transformation + * @param {Object} [pathOffset] the fabric.Path pathOffset + * @param {Number} pathOffset.x + * @param {Number} pathOffset.y + * @returns {Array} the transformed path + */ + function transformPath(path, transform, pathOffset) { + if (pathOffset) { + transform = fabric.util.multiplyTransformMatrices( + transform, + [1, 0, 0, 1, -pathOffset.x, -pathOffset.y] + ); + } + return path.path.map(function(pathSegment) { + var newSegment = pathSegment.slice(0), point = {}; + for (var i = 1; i < pathSegment.length - 1; i += 2) { + point.x = pathSegment[i]; + point.y = pathSegment[i + 1]; + point = fabric.util.transformPoint(point, transform); + newSegment[i] = point.x; + newSegment[i + 1] = point.y; + } + return newSegment; + }); + } /** * Calculate bounding box of a elliptic-arc @@ -829,15 +859,15 @@ fabric.util.makePathSimpler = makePathSimpler; fabric.util.getSmoothPathFromPoints = getSmoothPathFromPoints; fabric.util.getPathSegmentsInfo = getPathSegmentsInfo; - fabric.util.fromArcToBeziers = fromArcToBeziers; + fabric.util.getBoundsOfCurve = getBoundsOfCurve; + fabric.util.getPointOnPath = getPointOnPath; + fabric.util.transformPath = transformPath; /** * Typo of `fromArcToBeziers` kept for not breaking the api once corrected. * Will be removed in fabric 5.0 * @deprecated */ fabric.util.fromArcToBeizers = fromArcToBeziers; - fabric.util.getBoundsOfCurve = getBoundsOfCurve; - fabric.util.getPointOnPath = getPointOnPath; // kept because we do not want to make breaking changes. // but useless and deprecated. fabric.util.getBoundsOfArc = getBoundsOfArc; From 1c88a308d59fc3b0db026303ab55bb3a3dba5090 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Fri, 27 Aug 2021 12:06:35 +0200 Subject: [PATCH 2/7] yes? --- src/util/path.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/path.js b/src/util/path.js index cb90e263038..4694e6bbd9a 100644 --- a/src/util/path.js +++ b/src/util/path.js @@ -798,7 +798,7 @@ [1, 0, 0, 1, -pathOffset.x, -pathOffset.y] ); } - return path.path.map(function(pathSegment) { + return path.map(function(pathSegment) { var newSegment = pathSegment.slice(0), point = {}; for (var i = 1; i < pathSegment.length - 1; i += 2) { point.x = pathSegment[i]; From 679b204792f8d65adf19aa02a2e5dfa5d231f0ca Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Fri, 27 Aug 2021 22:54:41 +0200 Subject: [PATCH 3/7] ok --- src/brushes/pencil_brush.class.js | 2 +- src/shapes/path.class.js | 13 ++++--------- src/util/path.js | 13 ++++++++++++- test/unit/util.js | 9 +++++++++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/brushes/pencil_brush.class.js b/src/brushes/pencil_brush.class.js index 68c7c7f052a..59fa9c058e4 100644 --- a/src/brushes/pencil_brush.class.js +++ b/src/brushes/pencil_brush.class.js @@ -192,7 +192,7 @@ * @returns {boolean} */ _isEmptySVGPath: function (pathData) { - var pathString = pathData.map(function (segment) { return segment.join(' '); }).join(' '); + var pathString = fabric.util.joinPath(pathData); return pathString === 'M 0 0 Q 0 0 0 0 L 0 0'; }, diff --git a/src/shapes/path.class.js b/src/shapes/path.class.js index ab379e5b3cf..24ea8bf63dc 100644 --- a/src/shapes/path.class.js +++ b/src/shapes/path.class.js @@ -56,12 +56,9 @@ var fromArray = _toString.call(path) === '[object Array]'; - this.path = fromArray - ? fabric.util.makePathSimpler(path) - - : fabric.util.makePathSimpler( - fabric.util.parsePath(path) - ); + this.path = fabric.util.makePathSimpler( + fromArray ? path : fabric.util.parsePath(path) + ); if (!this.path) { return; @@ -193,9 +190,7 @@ * of the instance */ _toSVG: function() { - var path = this.path.map(function(path) { - return path.join(' '); - }).join(' '); + var path = fabric.util.joinPath(this.path); return [ '