Jump to content
Search Community

Change linear gradient on SVG lines

Mamat test
Moderator Tag

Recommended Posts

Hi,

 

I am very new on GSAP and I have some troubles in simple animation and timeline sequences

I would like to moove a line and then reverse the linear gradient color (instead of turning the color to red in the example) without getting this jerky effect on turning the color to another one.

 

I tried to create another linear gradient template in my SVG and reassign it to the line but it's not working :(

 

If someone have some tips for me, I would be greatful :)

 

I guess there is already a topic on that but I can't find it

Sorry, i'm not very fluent in english ^^

 

Thanks a lot

Mamat

See the Pen MWYKrjY by MamatUseweb (@MamatUseweb) on CodePen

Link to comment
Share on other sites

Hey Mamat and welcome to the forums! Your English is good.

 

When using a gradient fill like this, you have to animate the actual stop colors themselves. Since you're using a definition, you should animate that definition. I created a helper function to do this:

See the Pen GRgoyxw?editors=0010 by GreenSock (@GreenSock) on CodePen

 

Do note that this affects all elements that are using that same gradient. In order to prevent this, you'd need to have a new definition so that it doesn't affect other paths. 

Link to comment
Share on other sites

Hmmmm ...
 

I have another question, since the timeline is "pre compiled" before running, is it possible to reassign the original linear gradient in another step of my timeline ?

 

Here are my steps to explain what I mean :

- Rotate the line

- Reverse the color

- Rotate the line back

- Reassign original linear gradient

- And restart ...

 

My point is : is the method modify the structure of the SVG ?

Link to comment
Share on other sites

4 minutes ago, Mamat said:

Here are my steps to explain what I mean :

- Rotate the line

- Reverse the color

- Rotate the line back

- Reassign original linear gradient

- And restart ...

You can do this simply by adding more tweens to the timeline:

See the Pen PowZQmq?editors=0010 by GreenSock (@GreenSock) on CodePen

 

5 minutes ago, Mamat said:

is the method modify the structure of the SVG ?

You can use your developer tools to investigate this and see that GSAP modifies the inline styles or attributes for the elements. For example it will set something like transform="matrix(1,0,0,1,0,0)" or style="transform-origin: 0px 0px;". Depending on your needs, you may need to save the initial values sometimes. But in your case, as seen above, you don't have to worry about that. 

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