Skip to content

Instantly share code, notes, and snippets.

@mfazekas
Created March 6, 2020 19:18
Show Gist options
  • Save mfazekas/94a43a1b5fce23cb2d1c7b4f248d94b3 to your computer and use it in GitHub Desktop.
Save mfazekas/94a43a1b5fce23cb2d1c7b4f248d94b3 to your computer and use it in GitHub Desktop.
Example CoordinatesArray subclass
class RouteCoordinatesArray extends Animated.CoordinatesArray {
newCoordAnimator(config, origCoords) {
const {along} = config;
return {
dist: along[0],
distconf: along[1],
calculate(progress, coords) {
const currentDist = progress * this.dist;
let prevsum = 0;
let actsum = 0;
let i = origCoords.length - 1;
while (actsum < currentDist && i > 0) {
prevsum = actsum;
actsum += distance(
point(coords[i]),
point(coords[i - 1]),
this.distconf,
);
i -= 1;
}
if (actsum <= currentDist) {
return [coords[0]];
}
const r = (currentDist - prevsum) / (actsum - prevsum);
const or = 1.0 - r;
return [
...coords.slice(0, i + 1),
[
coords[i][0] * r + coords[i + 1][0] * or,
coords[i][1] * r + coords[i + 1][1] * or,
],
];
},
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment