question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash when interpolating to a shorter path

See original GitHub issue

Hi !

When interpolating from a longer path to a shorter one I have the following crash:

    const longer = parse('M50,10 L100 100 L200 200 L500 500');
    const shorter = parse('M50,10 L200 200 L400 400');
    const pathAnimatedProps = useAnimatedProps(() => ({
        d: interpolatePath(transition.value, [0, 1], [longer, shorter])
    }));

When going to other way around (from shorter to longer) i have the same problem as in issue https://github.com/wcandillon/react-native-redash/issues/415

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

4reactions
jadhaidarcommented, May 1, 2021

Hello everyone. I was able to get it to work quite nicely using a pretty simple trick.

Note that my values are scaled uniformly along the x-axis using their index, and not their value. Your use case may differ.

So the idea is we need to interpolate between paths of varying lengths. Instead of re-implementing the interpolatePath function to support that use case - I chose to “prepare” the paths beforehand and simply provide them with equal lengths to interpolatePaths.

The big question was: how do you add or remove points to a path without changing the way it looks?

Easy. Re-scale the values along the x-axis using the same number of data points as our output path, and then for every x, get the corresponding y value from the original path.

Here’s the code snippet for the helper function I created, it returns a new path that has the same amount of data points as specified with the precision param:

// precision is a number corresponding to the number
// of data points we'd like to have in our new path
const normalize = (path, precision, width) => {
  const scaleX = scaleLinear().domain([0, precision - 1]).range([0, width]);

  // create an array of length precision
  const values = Array(precision)
    .fill([])
    .map((_, index) => {
      // re-scale the X value using the new index
      const x = scaleX(index);

      // and now compute the corresponding Y value on the original path
      // for the new x position 
      // using redash's `getYForX`
      const y = getYForX(path, x);
      return [x, y];
    });

  return parse(
    line()
      .x(([x]) => x)
      .y(([, y]) => y)
      .curve(curveLinear)(values)
  );
};

This solution works pretty well, but it is not perfect. The normalized path will lose some detail when removing data points (especially noticeable on points that have a high delta value compared to their surrounding neighbours, i.e. big spike on the path). It will do for now, but any input to help improve this would be much appreciated!

Now the next question is: how can we remove points in a less arbitrary way? i.e. preserve the points with the highest deltas, and ditch those with smaller ones?

1reaction
Stevemoretzcommented, Apr 22, 2021

It woule be great to have more support on interpolatePath function.

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bountysource
Crash when interpolating to a shorter path.
Read more >
INTERPOLATE does not work - InkscapeForum.com
I have a problem with EXTENSIONS/INTERPOLATE what did I do: - I start inkscape - circle tool, draw circle, object to path
Read more >
Keyframe interpolation instability - Blender Stack Exchange
For interpolation to take the shortest path one has to choose the quaternion closest one to the previous frame (either q or -q)....
Read more >
Bug #1018338 “Crash when interpolating two paths
Does this crash also occur if you redo the tutorial in a new document based on the default template? If the crash only...
Read more >
Fast-Paced Multiplayer (Part III): Entity Interpolation
In the second article, we proposed client-side prediction as a way to overcome these limitations. The net result of these two articles is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found