Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                
<div class="wrapSVG">
  <svg  id="svg" viewBox="0 0 600 1000">

    <circle class="big" cx="50" cy="50" r="25" />
    <circle class="big" cx="500" cy="200" r="25" />
    <circle class="big" cx="200" cy="500" r="25" />
    <circle class="big" cx="300" cy="800" r="25" />

    <polyline id="greenPath" points="50,50 500,200 200,500 300,800"/>

    <path class="limePath" d="M 50 50 L 500 200"/>
    <path class="limePath" d="M 500 200 L 200 500"/>
    <path class="limePath" d="M 200 500 L 300 800"/>

    <text id="end" x="300" y="840">THE END</text>

    <circle class="limeCircle" cx="50" cy="50" r="5"/>

  </svg>
</div>

<h1 id="line">SCROLL or CLICK</h1>

<nav>
  <button>ONE</button>
  <button>TWO</button>
  <button>THREE</button>
  <button>FOUR</button>
</nav>
              
            
!

CSS

              
                body{
  margin:0;
  background-color:grey;
  height:4000px;
}
.wrapSVG{
  position:absolute;
  width:100vw;
  height:100vh;
  overflow:hidden;
}
#svg{
  margin-top:100px;
  overflow:visible;
  visibility:hidden;
  
}
#greenPath{
  fill:none;
  stroke:green;
  stroke-width:8;
}
.limePath{
  fill:none;
  stroke:lime;
  stroke-width:2;
}
.big{
  fill:green;
  stroke:lime;
  stroke-width:2;
}
.limeCircle{
  fill:lime;
  stroke:none;
}
text{
  font-family:arial;
  font-size:10px;
  text-anchor: middle;
  fill:#155f15;
  font-weight: 800; 
}

nav{
  position:fixed;
  top:0;
  right:0;
}
button{
  font-size: 15px;
  font-weight: 600;
  color: #3a3a3a;
  padding: 5px;
  width: 70px;
  height: 26px;
  background-color: #696868;
  border: none;
  border-radius: 5px;
  margin: 6px;
  outline: none;
  cursor: pointer;
}

#line{
  position:fixed;
  margin: 12px;
  font-family: sans-serif;
  font-size: 16px;
  color: #3a3a3a;
}
              
            
!

JS

              
                console.clear();

let circles = gsap.utils.toArray(".big");
let pathSections = gsap.utils.toArray(".limePath");
let ball = document.querySelector(".limeCircle");

// proper start positions 
gsap.set(circles, {transformOrigin:'center'});
gsap.set(circles[0], {rotation:18});
gsap.set(circles[1], {rotation:197});
gsap.set(circles[2], {rotation:-46});
gsap.set(circles[3], {rotation:-111});

gsap.set('#svg', {autoAlpha:1});
gsap.set('#end', {scale:0, opacity:0})

MotionPathPlugin.convertToPath( "#greenPath" );

const dur = 3;
const tt = 3000; // total scrub time

// animate the limeCircle/diff. paths using params
function limeCircle(path){
let action = gsap.timeline({})
.to(ball, { 
  motionPath: {
    path:path, align:path, 
    alignOrigin: [0.5, 0.5]},
  duration: dur, ease:'none'
});
  return action;
}

// master timeline

let master = gsap.timeline({
  scrollTrigger: {
    trigger: ".wrapSVG",
    pin:true,
    start: 'top top',
    end: "+=" + tt,
    scrub: 0.3,
    onUpdate: ({progress}) => console.log(progress),
  },
  defaults:{duration:dur, ease:'none'},
  onComplete: () =>{
    gsap.to('#end', {scale:1, opacity:1})
  }
})
.from(circles, {drawSVG:"0", stagger:dur*2})
.from(pathSections, {drawSVG:"0", stagger:dur*2},dur)

.add(limeCircle(pathSections[0]), dur)
.add(limeCircle(pathSections[1]), dur*3)
.add(limeCircle(pathSections[2]), dur*5)

.to('#svg', {attr:{viewBox:'0 150 600 1000'}}, dur)
.to('#svg', {attr:{viewBox:'0 450 610 1000'}}, dur*3)
.to('#svg', {attr:{viewBox:'200 780 200 500'}}, dur*5)

.set('#end', {scale:1, opacity:0}, dur*5)

// per button ===================
var stops = [0, 0.428, 0.714, 1.01] // see progress of ST

let lastPos = 0,
    nextPos,
    dist = [lastPos];

gsap.utils.toArray('button').forEach(function(button, i) {
  
  function go(here, speed){
    gsap.to(window, {
      scrollTo: tt*stops[here],
      duration: 3*speed, ease:'power1.inOut'
    });
    dist = [here]; // actual dist
  }

  button.addEventListener('click', () =>{
    
    // same speed per dist (= no. of sections)
    nextPos = i;
    dist.push(nextPos);
    dist.sort((a, b) => b - a);
    let speed = dist[0] - dist[1]
    
    go(i, speed); 
    
    console.log(speed);
    
  })
});


              
            
!
999px

Console