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

              
                <svg id="svg" viewBox="0 0 1000 1000">
  <path id="path1"></path>
  <path id="path2"></path>  
  <g id="dot-container"></g>
</svg>

<div class="controls">
  <input type="checkbox" id="show-points" checked>
  <label for="show-points">Show Points</label>
</div>
              
            
!

CSS

              
                #svg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

#path1 {
  fill: #2196F3;
}

#path2 {
  fill: #64b5f6;
}

.controls {
  position: fixed;
  top: 0;
  left: 0;
  padding: 15px;
}

.dot {
  fill: #1a237e;
  fill-opacity: 0.3;
  stroke: #1a237e;
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}
              
            
!

JS

              
                console.clear();

let blob1 = createBlob({
  element: document.querySelector("#path1"),
  numPoints: 20,
  centerX: 500,
  centerY: 500,
  minRadius: 300,
  maxRadius: 325,
  minDuration: 1,
  maxDuration: 2
});

let blob2 = createBlob({
  element: document.querySelector("#path2"),
  numPoints: 10,
  centerX: 500,
  centerY: 500,
  minRadius: 200,
  maxRadius: 225,
  minDuration: 0.75,
  maxDuration: 1.75
});

// To show the points
createDots([blob1, blob2]);

function createBlob(options) {
   
  let points = [];  
  let path = options.element;
  let slice = (Math.PI * 2) / options.numPoints;
  let startAngle = gsap.utils.random(0, 360);
  
  let tl = gsap.timeline({
    onUpdate: update
  });  
  
  for (let i = 0; i < options.numPoints; i++) {
    
    let angle = startAngle + i * slice;
    let duration = gsap.utils.random(options.minDuration, options.maxDuration);
    
    let point = {
      x: options.centerX + Math.cos(angle) * options.minRadius,
      y: options.centerY + Math.sin(angle) * options.minRadius
    };   
    
    let tween = gsap.to(point, {
      duration,
      x: options.centerX + Math.cos(angle) * options.maxRadius,
      y: options.centerY + Math.sin(angle) * options.maxRadius,
      repeat: -1,
      yoyo: true,
      ease: "sine.inOut"
    });
    
    tl.add(tween, -gsap.utils.random(0, duration));
    points.push(point);
  }
  
  options.tl = tl;
  options.points = points;
  
  function update() {
    path.setAttribute("d", cardinal(points, true, 1));
  }
  
  return options;
}

// Cardinal spline - a uniform Catmull-Rom spline with a tension option
function cardinal(data, closed, tension) {
  
  if (data.length < 1) return "M0 0";
  if (tension == null) tension = 1;
  
  let size = data.length - (closed ? 0 : 1);
  let path = "M" + data[0].x + " " + data[0].y + " C";
  
  for (let i = 0; i < size; i++) {
    
    let p0, p1, p2, p3;
    
    if (closed) {
      p0 = data[(i - 1 + size) % size];
      p1 = data[i];
      p2 = data[(i + 1) % size];
      p3 = data[(i + 2) % size];
      
    } else {
      p0 = i == 0 ? data[0] : data[i - 1];
      p1 = data[i];
      p2 = data[i + 1];
      p3 = i == size - 1 ? p2 : data[i + 2];
    }
        
    let x1 = p1.x + (p2.x - p0.x) / 6 * tension;
    let y1 = p1.y + (p2.y - p0.y) / 6 * tension;

    let x2 = p2.x - (p3.x - p1.x) / 6 * tension;
    let y2 = p2.y - (p3.y - p1.y) / 6 * tension;
    
    path += " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + p2.x + " " + p2.y;
  }
  
  return closed ? path + "z" : path;
}

function createDots(blobs) {
  
  let dotContainer = document.querySelector("#dot-container");
  let checkBox = document.querySelector("#show-points");
  let showPoints = true;
  let points = [];
  let dots = [];
  
  blobs.forEach(function(blob) {        
    blob.points.forEach(function(point) {      
      
      let dot = document.createElementNS("http://www.w3.org/2000/svg", "circle");
      dot.setAttribute("r", 4);
      dot.setAttribute("class", "dot");
      dotContainer.appendChild(dot);
      dots.push(dot);
      points.push(point);      
    });
  });
  
  onChange();
  checkBox.addEventListener("change", onChange);
  gsap.ticker.add(update);
  
  function onChange() {
    showPoints = checkBox.checked; 
    dotContainer.style.setProperty("visibility", showPoints ? "visible" : "hidden");
  }
  
  function update() {
    
    if (!showPoints) {
      return;
    }
    
    for (let i = 0; i < points.length; i++) {
      let p = points[i];
      dots[i].setAttribute("transform", "translate(" + p.x + "," + p.y + ")");
    }
  }
}


              
            
!
999px

Console