Jump to content
Search Community

SVG Animation: changing path object in timeline

fireball test
Moderator Tag

Go to solution Solved by fireball,

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

Hello

 

Having googled this issue for some time I'd really appreciate some help. I'm sure this is simple when you know how!

 

I'm adapting the svg animation technique here 

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

 

I want to animate a number of paths using a timeline.  I've setup a very simple 2 item demo of the issue: .  

 

This demo first calls the setupNext function to set the length and pathLength object (obj).

 

The first tween draws the first path (triangle) just fine.

 

onComplete in the first tween calls the setupNext function again to get and set length and pathLength values for the second path to tween.

 

In this second tween the length appears to set to zero (the rectangle vanishes) but the value doesn't tween to draw it again.

 

Looking at  a console.log() of the object it's clear I'm not setting it in the right way for the second tween. Is this a scope problem? Should I use invalidate()?

 

Anyone able to point me in the right direction?

 

Thank you.

See the Pen NPWJoV by fireball (@fireball) on CodePen

Link to comment
Share on other sites

Hi and welcome to the GreenSock forums.

 

Yeah, there are some bits of your code that aren't re-usable for multiple tweens.

Here is a little bit of a cleanup:

 

var svg1 = document.querySelector('#svg'+1);
var svg2 = document.querySelector('#svg'+2);
var tl = new TimelineLite();


//create a timeline with 2 tweens that draw 2 separate strokes
tl.add(createLineTween(svg1))
  .add(createLineTween(svg2));


//this function creates a single tween that animates the stroke of an svg
function createLineTween(svg) { 
   var pathObject = {length:0, pathLength:svg.getTotalLength()}; 
   var tween = TweenLite.to(pathObject, 0.5, {length:pathObject.pathLength, onUpdate:drawLine, onUpdateParams:[pathObject, svg], immediateRender:true});
   return tween;
};




 //update stroke   
 function drawLine(obj, svg) {
  svg.style.strokeDasharray = [obj.length, obj.pathLength].join(' ');
 };

Take a look here: http://codepen.io/GreenSock/pen/WbNBwO?editors=001

 

The basic idea is that the createLineTween() function is told which svg to use. It then creates a pathObject and a tween to draw the stroke.

That tween is then passed into a timeline via the add() method.

 

It appears the path of the rectangle isn't closing completely... not exactly sure why

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