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

XYChart: AnimatedAnnotation animates incorrectly #1766

Open
Kongkille opened this issue Nov 1, 2023 · 0 comments
Open

XYChart: AnimatedAnnotation animates incorrectly #1766

Kongkille opened this issue Nov 1, 2023 · 0 comments

Comments

@Kongkille
Copy link

Kongkille commented Nov 1, 2023

Description

Ran into an issue where if I change my chart data using the XYChart, the AnimatedAnnotation seems to animate incorrectly.

Here's a video from the chart I'm working on, as you can see the annotation immediately jumps to the x position and animates to the y position

Screen.Recording.2023-11-01.at.09.38.47.mov

(slowed down version using a copy of the code)

Screen.Recording.2023-11-01.at.09.40.04.mov

Potential fix

I'm still not quite sure what's causing this to happen. However, to get this to work on my own project, I made my own annotation with a few changes related to these lines:

const animatedXY = useSpring({
from: { x: lastXY.current.x - x, y: lastXY.current.y - y },
to: { x: 0, y: 0 },
reset: true,
});
useEffect(() => {
lastXY.current = { x, y };
}, [x, y]);

	const animatedXY = useSpring({
		from: {
			x: lastXY.current.x - x,
			y: lastXY.current.y - y,
		},
		to: { x: 0, y: 0 },
		immediate: lastXY.current.x === undefined || lastXY.current.y === undefined, // Ensures it starts at the correct position
		reset: true,
		onRest: () => {
			// Update the lastXY ref so that the next animation starts from the correct position.
			lastXY.current = { x, y };
			setIsAnimating(false);
		},
		onStart: () => {
			setIsAnimating(true);
		},
	});

	React.useEffect(() => {
		if (isAnimating) {
			// If we're already animating, update the lastXY ref so that the next animation
			// starts from the correct position.
			lastXY.current = { x, y };
		}
	}, [x, y, isAnimating]);
  1. In the effect updating the reference, we only update the ref if it's currently animating
  2. Use onRest to update the reference. Essentially waiting for the animation to finish before updating the last position.

I am not sure if that's the best fix though, I have yet to try to produce a minimum example but if relevant, I'm happy to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant