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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <h1>anchorsToProgress() GSAP Demo</h1>

<svg id="svg" viewBox="0 0 900 200">
  
</svg>
<div class="nav">
  
  <button class="previous">previous</button>
  <button class="next">next</button>
</div>

<link href='//fonts.googleapis.com/css?family=Signika+Negative:300,400' rel='stylesheet' type='text/css'>
              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
  background-color: black;
  color: white;
  font-family: "Signika Negative", sans-serif;
  text-align: center;
}

h1 {
  margin: 20px;
  text-align: center;
}

svg {
  overflow: visible;
  height: 100%;
  max-width: 100%;
}

circle {
  fill: white;
}

path {
  stroke-width: 3px;
  stroke: #444;
  fill: none;
}

.nav {
  display: flex;
  justify-content: center;
  padding: 0 0 20px 0;
  align-content: center;
}

button {
  position: relative;
  margin: 5px 10px 5px 0px;
  outline: none;
  border: none;
  padding: 10px 16px;
  background: #414141;
  background-image: -webkit-linear-gradient(top, #575757, #414141);
  background-image: -moz-linear-gradient(top, #575757, #414141);
  background-image: linear-gradient(to bottom, #575757, #414141);
  text-shadow: 0px 1px 0px #414141;
  -webkit-box-shadow: 0px 1px 0px 414141;
  -moz-box-shadow: 0px 1px 0px 414141;
  box-shadow: 0px 1px 0px 414141;
  color: #ffffff;
  text-decoration: none;
  border-radius: 4px;
  font-family: "Signika Negative", sans-serif;
  text-transform: uppercase;
  font-weight: 600;
  cursor: pointer;
  font-size: 18px;
  line-height: 18px;
}

button:hover {
  background: #57a818;
  background-image: -webkit-linear-gradient(top, #57a818, #4d9916);
  background-image: -moz-linear-gradient(top, #57a818, #4d9916);
  background-image: -ms-linear-gradient(top, #57a818, #4d9916);
  background-image: -o-linear-gradient(top, #57a818, #4d9916);
  background-image: linear-gradient(to bottom, #57a818, #4d9916);
  color: #ffffff;
  text-decoration: none;
}


              
            
!

JS

              
                gsap.registerPlugin(MotionPathPlugin, DrawSVGPlugin);

let anchors = [{x:50, y:130}, {x:300, y:10}, {x:500, y:70}, {x:700, y:190}, {x:850, y:100}], // anchor coordinates (feel free to change these if you want)
    rawPath = MotionPathPlugin.arrayToRawPath(anchors, {curviness:0.5}),
    path = buildPath(anchors, rawPath), // builds all the anchors and paths, returns the red SVG path
    progressArray = anchorsToProgress(rawPath), // calculate the progress value associated with each anchor, returned as an array.
    curIndex = 0,
    drawPath = gsap.from(path, { // this is the tween of the red path. We pause it initially and animate its progress.
      ease: "none",
      drawSVG: false,
      paused: true,
      duration: 5
    });

// This function calculates the progress value along the rawPath for each anchor and returns an array accordingly.
// So if the rawPath has 5 anchors, the array that's returned will have 5 progress values, each between 0 and 1.
function anchorsToProgress(rawPath, resolution) {
	resolution = ~~resolution || 12;
	if (!Array.isArray(rawPath)) {
		rawPath = MotionPathPlugin.getRawPath(rawPath);
	}
	MotionPathPlugin.cacheRawPathMeasurements(rawPath, resolution);
	let progress = [0],
		length, s, i, e, segment, samples;
	for (s = 0; s < rawPath.length; s++) {
		segment = rawPath[s];
		samples = segment.samples;
		e = segment.length - 6;
		for (i = 0; i < e; i+=6) {
			length = samples[(i / 6 + 1) * resolution - 1];
			progress.push(length / rawPath.totalLength);
		}
	}
	return progress;
}

// jump to a specific anchor (animate the tween to that progress value)
function goToAnchor(index) {
	curIndex = index;
	gsap.to(drawPath, {
		progress: progressArray[curIndex],
		overwrite: true,
		duration: 2
	});
}

// click as many times and as quickly as you want.
document.querySelector(".next").addEventListener("click", function() {
	if (curIndex < anchors.length - 1) {
		goToAnchor(curIndex + 1);
	}
});
document.querySelector(".previous").addEventListener("click", function() {
	if (curIndex > 0) {
		goToAnchor(curIndex - 1);
	}
});


// -- setup ---
function buildPath(anchors, rawPath) {
  let svg = document.querySelector("#svg"),
      pathTemplate = createSVG("path", svg, {d: MotionPathPlugin.rawPathToString(rawPath)}),
      path = createSVG("path", svg, {d: MotionPathPlugin.rawPathToString(rawPath)}),
      i;
  gsap.set(path, {stroke: "red", strokeWidth: 5});
  for (i = 0; i < anchors.length; i++) {
    createSVG("circle", svg, {cx: anchors[i].x, cy: anchors[i].y, r: 5});
  }
  return path;
}

function createSVG (type, container, attributes) {
    var element = document.createElementNS("http://www.w3.org/2000/svg", type),
        reg = /([a-z])([A-Z])/g,
        p;
    for (p in attributes) {
        element.setAttributeNS(null, p.replace(reg, "$1-$2").toLowerCase(), attributes[p]);
    }
    if (typeof(container) === "string") {
        container = document.querySelector(container);
    }
    container.appendChild(element);
    return element;
}

goToAnchor(1);
              
            
!
999px

Console