Share Posted June 22, 2020 When using "random()", the value ends up as a string. I would expect it's type to stay the same. This can cause problems with code that is expecting the value to be a number. See the Pen 58964daaa90343ed392c30ed9bffe9dc by osublake (@osublake) on CodePen Link to comment Share on other sites More sharing options...
Share Posted June 22, 2020 The string-based syntax you're using SHOULD result in it being a string because the random number substitution inside strings like that is specifically for strings. In other words, after the swap, it'd look like: gsap.to(obj, {x: "10.2347"}), for example. And GSAP does its best to honor what you're telling it to tween to, and in this case you're literally telling it that you want the end to be a string of "10.2347". See what I mean? If you want a number, just use gsap.utils.random(). gsap.set(obj, { x: gsap.utils.random(0, 100), }); Link to comment Share on other sites More sharing options...
Author Share Posted June 22, 2020 It's not a string on the first set. If it's supposed to be a string, then it should be documented as such, but I'm not sure I see where the string is even documented. Shouldn't it be here? https://greensock.com/docs/v3/GSAP/UtilityMethods/random() 1 Link to comment Share on other sites More sharing options...
Author Share Posted June 22, 2020 8 minutes ago, OSUblake said: It's not a string on the first set. See the Pen 8217fce92f4098e3a5e552fcdd281a26 by osublake (@osublake) on CodePen 2 1 Link to comment Share on other sites More sharing options...
Share Posted June 23, 2020 Ha - good catch. Such an odd edge case. It would only switch to a string if the start and end value had a decimal because of a check I was doing to sense if we could use the fast/simple numeric interpolation instead of complex string interpolation: if (!isNaN(start + end)) { ... } So if both had decimals and at least one of them was a string, it'd concatenate it as a string and end up with a value like "1.2345.147" which isNaN. Kinda funny. I just had to switch "+" to "*" to resolve that. It's fixed in the next release which you can preview at https://assets.codepen.io/16327/gsap-latest-beta.min.js And yeah, I misspoke - it will try to keep it as a number if possible rather than going to a string. As for the string-based "random(...)" in the docs, it's on all the most popular tween pages like .to(), .from(), and .fromTo() https://greensock.com/docs/v3/GSAP/gsap.to() (scroll down to the "random values" section) 3 Link to comment Share on other sites More sharing options...
Share Posted June 23, 2020 I added a note about the string form in the utility method docs to help people be aware of it. 2 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now