Skip to content

Commit b60d22b

Browse files
KTibowSethFalco
authored andcommitted
address SethFalco review
1 parent 842d5bf commit b60d22b

File tree

5 files changed

+21
-30
lines changed

5 files changed

+21
-30
lines changed

docs/03-plugins/convert-path-data.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ svgo:
3131
description: Number of decimal places to round to, using conventional rounding rules.
3232
default: 5
3333
smartArcRounding:
34-
description: Round the radius of circular arcs more when the effective change (measured with the sagitta) is under the error.
34+
description: Round the radius of circular arcs when the effective change is under the error. The effective change is determined using the <a href="https://wikipedia.org/wiki/Sagitta_(geometry)" target="_blank">sagitta</a> of the arc.
3535
default: true
3636
removeUseless:
3737
description: Remove redundant path commands that don't draw anything.

plugins/convertPathData.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,6 @@ function filters(
407407
var sdata = data,
408408
circle;
409409

410-
const sagitta = command === 'a' ? calculateSagitta(data) : undefined;
411-
412410
if (command === 's') {
413411
sdata = [0, 0].concat(data);
414412

@@ -645,16 +643,12 @@ function filters(
645643

646644
// round arc radius more accurately
647645
// eg m 0 0 a 1234.567 1234.567 0 0 1 10 0 -> m 0 0 a 1235 1235 0 0 1 10 0
648-
if (
649-
params.smartArcRounding &&
650-
command === 'a' &&
651-
sagitta !== undefined &&
652-
precision
653-
) {
646+
const sagitta = command === 'a' ? calculateSagitta(data) : undefined;
647+
if (params.smartArcRounding && sagitta !== undefined && precision) {
654648
for (let precisionNew = precision; precisionNew >= 0; precisionNew--) {
655649
const radius = toFixed(data[0], precisionNew);
656650
const sagittaNew = /** @type {number} */ (
657-
calculateSagitta([radius, radius].concat(data.slice(2)))
651+
calculateSagitta([radius, radius, ...data.slice(2)])
658652
);
659653
if (Math.abs(sagitta - sagittaNew) < error) {
660654
data[0] = radius;
@@ -1100,15 +1094,16 @@ function isCurveStraightLine(data) {
11001094
* Calculates the sagitta of an arc if possible.
11011095
*
11021096
* @type {(data: number[]) => number | undefined}
1097+
* @see https://wikipedia.org/wiki/Sagitta_(geometry)#Formulas
11031098
*/
11041099
function calculateSagitta(data) {
1105-
const rx = data[0],
1106-
ry = data[1],
1107-
distance = Math.sqrt(Math.pow(data[5], 2) + Math.pow(data[6], 2));
1100+
if (data[3] === 1) return undefined;
1101+
1102+
const [rx, ry] = data;
11081103
if (Math.abs(rx - ry) > error) return undefined;
1109-
if (distance / 2 > rx) return undefined;
1110-
if (data[3] == 1) return undefined;
1111-
return rx - Math.sqrt(Math.pow(rx, 2) - 0.25 * Math.pow(distance, 2));
1104+
const chord = Math.sqrt(data[5] ** 2 + data[6] ** 2);
1105+
if (chord > rx * 2) return undefined;
1106+
return rx - Math.sqrt(rx ** 2 - 0.25 * chord ** 2);
11121107
}
11131108

11141109
/**

test/plugins/convertPathData.14.svg

Lines changed: 2 additions & 6 deletions
Loading

test/plugins/convertPathData.32.svg

Lines changed: 4 additions & 4 deletions
Loading

test/plugins/convertPathData.33.svg

Lines changed: 4 additions & 4 deletions
Loading

0 commit comments

Comments
 (0)