Skip to content

Commit f238d6a

Browse files
authoredDec 27, 2023
feat(convertPathData): allow converting q to t in more cases (#1889)
1 parent 967d2f1 commit f238d6a

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed
 

‎plugins/convertPathData.js

+45-7
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,13 @@ function filters(
397397
relSubpoint = [0, 0],
398398
pathBase = [0, 0],
399399
prev = {};
400+
/** @type {Point | undefined} */
401+
let qControlPoint;
400402

401403
path = path.filter(function (item, index, path) {
404+
const qPoint = qControlPoint;
405+
qControlPoint = undefined;
406+
402407
let command = item.command;
403408
let data = item.args;
404409
let next = path[index + 1];
@@ -795,14 +800,24 @@ function filters(
795800
// t + q → t + t
796801
else if (
797802
// @ts-ignore
798-
prev.command === 't' &&
799-
// @ts-ignore
800-
Math.abs(data[2] - prev.args[0]) < error &&
801-
// @ts-ignore
802-
Math.abs(data[3] - prev.args[1]) < error
803+
prev.command === 't'
803804
) {
804-
command = 't';
805-
data = data.slice(2);
805+
// @ts-ignore
806+
const predictedControlPoint = reflectPoint(qPoint, item.base);
807+
const realControlPoint = [
808+
// @ts-ignore
809+
data[0] + item.base[0],
810+
// @ts-ignore
811+
data[1] + item.base[1],
812+
];
813+
if (
814+
Math.abs(predictedControlPoint[0] - realControlPoint[0]) <
815+
error &&
816+
Math.abs(predictedControlPoint[1] - realControlPoint[1]) < error
817+
) {
818+
command = 't';
819+
data = data.slice(2);
820+
}
806821
}
807822
}
808823
}
@@ -873,6 +888,18 @@ function filters(
873888
)
874889
return false;
875890

891+
if (command === 'q') {
892+
// @ts-ignore
893+
qControlPoint = [data[0] + item.base[0], data[1] + item.base[1]];
894+
} else if (command === 't') {
895+
if (qPoint) {
896+
// @ts-ignore
897+
qControlPoint = reflectPoint(qPoint, item.base);
898+
} else {
899+
// @ts-ignore
900+
qControlPoint = item.coords;
901+
}
902+
}
876903
prev = item;
877904
return true;
878905
});
@@ -1138,6 +1165,17 @@ function getDistance(point1, point2) {
11381165
return Math.hypot(point1[0] - point2[0], point1[1] - point2[1]);
11391166
}
11401167

1168+
/**
1169+
* Reflects point across another point
1170+
*
1171+
* @param {Point} input
1172+
* @param {Point} base
1173+
* @returns {Point}
1174+
*/
1175+
function reflectPoint(input, base) {
1176+
return [2 * base[0] - input[0], 2 * base[1] - input[1]];
1177+
}
1178+
11411179
/**
11421180
* Returns coordinates of the curve point corresponding to the certain t
11431181
* a·(1 - t)³·p1 + b·(1 - t)²·t·p2 + c·(1 - t)·t²·p3 + d·t³·p4,

‎test/plugins/convertPathData.34.svg

+16
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.