Jump to content
Search Community

Is it possible to compile random method from its string form?

Jeanno test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hello everyone !

I want to manipulate the rotation of a 3d object (using threejs).

So in order to do that, I'm able to use:

gsap.to(myObject.rotation, {
  duration: 1,
  x: "random(0,30)"
});

The issue is that the rotation property of threejs object is in radians. So I need to convert it to degrees. I can use the util method from threejs:

THREE.Math.degToRad(myValue)

but it needs a number, not a string.

So I would like to know if there is a way to "compile" the random value in string format to a number ?

Of course I can make a parser to convert the string to the appropriate value. But I just wanted to know if there was a more vanilla way to do it?

Thanks !

Link to comment
Share on other sites

Why not do this?:

x: gsap.utils.random(0, THREE.Math.degToRad(parseFloat(myValue)))

Or if you're gonna need to convert degrees to radians and get a random value, I'd create my own convenience function: 

const randomRotation = degrees => gsap.utils.random(0, parseFloat(degrees) * Math.PI / 180);

// usage: 
gsap.to(obj.rotation, {x: randomRotation(30), ...});

Does that help? 

Link to comment
Share on other sites

Hello guys,

Thanks for your answers. I should have also stated that the value of X is stored as a string in a JSON file. So none of the answers above really helps.

 

Edit: For now I've copied the _replaceRandom method from GSAP that does just that. 

Link to comment
Share on other sites

  • Solution

Sure they do. I just edited mine - all you need to do is wrap your string in parseFloat(). 👍

 

If you still need help, please provide a minimal demo, like a CodePen, that clearly shows the issue (even if it's just a few lines of JS with a console.log()) - it's always best to see it in context and be able to tinker. 

  • Like 1
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...