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

              
                <p>Drag and throw me around :)</p>

<svg>
  <g></g>
  <circle r="80" />
</svg>
              
            
!

CSS

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

path {
  fill: none;
  stroke: red;
  stroke-linecap: round;
  stroke-linejoin: round;
}

circle {
  fill: red;
}

p {
  margin: 10px;
  color: #999;
}
              
            
!

JS

              
                

var svgns = "http://www.w3.org/2000/svg";
var arms = document.querySelector("g");
var head = document.querySelector("circle");

var numTentacles = 10;
var strokeWidth = 20;
var radius = (head.getBBox().width - strokeWidth) * 0.35;

TweenLite.set(head, {
  x: window.innerWidth  / 2,
  y: window.innerHeight / 2,
  fill: "hsl(0,80%," + random(50, 70) + "%)"
});

var dragger = new Draggable(head, {
  bounds: window,
  throwProps: true,
  onDrag: update,
  onThrowUpdate: update
});

var tentacles = [];

for (var i = 0; i < numTentacles; i++) {  
  var stroke = "hsl(0,80%," + random(50, 70) + "%)";  
  tentacles.push(createTentacle(stroke));
}

TweenLite.to(head, 6, {
  y: 100,
  onUpdate: function() {
    dragger.update();
    update();
  }
});

function createTentacle(stroke) {
  
  var path = document.createElementNS(svgns, "path");
  arms.appendChild(path);  
  TweenLite.set(path, { stroke: stroke, strokeWidth: strokeWidth })
  
  var points = [];  
  var total = random(30, 50);
  var space = random(10, 15);
  var angle = random(Math.PI * 2);

  for (var i = 0; i < total; i++) {
    points.push({ x: dragger.x, y: dragger.y });
  }  
  
  return {
    path: path,
    total: total,
    space: space,
    points: points,
    offsetX: Math.cos(angle) * radius,
    offsetY: Math.sin(angle) * radius
  };
}

function update() {
  
  for (var tentacle of tentacles) {
  
    var points = tentacle.points;
    var total = tentacle.total;
    var space = tentacle.space;            
    var first = points[0];
    
    first.x = dragger.x + tentacle.offsetX;
    first.y = dragger.y + tentacle.offsetY;
    
    for (var i = 0; i < total - 1; i++) {
    
      var point = points[i];
      var next  = points[i + 1];
      
      var dx = point.x - next.x;
      var dy = point.y - next.y;

      var magnitude = Math.sqrt(dx * dx + dy * dy);

      if (magnitude) {
        dx /= magnitude;
        dy /= magnitude;
      }

      dx *= space;
      dy *= space;

      next.x = point.x - dx;
      next.y = point.y - dy;
    }

    tentacle.path.setAttribute("d", catmullRom(points, false, 0.5));       
  }
}

// Path smoothing
function catmullRom(data, closed, alpha) {
    
  if (data.length < 1) return "";
  if (alpha == null) alpha = 0.5;
  
  var size = data.length - (closed ? 0 : 1);
  var path = `M${data[0].x},${data[0].y} C`;
  
  for (var i = 0; i < size; i++) {
    
    var 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];
    }
        
    var d1 = Math.sqrt(Math.pow(p0.x - p1.x, 2) + Math.pow(p0.y - p1.y, 2));
    var d2 = Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
    var d3 = Math.sqrt(Math.pow(p2.x - p3.x, 2) + Math.pow(p2.y - p3.y, 2));
    
    var d3powA  = Math.pow(d3, alpha);
    var d3pow2A = Math.pow(d3, 2 * alpha);
    
    var d2powA  = Math.pow(d2, alpha);
    var d2pow2A = Math.pow(d2, 2 * alpha);
    
    var d1powA  = Math.pow(d1, alpha);
    var d1pow2A = Math.pow(d1, 2 * alpha);
    
    var A = 2 * d1pow2A + 3 * d1powA * d2powA + d2pow2A;
    var B = 2 * d3pow2A + 3 * d3powA * d2powA + d2pow2A;
    var N = 3 * d1powA * (d1powA + d2powA);
    
    if (N > 0) N = 1 / N;        
    var M = 3 * d3powA * (d3powA + d2powA);    
    if (M > 0) M = 1 / M;
        
    var x1 = (-d2pow2A * p0.x + A * p1.x + d1pow2A * p2.x) * N;
    var y1 = (-d2pow2A * p0.y + A * p1.y + d1pow2A * p2.y) * N;

    var x2 = (d3pow2A * p1.x + B * p2.x - d2pow2A * p3.x) * M;
    var y2 = (d3pow2A * p1.y + B * p2.y - d2pow2A * p3.y) * M;
        
    if (x1 == 0 && y1 == 0) {
      x1 = p1.x;
      y1 = p1.y;
    }
    
    if (x2 == 0 && y2 == 0) {
      x2 = p2.x;
      y2 = p2.y;
    }
        
    path += ` ${x1},${y1},${x2},${y2},${p2.x},${p2.y}`;
  }
  
  return closed ? path + "z" : path;  
}

function random(min, max) {
  if (max == null) { max = min; min = 0; }
  return Math.random() * (max - min) + min;
}


              
            
!
999px

Console