Jump to content
Search Community

About utility methods

ngc7293 test
Moderator Tag

Recommended Posts

1. shuffle()

Can you add a parameter to ensure that the result is different from the original value?
gsap.utils.shuffle([1,2,3]) => [1,2,3]

2. wrap()

I see that the result returned by this method does not seem to contain the second parameter. Is this as expected?
 

gsap.utils.wrap(5, 10, 0) => 5

gsap.utils.wrap(5, 10, 5) => 5 (not 10?)

3. wrapYoyo()

I failed to grasp the logic of this method.

https://greensock.com/docs/v3/GSAP/UtilityMethods/wrapYoyo()

let num = gsap.utils.wrapYoyo(5, 10, 7); //8 (index 7 maps to index 3 in a 6-element range)

The result of the example of the document is 8, but what I get is 7.
gsap.utils.wrapYoyo(5, 10, 7) => 7

 I try to understand it like this, 

value  10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10...
index   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...

but I seem to be wrong.

gsap.utils.wrapYoyo(1, 2, 0) => 2(not 1?)

gsap.utils.wrapYoyo(1, 3, 0) => 2(not 1 or 3?why 2?)

 

Please forgive me for my poor English.
Link to comment
Share on other sites

1 hour ago, ngc7293 said:

Can you add a parameter to ensure that the result is different from the original value?

 

No. A unique shuffle might go into an infinite loop.

 

1 hour ago, ngc7293 said:

 

I see that the result returned by this method does not seem to contain the second parameter. Is this as expected?

 

1 hour ago, ngc7293 said:

3. wrapYoyo()

I failed to grasp the logic of this method.

 

Play around with this demo. It does wrap and wrapYoyo on a 0-5 range. Feel free to change those values.

 

See the Pen 5e504d79528f8087247d5bfa796efd34?editors=0010 by osublake (@osublake) on CodePen

 

 

  • Like 3
Link to comment
Share on other sites

15 hours ago, ngc7293 said:

Can you add a parameter to ensure that the result is different from the original value?

I don't want to sacrifice the kb it'd take to put that into the core (I doubt even 0.1% of our users would ever tap into it), but here's a function that should give you exactly what you're asking for and to Blake's excellent point, it gives up if 100 attempts fail to deliver a unique result (meaning it's almost surely impossible to get a unique shuffle, like if you passed in an array like [1,1,1]). 

function uniqueShuffle(array) {
    let _arraysMatch = (a1, a2) => {
		    let i = a1.length;
		    while (i-- && a1[i] === a2[i]) { }
		    return i < 0;
	    },
	    copy = array.slice(0),
        i = 100;
    while (_arraysMatch(gsap.utils.shuffle(array), copy) && i--) { }
    return array;
 }

 

16 hours ago, ngc7293 said:

The result of the example of the document is 8, but what I get is 7

I'm so sorry about that - there were several typos in the docs. I don't blame you for being confused! Those should be fixed now. 

 

50 minutes ago, ngc7293 said:

Although it has not solved my doubts, thank you for your reply.

What's still confusing at this point? Blake's demo illustrates what these utility methods do well. Maybe read the updated docs and see if that helps at all? 

  • Like 1
Link to comment
Share on other sites

On the first point, I think I need better functionality than reducing the volume a little bit, because there is a similar treatment in my common method,and now I have to load two methods to deal with the same problem. The volume of GSAP is reduced, but the volume of my whole project is not optimized.

In addition, with regard to the question of endless loops, I think this is something that every programmer should take the initiative to avoid. In fact, who would try to disrupt such an array([1,1,1])?

The second is wrap method. I see that the result contains the first parameter but not the second parameter. Shouldn't it contain both parameters?

gsap.utils.wrap(0, 5, n) => 0 <= result < 5, why not 0 <= result <= 5

The last is wrapYoyo method.

Therefore, it is difficult to correspond to the corresponding relationship between the index value and the result value, whether it is flashback or positive order according to the first two parameters.
gsap.utils.wrapYoyo(0, 3, 0) => 0
gsap.utils.wrapYoyo(1, 3, 0) => 2

These are my personal opinions. If it looks confusing, I'm sorry I used machine translation.
Link to comment
Share on other sites

18 hours ago, ngc7293 said:

On the first point, I think I need better functionality than reducing the volume a little bit, because there is a similar treatment in my common method,and now I have to load two methods to deal with the same problem. The volume of GSAP is reduced, but the volume of my whole project is not optimized.

I don't understand. Are you saying we should put all that functionality into the GSAP core and force EVERYONE to pay the kb price even though 99.9% would never use it? And why would you have to load two methods in your code? You're welcome to just use the uniqueShuffle() method I provided. If you're saying it's a waste of kb to load the gsap.utils.shuffle() method AND the uniqueShuffle() method, that's not really true because uniqueShuffle() leverages code in gsap.utils.shuffle(), so it's all necessary anyway. Maybe I misunderstood your point. 

 

18 hours ago, ngc7293 said:

In addition, with regard to the question of endless loops, I think this is something that every programmer should take the initiative to avoid. In fact, who would try to disrupt such an array([1,1,1])?

I disagree. Sometimes these Arrays are built dynamically, like based on user interaction or something. We can't just assume that developers will never feed in an array that has uniform values. I've been developing this library long enough to have people surprise me all the time with how they use the tools :)

 

18 hours ago, ngc7293 said:

The second is wrap method. I see that the result contains the first parameter but not the second parameter. Shouldn't it contain both parameters?

No. The perfect use case is for wrapping Arrays, like gsap.utils.wrap(0, array.length) for iterators. It's the same for the modulus (%) operator, it's non-inclusive of the max. 

 

18 hours ago, ngc7293 said:

Therefore, it is difficult to correspond to the corresponding relationship between the index value and the result value, whether it is flashback or positive order according to the first two parameters.

I don't understand, sorry. What's "flashback". The examples you provided look exactly correct. I don't see why you'd think it's not intuitive. 

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...