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

Fix getStroke when there are multiple duplicates #18

Merged
merged 3 commits into from
Jun 28, 2021
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
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { min, PI } = Math
export function getStrokePoints<
T extends number[],
K extends { x: number; y: number; pressure?: number }
>(points: (T | K)[], options: StrokeOptions): StrokePoint[] {
>(points: (T | K)[], options = {} as StrokeOptions): StrokePoint[] {
let { simulatePressure = true, streamline = 0.5, size = 8 } = options

streamline /= 2
Expand All @@ -25,7 +25,7 @@ export function getStrokePoints<

const pts = toPointsArray(points)

const len = pts.length
let len = pts.length

if (len === 0) return []

Expand All @@ -42,9 +42,9 @@ export function getStrokePoints<
]

for (
let i = 1, curr = pts[i], prev = strokePoints[0];
i < pts.length;
i++, curr = pts[i], prev = strokePoints[i - 1]
let i = 1, j = 0, curr = pts[i], prev = strokePoints[j];
i < len;
i++, curr = pts[i], prev = strokePoints[j]
) {
const point = vec.lrp(prev.point, curr, 1 - streamline)

Expand All @@ -62,6 +62,8 @@ export function getStrokePoints<
distance,
runningLength,
})

j += 1 // only increment j if we add an item to strokePoints
}

/*
Expand All @@ -74,6 +76,9 @@ export function getStrokePoints<
end cap.
*/

// Update the length to the length of the strokePoints array.
len = strokePoints.length

const totalLength = strokePoints[len - 1].runningLength

for (let i = len - 2; i > 1; i--) {
Expand Down
333 changes: 333 additions & 0 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`The algorithm. Removes any duplicates: stroke with duplicates removed 1`] = `
Array [
Array [
26.505973339645458,
23.492805957229542,
],
Array [
83.13097991979497,
80.30255279504878,
],
Array [
96.84340515172856,
94.01497802698238,
],
Array [
101.3922385219373,
98.56381139719112,
],
Array [
101.95340164075448,
99.66515602948375,
],
Array [
101.76003800794093,
100.8860059590433,
],
Array [
100.8860059590433,
101.76003800794093,
],
Array [
99.66515602948375,
101.95340164075448,
],
Array [
80.6439401914923,
83.06723118113189,
],
Array [
4.834412997599668,
7.662840122345858,
],
Array [
4.2732498787824875,
5.935757629892302,
],
Array [
5.34064556049367,
4.4666135115960275,
],
Array [
7.156607559451857,
4.466613511596028,
],
Array [
8.224003241163038,
5.935757629892301,
],
Array [
7.662840122345858,
7.662840122345858,
],
Array [
5.935757629892302,
8.22400324116304,
],
Array [
4.466613511596028,
7.156607559451857,
],
Array [
4.466613511596027,
5.340645560493671,
],
Array [
5.935757629892301,
4.273249878782488,
],
Array [
7.662840122345855,
4.8344129975996655,
],
Array [
83.06723118113189,
80.6439401914923,
],
Array [
101.95340164075448,
99.66515602948375,
],
Array [
101.76003800794093,
100.8860059590433,
],
Array [
100.8860059590433,
101.76003800794093,
],
Array [
99.66515602948375,
101.95340164075448,
],
Array [
98.56381139719112,
101.3922385219373,
],
Array [
94.01497802698238,
96.84340515172856,
],
Array [
80.30255279504878,
83.13097991979497,
],
Array [
23.492805957229542,
26.505973339645458,
],
Array [
-1.5065836912079575,
1.5065836912079575,
],
Array [
-2.1043994846717706,
0.333304134598981,
],
Array [
-1.8984062008987304,
-0.9672862728359364,
],
Array [
-0.9672862728359362,
-1.8984062008987306,
],
Array [
0.33330413459898056,
-2.1043994846717706,
],
Array [
1.5065836912079573,
-1.5065836912079578,
],
]
`;

exports[`The algorithm. Removes any duplicates: stroke-points with duplicates removed 1`] = `
Array [
Object {
"distance": 0,
"point": Array [
0,
0,
],
"pressure": 0.5,
"runningLength": 0,
"vector": Array [
0,
0,
],
},
Object {
"distance": 10.606601717798213,
"point": Array [
7.5,
7.5,
],
"pressure": 0.5,
"runningLength": 10.606601717798213,
"vector": Array [
-0.7071067811865475,
-0.7071067811865475,
],
},
Object {
"distance": 2.6516504294495533,
"point": Array [
9.375,
9.375,
],
"pressure": 0.5,
"runningLength": 13.258252147247767,
"vector": Array [
-0.7071067811865475,
-0.7071067811865475,
],
},
Object {
"distance": 0.6629126073623883,
"point": Array [
9.84375,
9.84375,
],
"pressure": 0.5,
"runningLength": 13.921164754610155,
"vector": Array [
-0.7071067811865475,
-0.7071067811865475,
],
},
Object {
"distance": 0.16572815184059708,
"point": Array [
9.9609375,
9.9609375,
],
"pressure": 0.5,
"runningLength": 14.086892906450752,
"vector": Array [
-0.7071067811865475,
-0.7071067811865475,
],
},
Object {
"distance": 0.04143203796014927,
"point": Array [
9.990234375,
9.990234375,
],
"pressure": 0.5,
"runningLength": 14.1283249444109,
"vector": Array [
-0.7071067811865475,
-0.7071067811865475,
],
},
Object {
"distance": 95.46977346967395,
"point": Array [
77.49755859375,
77.49755859375,
],
"pressure": 0.5,
"runningLength": 109.59809841408486,
"vector": Array [
-0.7071067811865476,
-0.7071067811865476,
],
},
Object {
"distance": 23.86744336741849,
"point": Array [
94.3743896484375,
94.3743896484375,
],
"pressure": 0.5,
"runningLength": 133.46554178150336,
"vector": Array [
-0.7071067811865476,
-0.7071067811865476,
],
},
Object {
"distance": 5.966860841854622,
"point": Array [
98.59359741210938,
98.59359741210938,
],
"pressure": 0.5,
"runningLength": 139.43240262335797,
"vector": Array [
-0.7071067811865476,
-0.7071067811865476,
],
},
Object {
"distance": 1.4917152104636555,
"point": Array [
99.64839935302734,
99.64839935302734,
],
"pressure": 0.5,
"runningLength": 140.92411783382164,
"vector": Array [
-0.7071067811865476,
-0.7071067811865476,
],
},
Object {
"distance": 0.3729288026159139,
"point": Array [
99.91209983825684,
99.91209983825684,
],
"pressure": 0.5,
"runningLength": 141.29704663643756,
"vector": Array [
-0.7071067811865476,
-0.7071067811865476,
],
},
Object {
"distance": 0.09323220065397847,
"point": Array [
99.97802495956421,
99.97802495956421,
],
"pressure": 0.5,
"runningLength": 141.39027883709153,
"vector": Array [
-0.7071067811865476,
-0.7071067811865476,
],
},
Object {
"distance": 106.04270912781864,
"point": Array [
24.994506239891052,
24.994506239891052,
],
"pressure": 0.5,
"runningLength": 247.43298796491018,
"vector": Array [
0.7071067811865475,
0.7071067811865475,
],
},
Object {
"distance": 26.51067728195466,
"point": Array [
6.248626559972763,
6.248626559972763,
],
"pressure": 0.5,
"runningLength": 273.94366524686484,
"vector": Array [
0.7071067811865475,
0.7071067811865475,
],
},
]
`;
Loading