Jump to content
Search Community

Animate a path of curved svg path to animate along center of viewport.

fawad4Real test
Moderator Tag

Recommended Posts

Hello developer, hope you all are doing well. I want my svg to start from the center of the screen, keep animating at the center of the screen and end its animation at the center of the screen. for now, I am using an SVG and keep its height 0 and then it animates to its full height. I am using it for my timeline project but the line starts normally then moves away from the center and speeds up and leaves the screen to the bottom. 

See the Pen bGJEvrL by Fawad4real (@Fawad4real) on CodePen

Link to comment
Share on other sites

I've used scaleY instead of height, because it is a much more performant property to animate, I've also set ease: "none" because that feels better with scrub: true ScrollTriggers. I've also set the endTrigger to be the bottom of the SVG, it seems that the SVG 734px larger, so I've tried removing that from the bottom trigger to make them in sync, but I think you're better off making the SVG and the line the same size. Hope it helps and happy tweening! 

 

See the Pen LYXBVWz?editors=0010 by mvaneijgen (@mvaneijgen) on CodePen

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

  • 7 months later...

How do I do it if I have a curved path? @mvaneijgen , I want the red line to cover the green and stay at the center. It will be a lot of help.

 

<svg
data-name="Isolation Mode"
width="205"
 height="4807"
x="0px"
y="0px"
viewBox="0 0 205 4807.3"
>
<path
d="M201.6,0.1C187.4,240.3,2.5,250.3,2.5,509.8c0,269.8,200,269.8,200,539.7s-200,269.8-200,539.7
s200,269.8,200,539.7s-200,269.8-200,539.7s200,269.8,200,539.7S2.5,3478,2.5,3747.9s200,269.8,200,539.7
c0,263-190,269.7-199.6,519.7"
fill="none"
stroke="green"
strokeMiterlimit="10"
strokeWidth="5"
strokeDasharray="0 0 20.06 20.06"
/>
</svg>
<svg
data-name="Isolation Mode"
width="205"
height="4807"
x="0px"
y="0px"
viewBox="0 0 205 4807.3"
>
<path
d="M201.6,0.1C187.4,240.3,2.5,250.3,2.5,509.8c0,269.8,200,269.8,200,539.7s-200,269.8-200,539.7
s200,269.8,200,539.7s-200,269.8-200,539.7s200,269.8,200,539.7S2.5,3478,2.5,3747.9s200,269.8,200,539.7
c0,263-190,269.7-199.6,519.7"
fill="none"
stroke="red"
strokeMiterlimit="10"
strokeWidth="5"
strokeDasharray="0 0 20.06 20.06"
/>
</svg>
Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

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

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

No need to post your question multiple times. It is even better to keep it in the same topic so everyone reading gets the context of what has already been asked. 

 

I've moved to DrawSVG, because I don't know the syntax of animating strokeDasharray, I just grap DrawSVG and know I don't have to fight browser bugs. But if you're set on using it, you sure can convert it back. 

 

I had to remove style="stroke-dashoffset:0; stroke-dasharray: 4807px, 4807px" from your path because this was throwing off the calculations and made sure your start and end point where in the center. Most importantly I've set ease: "none", so that the animation is linear all the way through. Hope it helps and happy tweening! 

 

See the Pen JjVGLgR?editors=1010 by mvaneijgen (@mvaneijgen) on CodePen

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