Jump to content
Search Community

Animating stroke-dashoffset works fine in CSS animation - stagger with GSAP?

Devotee007 test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hi,

I have this code for a CSS animation of a pie chart. It works fine. 
.icon:hover .bottom  {
  stroke-dasharray: 0 100;
  stroke-dashoffset: -25;
}

But when I do this with GSAP - tl.to('.bottom', {strokeDasharray: "0,100", strokeDashoffset: "-25", dutation:.3}, 0)  the animation stagger. 

What am I doing wrong? using stroke-dashoffset: -25; was the only solution I found to reverse the animation of the pink pie part. Is there another solution?

//Devotee007

See the Pen JjBgQrO by Devotee007 (@Devotee007) on CodePen

Link to comment
Share on other sites

Solved it by changing stroke-dashoffset from 0 to -125

 

<circle class="bottom" cx="50%" cy="50%" r="15.9" stroke="pink" stroke-dasharray="25 100" stroke-dashoffset="-125"></circle>

 

And then this GSAP code.

const tl = gsap.timeline({ paused: true, reversed: true})
tl.to('.top', {strokeDasharray: "0,100", duration:.3}, 0)
tl.to('.bottom', {strokeDasharray: "0,100", duration:.3}, 0)
tl.to('.icon', {x:"7.5rem", duration:.3}, .1)

const playBtn = document.getElementById('icon')

playBtn.addEventListener('click', () => {
  tl.reversed() ? tl.play() : tl.reverse()
})

 

//Devotee007

Link to comment
Share on other sites

Is there a new pen with the updated code or is the original one updated? 

 

The frist two tweens have the same animation, so you can also just combine those to be one tween. Also in the .icon tween it states transform: "translate(bottom, bottom)", this does nothing. https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translate translate wants a number value, bottom is nothing in this case, did you mean transformOrigin? And in rotation you can also direct†y call the number instead of a string "90deg" = 90

 

9 hours ago, Devotee007 said:

but I can't animate in with ease now, because the pink one has a negative value

An ease should not care about if it is a negative value or not. 

 

See the Pen PodYJve by mvaneijgen (@mvaneijgen) on CodePen

  • Like 2
Link to comment
Share on other sites

@mvaneijgen, this is the updated codepen I changed this:

 

stroke-dashoffset from 0 to -125

<circle class="bottom" cx="50%" cy="50%" r="15.9" stroke="pink" stroke-dasharray="25 100" stroke-dashoffset="-125"></circle>


After that the flickering stopped. In the video file in previous post you can see the "flickering". When I just did the animation with CSS it was smooth.

After changing from 0 to -125 the ease on the animation seem to happen backwards on the pink area. See video file in this post. Both the green and pink are has ease:"back.in" when I recorded it.

Thank you for your tip about the code, I will updated. :)

 

  • Like 1
Link to comment
Share on other sites

Hi @mvaneijgen

 

Here's a codepen which shows the original issue - 

See the Pen MWqgyzM by barthendrix (@barthendrix) on CodePen

 

If you uncomment the commented css lines (which handle the transition of the pie wedges on hover) and put a return at the top of the js file, the css animation is smooth. When you use the js only approach, the pink wedge has a flicker when it's animating. It seems like it's the combination of animating stroke-dashoffset and stroke-dasharray that's causing the flicker.

 

Hope I got that right @Devotee007 - and the animation is starting to look pretty cool!

  • Like 1
Link to comment
Share on other sites

  • Solution

It looks to me that's just because by default when GSAP animates px-based values it generally rounds them to whole numbers because in most cases the browser doesn't do sub-pixel rendering (for example, on values like top, left, etc.). You can disable it by setting autoRound: false: 

See the Pen PodoqyJ?editors=1010 by GreenSock (@GreenSock) on CodePen

 

Does that clear things up? 

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