Skip to content
Open
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
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
]
}
],
"import/no-named-as-default-member": 0
"@typescript-eslint/no-non-null-assertion": 0
},
"env": {
"jest": true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"react-native-reanimated": "2.0.0-rc.0",
"semantic-release": "^15.13.3",
"semantic-release-cli": "^4.1.2",
"typescript": "4.0.3"
"typescript": "4.1.3"
},
"react-native": "lib/module/index.js",
"module": "lib/module/index.js",
Expand Down
21 changes: 0 additions & 21 deletions src/Array.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/Math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,8 @@ export const cubicBezierYForX = (
const t = solveCubic(pa, pb, pc, pd)
.map((root) => round(root, precision))
.filter((root) => root >= 0 && root <= 1)[0];
if (t === undefined) {
throw new Error("y not found for x=" + x);
}
return cubicBezier(t, a.y, b.y, c.y, d.y);
};
55 changes: 27 additions & 28 deletions src/Paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ export const parse = (d: string): Path => {
* @summary Interpolate between paths.
* @worklet
*/
export const interpolatePath = (
export const interpolatePath = <
T extends readonly [number, number, ...number[]]
>(
value: number,
inputRange: number[],
outputRange: Path[],
inputRange: T,
outputRange: { [K in keyof T]: Path },
extrapolate = Animated.Extrapolate.CLAMP
) => {
"worklet";
Expand All @@ -100,41 +102,41 @@ export const interpolatePath = (
x: interpolate(
value,
inputRange,
outputRange.map((p) => p.curves[index].c1.x),
outputRange.map((p) => p.curves[index]!.c1.x),
extrapolate
),
y: interpolate(
value,
inputRange,
outputRange.map((p) => p.curves[index].c1.y),
outputRange.map((p) => p.curves[index]!.c1.y),
extrapolate
),
},
c2: {
x: interpolate(
value,
inputRange,
outputRange.map((p) => p.curves[index].c2.x),
outputRange.map((p) => p.curves[index]!.c2.x),
extrapolate
),
y: interpolate(
value,
inputRange,
outputRange.map((p) => p.curves[index].c2.y),
outputRange.map((p) => p.curves[index]!.c2.y),
extrapolate
),
},
to: {
x: interpolate(
value,
inputRange,
outputRange.map((p) => p.curves[index].to.x),
outputRange.map((p) => p.curves[index]!.to.x),
extrapolate
),
y: interpolate(
value,
inputRange,
outputRange.map((p) => p.curves[index].to.y),
outputRange.map((p) => p.curves[index]!.to.y),
extrapolate
),
},
Expand Down Expand Up @@ -279,8 +281,7 @@ export const selectCurve = (path: Path, x: number): SelectedCurve => {
from: path.move,
curve: null,
};
for (let i = 0; i < path.curves.length; i++) {
const c = path.curves[i];
for (const c of path.curves) {
const contains =
result.from.x > c.to.x
? x >= c.to.x && x <= result.from.x
Expand Down Expand Up @@ -324,8 +325,8 @@ export const getYForX = (path: Path, x: number, precision = 2) => {

const controlPoint = (
current: Vector,
previous: Vector,
next: Vector,
previous: Vector | undefined,
next: Vector | undefined,
reverse: boolean,
smoothing: number
) => {
Expand Down Expand Up @@ -355,22 +356,21 @@ const exhaustiveCheck = (a: never): never => {
* from https://github.com/rainbow-me/rainbow
*/
export const curveLines = (
points: Vector<number>[],
points: readonly [Vector<number>, ...Vector<number>[]],
smoothing: number,
strategy: "complex" | "bezier" | "simple"
) => {
"worklet";
const path = createPath(points[0]);
// build the d attributes by looping over the points
for (let i = 0; i < points.length; i++) {
if (i === 0) {
continue;
}
const point = points[i];
points.forEach((point, i) => {
const next = points[i + 1];
const prev = points[i - 1];
const cps = controlPoint(prev, points[i - 2], point, false, smoothing);
const cpe = controlPoint(point, prev, next, true, smoothing);
const p1 = points[i - 1];
if (p1 === undefined) {
return;
}
const cps = controlPoint(p1, points[i - 2], point, false, smoothing);
const cpe = controlPoint(point, p1, next, true, smoothing);
switch (strategy) {
case "simple":
const cp = {
Expand All @@ -380,8 +380,7 @@ export const curveLines = (
addQuadraticCurve(path, cp, point);
break;
case "bezier":
const p0 = points[i - 2] || prev;
const p1 = points[i - 1];
const p0 = points[i - 2] || p1;
const cp1x = (2 * p0.x + p1.x) / 3;
const cp1y = (2 * p0.y + p1.y) / 3;
const cp2x = (p0.x + 2 * p1.x) / 3;
Expand All @@ -395,9 +394,9 @@ export const curveLines = (
});
if (i === points.length - 1) {
path.curves.push({
to: points[points.length - 1],
c1: points[points.length - 1],
c2: points[points.length - 1],
to: point,
c1: point,
c2: point,
});
}
break;
Expand All @@ -411,6 +410,6 @@ export const curveLines = (
default:
exhaustiveCheck(strategy);
}
}
});
return path;
};
6 changes: 3 additions & 3 deletions src/Physics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
export const snapPoint = (
value: number,
velocity: number,
points: ReadonlyArray<number>
): number => {
points: readonly number[]
) => {
"worklet";
const point = value + 0.2 * velocity;
const deltas = points.map((p) => Math.abs(point - p));
const minDelta = Math.min.apply(null, deltas);
return points.filter((p) => Math.abs(point - p) === minDelta)[0];
return points.filter((p) => Math.abs(point - p) === minDelta)[0]!;
};
6 changes: 0 additions & 6 deletions src/__tests__/Array.test.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/__tests__/Paths.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { serialize, parse, getYForX, curveLines } from "../Paths";
import { Vector } from "../Vectors";

import { d1, d2 } from "./paths";

// Graph line with random points
const points: Vector[] = [
const points = [
{ x: 0, y: 192 },
{ x: 16.189944134078214, y: 192 },
{ x: 32.37988826815643, y: 192 },
Expand Down Expand Up @@ -32,7 +31,7 @@ const points: Vector[] = [
{ x: 388.5586592178771, y: 192 },
{ x: 404.7486033519553, y: 192 },
{ x: 414, y: 192 },
];
] as const;

test("parse()", () => {
const path =
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ import "./Coordinates.test";
import "./Math.test";
import "./Physics.test";
import "./Paths.test";
import "./Array.test";
import "./Matrix4.test";
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export * from "./Math";
export * from "./Vectors";
export * from "./Paths";
export * from "./Physics";
export * from "./Array";
export * from "./Matrix4";
export * from "./Colors";
export { default as ReText } from "./ReText";
2 changes: 1 addition & 1 deletion src/v1/Hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const useDiff = (node: Animated.Node<number>) => {

export const useDebug = (values: { [key: string]: Animated.Node<number> }) => {
const keys = Object.keys(values);
useCode(() => block(keys.map((name) => debug(name, values[name]))), [
useCode(() => block(keys.map((name) => debug(name, values[name]!))), [
keys,
values,
]);
Expand Down
4 changes: 2 additions & 2 deletions src/v1/SVG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const parsePath = (d: string): ReanimatedPath => {
absSVG(parseSVG(d))
);
const parts: BezierCubicCurve[] = curves.map((curve, index) => {
const prevCurve = curves[index - 1];
const prevCurve = curves[index - 1]!;
const p0 =
index === 0
? { x: move[MX], y: move[MY] }
Expand Down Expand Up @@ -171,7 +171,7 @@ export const interpolatePath = (
typeof path === "string" ? parsePath(path) : path
);
const [path] = paths;
const commands = path.segments.map((_, index) => {
const commands = path!.segments.map((_, index) => {
const interpolatePoint = (point: BezierPoint) =>
interpolateNode(value, {
inputRange,
Expand Down
6 changes: 3 additions & 3 deletions src/v1/String.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export const string = (
...values: readonly Concatable[]
) => {
if (values.length === 0) {
return concat(strings[0]);
return concat(strings[0]!);
}
const result = values.reduce<Concatable[]>(
(acc, v, idx) => [...acc, strings[idx], v],
(acc, v, idx) => [...acc, strings[idx]!, v],
[]
);
result.push(strings[strings.length - 1]);
result.push(strings[strings.length - 1]!);
return concat(...result);
};
10 changes: 5 additions & 5 deletions src/v1/bezier/CubicBezierLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ const cValues = [
const binomialCoefficients = [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]];

// Look up what the binomial coefficient is for pair {n,k}
const binomials = (n: number, k: number) => binomialCoefficients[n][k];
const binomials = (n: number, k: number) => binomialCoefficients[n]![k]!;

/**
* Compute the curve derivative (hodograph) at t.
Expand All @@ -727,15 +727,15 @@ const getDerivative = (derivative: number, t: number, vs: number[]): number => {
if (derivative === 0) {
value = 0;
for (k = 0; k <= n; k += 1) {
value += binomials(n, k) * (1 - t ** n - k) * t ** k * vs[k];
value += binomials(n, k) * (1 - t ** n - k) * t ** k * vs[k]!;
}
return value;
}
// Still some derivative? go down one order, then try
// for the lower order curve's.
const vs1 = new Array(n);
for (k = 0; k < n; k += 1) {
vs1[k] = n * (vs[k + 1] - vs[k]);
vs1[k] = n * (vs[k + 1]! - vs[k]!);
}
return getDerivative(derivative - 1, t, vs1);
};
Expand All @@ -755,8 +755,8 @@ const getArcLength = (xs: CtrlPoint, ys: CtrlPoint, t = 1, n = 20) => {
let sum = 0;
let i;
for (i = 0; i < n; i += 1) {
const correctedT = z * tValues[n][i] + z;
sum += cValues[n][i] * B(xs, ys, correctedT);
const correctedT = z * tValues[n]![i]! + z;
sum += cValues[n]![i]! * B(xs, ys, correctedT);
}
return z * sum;
};
Expand Down
34 changes: 21 additions & 13 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
"target": "es2017",
"lib": ["es6"],
"jsx": "react-native",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"strict": true,
"noEmit": true /* Do not emit outputs. */,
/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
"noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
/* Module Resolution Options */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"resolveJsonModule": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* Enable strict null checks. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
"strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
"types": ["react", "react-native", "jest"],
"skipLibCheck": true
},
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10756,10 +10756,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.3.tgz#153bbd468ef07725c1df9c77e8b453f8d36abba5"
integrity sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==
typescript@4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==

typescript@^3.9.7:
version "3.9.7"
Expand Down