Jump to content
Search Community

ColorProps? Tweening from one linear-gradient to the other linear-gradient

venn test
Moderator Tag

Go to solution Solved by Jonathan,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

I am trying to figure how the colorProps plugin works?

 

I have a div #bgGrad with a linear-gradient background and I would like to tween it to ANOTHER linear gradient.

 

How do I do that? I tried

TweenMax.to("#bgGrad", 1, {colorProps:{background:"linear-gradient(#ffffff, #007dba, #004165)"}});

but in vain...

Link to comment
Share on other sites

  • Solution

Hello venn, and Welcome to the GreenSock Forum!

 

Here is an example of a cross browser linear-gradient tween

 

See the Pen vJwzD by jonathan (@jonathan) on CodePen

 

And here is a cross browser radial-gradient tween:

 

See the Pen BeJwd by jonathan (@jonathan) on CodePen

 

The syntax is very important since browsers like Chrome will keep changing the syntax instead of using the standard syntax for CSS gradients. Chrome will still want the webkit prefix even though at beginning of last year they honored the standards syntax and now they don't. So to play it safe make sure to use the webkit prefix and the older gradient syntax.

 

So you will notice in my codepen examples that i do a check for if the browser is Chrome, since Google Chrome can be a real poopy pants, changing what it expects as proper syntax for CSS gradients.

 

Keep in mind that not declaring an angle will assume to top (top-to-bottom):

 

Proper syntax for CSS linear-gradient:

// new syntax
linear-gradient(to <side-or-corner>, <color> <percentage-or-length>, <color> <percentage-or-length>);
// usage:
linear-gradient(to top, #3e3e40, #252122);

Also see GSAP JS Forum Topic:

Resources:

MDN Using Gradients: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Images/Using_CSS_gradients

MDN Linear Gradients: https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient

CSS Tricks Gradients: https://css-tricks.com/css3-gradients/

 

:)

  • Like 5
Link to comment
Share on other sites

Thanks for the comprehensive reply Jonathan. Much appreciated.

 

Looking at the documentation,

http://greensock.com/docs/#/HTML5/Plugins/ColorPropsPlugin/

TweenLite.to(myObject, 1, {colorProps:{borderColor:"red", myCustomProp:"rgb(204,51,0)"}, ease:Linear.easeNone});

It seems that the first parameter is an object within the DOM? 

However it the codepen examples you have posted, you declare a variable to store the color value?

And then you apply the ongoing changing of the gradient color to the element using the onUpdate callback.

 

Am I right to say that, if you are tweening gradient colors that will be the only way to do it?

 

-

 

The documentation that is written I don't quite get it...

It says it is not intended for CSS related properties. However isn't changing borderColor a CSS property?

TweenLite.to(myObject, 1, {colorProps:{borderColor:"red", myCustomProp:"rgb(204,51,0)"}, ease:Linear.easeNone});

Is this particular line of code saying "Change myObject's borderColor from red to a rgb(204,51,0)" ?

Link to comment
Share on other sites

In the cases in the docs,  myObject is not supposed to be a DOM element. GSAP is in no way tied to the DOM so ColorPropsPlugin was written for those cases where you aren't animating CSS colors with the CSSPlugin. Imagine working in a <canvas> tool for designing a kitchen. You may have a kitchen object that holds values of colors for some of its elements. At some point you may tween kitchen.applianceColor and that will animate the color of the refrigerator and dishwasher... the kitchen object may look like this:

var kitchen = {
   floorColor:"red";
   applianceColor:"black";
}

CSSPlugin does not directly support animating CSS gradients (due mostly to the large disparity in syntax across browsers over time).

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