Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(convertPathData): allow converting q to t in more cases #1889

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,13 @@ function filters(
relSubpoint = [0, 0],
pathBase = [0, 0],
prev = {};
/** @type {Point | undefined} */
let qControlPoint;

path = path.filter(function (item, index, path) {
const qPoint = qControlPoint;
qControlPoint = undefined;

let command = item.command;
let data = item.args;
let next = path[index + 1];
Expand Down Expand Up @@ -795,14 +800,24 @@ function filters(
// t + q → t + t
else if (
// @ts-ignore
prev.command === 't' &&
// @ts-ignore
Math.abs(data[2] - prev.args[0]) < error &&
// @ts-ignore
Math.abs(data[3] - prev.args[1]) < error
prev.command === 't'
) {
command = 't';
data = data.slice(2);
// @ts-ignore
const predictedControlPoint = reflectPoint(qPoint, item.base);
const realControlPoint = [
// @ts-ignore
data[0] + item.base[0],
// @ts-ignore
data[1] + item.base[1],
];
if (
Math.abs(predictedControlPoint[0] - realControlPoint[0]) <
error &&
Math.abs(predictedControlPoint[1] - realControlPoint[1]) < error
) {
command = 't';
data = data.slice(2);
}
}
}
}
Expand Down Expand Up @@ -873,6 +888,18 @@ function filters(
)
return false;

if (command === 'q') {
// @ts-ignore
qControlPoint = [data[0] + item.base[0], data[1] + item.base[1]];
} else if (command === 't') {
if (qPoint) {
// @ts-ignore
qControlPoint = reflectPoint(qPoint, item.base);
} else {
// @ts-ignore
qControlPoint = item.coords;
}
}
prev = item;
return true;
});
Expand Down Expand Up @@ -1138,6 +1165,17 @@ function getDistance(point1, point2) {
return Math.hypot(point1[0] - point2[0], point1[1] - point2[1]);
}

/**
* Reflects point across another point
*
* @param {Point} input
* @param {Point} base
* @returns {Point}
*/
function reflectPoint(input, base) {
return [2 * base[0] - input[0], 2 * base[1] - input[1]];
}

/**
* Returns coordinates of the curve point corresponding to the certain t
* a·(1 - t)³·p1 + b·(1 - t)²·t·p2 + c·(1 - t)·t²·p3 + d·t³·p4,
Expand Down
16 changes: 16 additions & 0 deletions test/plugins/convertPathData.34.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.