Jump to content
Search Community

Animating CSS

Petr Mucha test
Moderator Tag

Recommended Posts

Hi, I was wondering whether it is a good practice to use gsap to tween an integer and than asign this integer to an css property with requestAnimationFrame function, or it'is a nonsense and is always better to tween the css property with gsap css plugin. And if so, than why?.

Thanks for any answers

P
 

Link to comment
Share on other sites

Thanks for the reply! I want the requestAnimationFrame to be watching a variable simple because there are more factors influencing it during the Life Cycle of the app. It'll be increased over time automatically and different types of interactions will take place so I was considering this hack of tweening, increasing, decreasing and resetting the variable rather than the css property. Just for simplifying things in coding. I am fine with it. The only thing is whether it would influence the performance in a bad way or no.

 

See the Pen povzgjL by mindfulness (@mindfulness) on CodePen


I suck in Codepening but here's the example. The thing is that the cube shall have various ways of interaction like swiping, automove and such so the idea of having one variable changing and the rest done by every frame loop seems quite nice to me, but...

 

 

Link to comment
Share on other sites

My question was more general like if it is a totally wrong idea to use gsap to animate a variable rather than css property. The combination of requestAnimationFrame and gsap tween for changing the value of a variable. It's in the whole code not just a line or two. I use this key function to make it move:
 

move_the_cube(){

    /*
    * MAIN LOOP EVERY ANIMATION FRAME
    */
        if(this._y >= 360)
            this._y = 0;

        
        window.requestAnimationFrame(()=>{
            this.cube_el.style.transform = 'rotateY('+this._y+'deg)';
            this.cube_rotator_el.style.transform = ' rotatex('+this._x+'deg)';
            this.move_the_cube();
        });
        
    }
}


 

Link to comment
Share on other sites

18 minutes ago, Petr Mucha said:

My question was more general like if it is a totally wrong idea to use gsap to animate a variable rather than css property.

Oh, not at all! GSAP is really a property (variable) setter so it works with any object that you give it, including DOM, canvas, WebGL, etc.

 

Your use of raf with GSAP doesn't make much sense to me. Just use GSAP's .set() method and it will do that all for you (with likely less errors):

 

move_the_cube(){

    /*
    * MAIN LOOP EVERY ANIMATION FRAME
    */
        if(this._y >= 360)
            this._y = 0;

        gsap.set(cube_el, {rotationY: this._y, rotationX: this._x});
        requestAnimationFrame(this.move_the_cube);
    }
}

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

6 minutes ago, ZachSaucier said:

 

Your use of raf with GSAP doesn't make much sense to me. 

 

Hmm I think it comes from working with three.js where You just need raf to make things moving (render scene) I'll do the testing on how it would work with direct animation of css.

Thanks a bunch for Your time

Have a good one!

 

Link to comment
Share on other sites

You don't need to use requestAnimationFrame as gsap is already using it. Just use the onUpdate callback in a tween/timeline, or gsap's ticker. 

https://greensock.com/docs/v3/GSAP/gsap.ticker

 

And another way to set properties is with the quick setter.

https://greensock.com/docs/v3/GSAP/gsap.quickSetter()

 

This demo uses ticker and quickSetter.

 

See the Pen WNNNBpo by GreenSock (@GreenSock) on CodePen

 

 

 

 

  • Like 3
  • Thanks 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...