Jump to content
Search Community

random value is a string

OSUblake test
Moderator Tag

Recommended Posts

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

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)

  • Like 3
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...