benoit958 Posted April 24 Share Posted April 24 Hello ! I'm facing a little issue using gsap.utils.random. I'm trying to randomize height and width of boxes on each repeat and appending "%" sign to use percentage. Here is what I noticied : i can't use AND append some string. Is this a bug or am I missing something ? width: gsap.utils.random(0, 20) + "%", // working // width: gsap.utils.random(0, 20, true), // working // width: gsap.utils.random(0, 20, true) + "%", // not working I've made a codepen exemple : See the Pen qBwgPqR by mi-re (@mi-re) on CodePen Link to comment Share on other sites More sharing options...
Rodrigo Posted April 24 Share Posted April 24 Hi, That is to be expected actually. When you pass a true argument in the random utility method, what is returned is a function that has to be called, without it what's returned is a number: typeof gsap.utils.random(0, 20) // -> Number typeof gsap.utils.random(0, 20) + "%" // -> String - Coerced into a string typeof gsap.utils.random(0, 20, true) // -> Function typeof gsap.utils.random(0, 20, true) + "%" // -> ....?? I quit! What you can do is something like this: width: () => gsap.utils.random(0, 20, true)() + "%" But I don't really see any upside between using that one ☝️ instead of this: width: gsap.utils.random(0, 20) + "%", Finally you can use the Unitize utility as well: https://gsap.com/docs/v3/GSAP/UtilityMethods/unitize() Hopefully this helps. Happy Tweening! Link to comment Share on other sites More sharing options...
benoit958 Posted April 24 Author Share Posted April 24 Thank you Rodrigo, the thing is if I'm doing that width: gsap.utils.random(0, 20) + "%", width is calculated just once and not on each repeat cycle. but this is working, thanks width: () => gsap.utils.random(0, 20, true)() + "%" Link to comment Share on other sites More sharing options...
GreenSock Posted April 24 Share Posted April 24 That seems a little more verbose than necessary: // LONG width: () => gsap.utils.random(0, 20, true)() + "%" // SHORTER width: () => gsap.utils.random(0, 20) + "%" 1 Link to comment Share on other sites More sharing options...
benoit958 Posted April 25 Author Share Posted April 25 Hello, yes but the second option doesn't recalculate width on each repeat. I've updated the codepen sketch Link to comment Share on other sites More sharing options...
Solution mvaneijgen Posted April 25 Solution Share Posted April 25 Are you sure you've updated your pen? It is always best to relink the pen or even fork, so that in the thread we can see the progress of the current version of that time. To me it seems like you've removed the functions from the parameters, this is importent, because it indicates to GSAP that is can recalculate the values, if you leave this out you tell GSAP get the value once and never update it! // From height: gsap.utils.random(10, 100, true), // Is already a function width: gsap.utils.random(0, 100) + "%", // not a fucntion // To height: () => gsap.utils.random(10, 100, true), // Better save and also convert it to a function width: () => gsap.utils.random(0, 100) + "%", // Convert to function Hope it helps and happy tweening! See the Pen XWQOGpa?editors=0010 by mvaneijgen (@mvaneijgen) on CodePen 2 Link to comment Share on other sites More sharing options...
benoit958 Posted April 25 Author Share Posted April 25 Haa, ok I get it now. Thanks ! 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