Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section class="content">
  <h1>SECTION 1</h1>
  <p>Please scroll down</p>
</section>
<section class="content">
  <div id="you" class="you">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 580 580" text-rendering="geometricPrecision" shape-rendering="geometricPrecision" style="white-space: pre;">
      <g id="svg-YourSeatSphere" transform="translate(290,290) translate(-210,-210)">
        <ellipse id="svg-yourSeat" rx="300" ry="300" fill="#33741c" stroke-linecap="round" stroke="#86b744" stroke-width="5" transform="translate(210,210) scale(0.7,0.7)" />
      </g>
      <circle id="svg-measure" stroke="rgba(255,255,255,0.1)" stroke-width="5" fill="none" r="250" cx="290" cy="290" />
      <circle id="svg-impactRingCircle" stroke="white" stroke-width="5" fill="transparent" r="250" cx="290" cy="290" />
      <path id="svg-track" d="M40,290 a1,1 0 0,0 500,0" fill="none" />
      <circle id="svg-handle" fill="#ffffff" cx="20" cy="20" r="20" />
    </svg>
  </div>
</section>

<section class="content">
  <h1>SECTION 2</h1>
</section>
              
            
!

CSS

              
                body {
  background: #0e1a25;
  text-align: center;
  color: #ffffff;
}
.content {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.you {
  width: 300px;
}
/* IMPACT CONTROL */
#svg-impactRingCircle {
  /* transition: 0.35s stroke-dashoffset; */
  transform: rotate(-180deg);
  transform-origin: 50% 50%;
}

              
            
!

JS

              
                window.onload = function () {
  // MOTHER OF ALL TIMELINES
  var tl_mother = gsap.timeline();
  tl_mother.call(ALL_FUNCTIONS.yourImpact);
  //More nested functions here
};

var ALL_FUNCTIONS = {
  yourImpact: function () {
    gsap.set("#svg-yourSeat", { fill: "red", stroke: "red" });

    var circle = document.getElementById("svg-impactRingCircle");
    var radius = circle.r.baseVal.value;
    var circumference = radius * 2 * Math.PI;
    circle.style.strokeDasharray = `${circumference} ${circumference}`;
    circle.style.strokeDashoffset = `${circumference}`;
    function setMeasure(percent) {
      const offset = circumference + (percent / 100) * circumference;
      circle.style.strokeDashoffset = offset;
    }

    // Closest Point on Path
    // https://bl.ocks.org/mbostock/8027637
    var drag = document.querySelector("#svg-handle");
    var path = document.querySelector("#svg-track");
    var pathLength = path.getTotalLength() || 0;
    var startPoint = path.getPointAtLength(0);

    gsap.set(drag, {
      // onDrag:update,
      transformOrigin: "center",
      xPercent: -50,
      yPercent: -50,
      x: startPoint.x,
      y: startPoint.y
    });
    var draggable = new Draggable(drag, {
      liveSnap: {
        points: pointModifier
      }
    });

    function pointModifier(point) {
      var p = closestPoint(path, pathLength, point);
      return p.point;
    }
    function closestPoint(pathNode, pathLength, point) {
      console.log(point);
      var precision = 8,
        best,
        bestLength,
        bestDistance = Infinity;
      // linear scan for coarse approximation
      for (
        var scan, scanLength = 0, scanDistance;
        scanLength <= pathLength;
        scanLength += precision
      ) {
        if (
          (scanDistance = distance2(
            (scan = pathNode.getPointAtLength(scanLength))
          )) < bestDistance
        ) {
          (best = scan),
            (bestLength = scanLength),
            (bestDistance = scanDistance);
        }
      }
      // binary search for precise estimate
      precision /= 2;
      while (precision > 0.5) {
        var before,
          after,
          beforeLength,
          afterLength,
          beforeDistance,
          afterDistance;
        if (
          (beforeLength = bestLength - precision) >= 0 &&
          (beforeDistance = distance2(
            (before = pathNode.getPointAtLength(beforeLength))
          )) < bestDistance
        ) {
          (best = before),
            (bestLength = beforeLength),
            (bestDistance = beforeDistance);
        } else if (
          (afterLength = bestLength + precision) <= pathLength &&
          (afterDistance = distance2(
            (after = pathNode.getPointAtLength(afterLength))
          )) < bestDistance
        ) {
          (best = after),
            (bestLength = afterLength),
            (bestDistance = afterDistance);
        } else {
          precision /= 2;
        }
        // console.log("total length: ",pathLength, "distance: ", beforeLength);
        update(beforeLength);
      }
      var len2 = bestLength + (bestLength === pathLength ? -0.1 : 0.1);
      return {
        point: best,
        distance: Math.sqrt(bestDistance)
      };
      function distance2(p) {
        var dx = p.x - point.x,
          dy = p.y - point.y;
        return dx * dx + dy * dy;
      }
    }

    function update(distance) {
      // progress = currentDistance/pathLength * 100 / 100
      // Divide by 100 because timline progress is measured as fraction of 1
      tlProgress = Math.round((distance / pathLength) * 100) / 100;
      // Divide by two because the total possible distance is half of the circle
      measure = (tlProgress * 100) / 2;
      setMeasure(measure);
      tl_impactEffect(tlProgress);
    }

    //  YOUR IMPACT EFFECT
    function tl_impactEffect(tlprogress) {
      var tl = gsap.timeline({
        paused: true,
        duration: 10
      });
      tl.to(
        "#svg-yourSeat",
        { duration: "0.5", fill: "red", stroke: "red" },
        "<"
      );
      tl.to(
        "#svg-yourSeat",
        { duration: "2.5", fill: "#86b744", stroke: "#86b744" },
        "<"
      );
      tl.progress(tlProgress);
      return tl;
    }
    var tl = gsap.timeline({
      // paused: true,
      scrollTrigger: {
        trigger: ".you",
        start: "top bottom",
        end: "top top",
        scrub: true
      }
    });
    tl.to("#svg-handle", {
      duration: 1,
      ease: "power1.inOut",
      motionPath: {
        end: 0.3,
        path: "#svg-track",
        align: "#svg-track",
        alignOrigin: [0.5, 0.5]
      }
    });
    tl.call(closestPoint, [path, pathLength, { x: 143.0357, y: 492.263 }], ">");
    return tl;
  } //END YOUR IMPACT
};

              
            
!
999px

Console