Search code examples
javascriptjquerysvggsapscrollmagic

ScrollMagic + TweenMax: Draw multiple SVG paths simultaneously?


This is my script for drawing an SVG path with ScrollMagic:

 // Prepare SVG
  function pathPrepare_journey($el) {
    var lineLength_journey = $el[0].getTotalLength();
    $el.css("stroke-dasharray", lineLength_journey);
    $el.css("stroke-dashoffset", lineLength_journey);
  }

  var $journey1 = $("path#path1");
  var $journey1_2 = $("path#path2");
  var $journey1_3 = $("path#path3");

  pathPrepare_journey($journey1);
  pathPrepare_journey($journey1_2);
  pathPrepare_journey($journey1_3);

  // Reference to container
  var container = $("#section1");
  var containerHeight = $(container).height();
  var vpHeight = $(window).height();

  // Init controller
  var SVGcontroller_journey = new ScrollMagic.Controller();

  // Build tween
  var tween_journey = new TimelineMax().add(
    TweenMax.to($journey1, 1, { strokeDashoffset: 0, ease: Linear.easeNone })
  );

  // Build scene
  var scene = new ScrollMagic.Scene({
    triggerElement: container,
    duration: containerHeight - vpHeight / 2,
    tweenChanges: true
  })
    .setTween(tween_journey)
    .addIndicators({
      name: "Draw Journey Lines#1",
      colorTrigger: "brown",
      colorStart: "brown",
      colorEnd: "brown",
      indent: 600
    })
    .addTo(SVGcontroller_journey);

It works perfectly fine, but as you can see, I have three individual paths inside my SVG ($journey1,$journey1_2 & $journey1_3), and the ScrollMagic scene currently only draws one of them, $journey1, because I was only able to add that one to the TimelineMax().

My simple question: How do I add the other paths so they are drawn at the same time as $journey1?

I was able to add the other paths, but they are being drawn consecutively:

// Build tween
  var tween_journey = new TimelineMax()
    .add(
      TweenMax.to($journey1, 1, { strokeDashoffset: 0, ease: Linear.easeNone })
    )
    .add(
      TweenMax.to($journey1_2, 1, { strokeDashoffset: 0, ease: Linear.easeNone })
    );

Solution

  • I figured out, that I can target paths by class inside the TweenMax timeline like so:

    var tween_journey = new TimelineMax();
    tween_journey.to(".cls-2", 1, { strokeDashoffset: 0, ease: Linear.easeNone });
    

    This animates/draws all paths of the given class at the same time and hence solves my problem.