Skip to content

Commit

Permalink
fix(🐛): rounding issues with getYForX (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Oct 12, 2020
1 parent 602d176 commit bf93164
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/Math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ export const cubicBezierYForX = (
a: Vector,
b: Vector,
c: Vector,
d: Vector
d: Vector,
precision = 2
) => {
"worklet";
const pa = -a.x + 3 * b.x - 3 * c.x + d.x;
Expand All @@ -193,7 +194,7 @@ export const cubicBezierYForX = (
const pd = a.x - x;
// eslint-disable-next-line prefer-destructuring
const t = solveCubic(pa, pb, pc, pd)
.map((root) => round(root, 5))
.map((root) => round(root, precision))
.filter((root) => root >= 0 && root <= 1)[0];
return cubicBezier(t, a.y, b.y, c.y, d.y);
};
4 changes: 2 additions & 2 deletions src/Paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export const close = () => {
// ~151
getYForX(p1, 50)
*/
export const getYForX = (path: Path, x: number) => {
export const getYForX = (path: Path, x: number, precision = 2) => {
"worklet";
const p = path.filter((c) => {
if (isCurve(c)) {
Expand All @@ -298,7 +298,7 @@ export const getYForX = (path: Path, x: number) => {
return false;
});
if (p.length > 0 && isCurve(p[0])) {
return cubicBezierYForX(x, p[0].from, p[0].c1, p[0].c2, p[0].to);
return cubicBezierYForX(x, p[0].from, p[0].c1, p[0].c2, p[0].to, precision);
}
return 0;
};
Loading

0 comments on commit bf93164

Please sign in to comment.