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

Animation fails with duration:0 #27

Closed
pixelzoom opened this issue Jan 8, 2020 · 3 comments
Closed

Animation fails with duration:0 #27

pixelzoom opened this issue Jan 8, 2020 · 3 comments
Assignees
Labels

Comments

@pixelzoom
Copy link
Contributor

pixelzoom commented Jan 8, 2020

If an Animation is created and run with duration: 0, it fails with NaN due to a divide-by-zero error.

This was the cause of phetsims/unit-rates#215. In Unit Rates, there's an animation that moves a marker along the number line, based on the number that's entered into a keypad.

Here's the Animation definition in DoubleNumberLineAccordionBox:

markerEditorAnimation = new Animation( {
  duration: 0.002 * Math.abs( destinationX - markerEditorNode.x ), // 2ms per 1 unit of distance
  easing: Easing.QUADRATIC_IN_OUT,
  object: markerEditorNode,
  attribute: 'x',
  to: destinationX
} );

If a value that is larger than the number line's range is entered into the keypad, then the marker moves to the far right end of the number line. If you enter 2 such large values in a row, then the distance that the marker needs to move is zero, and the computed value of Animation option duration will also be zero.

For example, here's the marker located at "333", and I'm about to enter "334". This requires no movement of the marker, so duration is 0.

screenshot_52

When you try to run an Animation with duration: 0, Animation step will have a divide-by-zero at line 312, where this.length is zero and ratio is therefore NaN.

312 const ratio = Utils.clamp( ( this.length - this.remainingAnimation ) / this.length, 0, 1 );

This causes failure further along in the code. In the case of Unit Rates, it failed down in Easing.polynomialEaseInOutValue, where an assertion noticed that the ratio was invalid.

In Unit Rates, I'm simply not going to create an Animation if duration is zero. But this should be handled in Animation by either:

(a) an immediate assertion failure if options.duration is zero, or
(b) the animation is a no-op and completes immediately when it's started.

I don't know which option is better, but option (b) would have prevented having to add logic to Unit Rates.

@jonathanolson
Copy link
Contributor

I'll lean towards (b) unless it seems unworkable in the code. Thanks for spotting!

@jonathanolson
Copy link
Contributor

Implemented (b) above.

@pixelzoom
Copy link
Contributor Author

I tested this by removing the workaround in phetsims/unit-rates#215. Seems to work great. Thanks, closing.

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

No branches or pull requests

2 participants