Jeanno Posted January 6, 2023 Share Posted January 6, 2023 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 More sharing options...
fakesamgregory Posted January 6, 2023 Share Posted January 6, 2023 Not sure if I fully understand. A more complete code sample would be awesome but can't you use... const myValue = '120' THREE.Math.degToRad(Number(myValue)) Link to comment Share on other sites More sharing options...
GreenSock Posted January 7, 2023 Share Posted January 7, 2023 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 More sharing options...
Jeanno Posted January 7, 2023 Author Share Posted January 7, 2023 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 More sharing options...
Solution GreenSock Posted January 7, 2023 Solution Share Posted January 7, 2023 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. 1 Link to comment Share on other sites More sharing options...
Jeanno Posted January 7, 2023 Author Share Posted January 7, 2023 All right, thanks for your answer 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