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 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

              
                <svg id="svg">
  <g id="group">
    <path id="path" d="M37.7,10C17.2,21.2-4.9,46.9,1,65.9c8.9,28.9,73.4,13,103.9,56c20.9,29.4,7.8,60.7,19.2,63.9
	c17.5,5,77.1-61.1,57.6-120.7C163.3,9.3,83.9-15.4,37.7,10z" />    
    <rect id="rect" x="0" y="0" width="50" height="50" />
  </g> 
  <g id="boxes"></g>
</svg>
              
            
!

CSS

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

* {
  vector-effect: non-scaling-stroke;
}

#path,
#rect {
  stroke: #000;
  stroke-width: 2;
  stroke-opacity: 0.3;
}

#path {
  fill: #81c784;
}

#rect {
  fill: #00b8d4;
  fill-opacity: 0.8;
}
              
            
!

JS

              
                console.clear();

var boxes = document.querySelector("#boxes");
var group = document.querySelector("#group");
var path  = document.querySelector("#path");
var rect  = document.querySelector("#rect");

TweenLite.defaultEase = Power1.easeInOut;
TweenLite.set([group, path, rect], { transformOrigin: "center" });

var tl = new TimelineMax({ repeat: -1, repeatDelay: 2, yoyo: true, paused: true })
  .from(group, 3, { rotation: 90, x: 500, y: 50 }, 0)
  .from(path, 3, { rotation: 90, x: 200, y: 200 }, 0)
  .from(rect, 3, { rotation: 45, x: 300, y: 500 }, 0);

// Bounds without transforms - red
var box1 = getBBox(path, true);
var box2 = getBBox(rect, true);

// Bounds with transforms - green
var box3 = getBBox(path);
var box4 = getBBox(rect);
var box5 = getBBox(group);

showBounds(box5, "rgba(46,125,50,0.3)", 3);

showBounds(box1, "#d50000", 3);
showBounds(box2, "#d50000", 3);

showBounds(box3, "#2e7d32", 3);
showBounds(box4, "#2e7d32", 3);

tl.play();

/**
 * @param {SVGElement} element - Element to get the bounding box for
 * @param {boolean} [withoutTransforms=false] - If true, transforms will not be calculated
 * @param {SVGElement} [toElement] - Element to calculate bounding box relative to
 * @returns {SVGRect} Coordinates and dimensions of the real bounding box
 */
function getBBox(element, withoutTransforms, toElement) {
  
  var svg = element.ownerSVGElement;
  
  if (!svg) {
    return { x: 0, y: 0, width: 0, height: 0 };
  }
  
  if (withoutTransforms) {
    return element.getBBox();
  }
  
  var p = svg.createSVGPoint();
  
  var bounds = getBounds(element, toElement || svg);
  
  return {
    x: bounds.minX,
    y: bounds.minY,
    width: bounds.maxX - bounds.minX,
    height: bounds.maxY - bounds.minY
  };    
  
  function getBounds(node, toElement) {
            
    var minX, minY, maxX, maxY;
    var children = node.children;
    if (!node.children) {
      var i = 0, child, nodes = node.childNodes, children = [];
      while (child = nodes[i++]) {
        if (child.nodeType === 1) {
          children.push(child);
        }
      }
    }
    var i = children && children.length;    
    var r = null;
        
    if (i && node.tagName.toUpperCase() === "G") {
            
      minX =  Infinity;
      minY =  Infinity;
      maxX = -Infinity;
      maxY = -Infinity;
      
      while (i--) {
        
        var bounds = getBounds(children[i], node);
        
        if (bounds.minX < minX) minX = bounds.minX;
        if (bounds.minY < minY) minY = bounds.minY;
        if (bounds.maxX > maxX) maxX = bounds.maxX;
        if (bounds.maxY > maxY) maxY = bounds.maxY;       
      }      
      
      r = {
        x: minX,
        y: minY,
        width: maxX - minX,
        height: maxY - minY
      };      
    }
          
    if (!r) {
      r = node.getBBox();
    }
    
    var matrix = (toElement || svg).getScreenCTM().inverse().multiply(node.getScreenCTM()); 

    p.x = r.x;
    p.y = r.y;
    var a = p.matrixTransform(matrix);

    p.x = r.x + r.width;
    p.y = r.y;
    var b = p.matrixTransform(matrix);

    p.x = r.x + r.width;
    p.y = r.y + r.height;
    var c = p.matrixTransform(matrix);

    p.x = r.x;
    p.y = r.y + r.height;
    var d = p.matrixTransform(matrix);

    minX = Math.min(a.x, b.x, c.x, d.x);
    maxX = Math.max(a.x, b.x, c.x, d.x);
    minY = Math.min(a.y, b.y, c.y, d.y);
    maxY = Math.max(a.y, b.y, c.y, d.y);
    
    return {
      minX: minX || 0,
      minY: minY || 0,
      maxX: maxX || 0,
      maxY: maxY || 0
    }; 
  }
}

function showBounds(bounds, stroke, strokeWidth) {
  
  var element = document.createElementNS("http://www.w3.org/2000/svg", "rect");
  boxes.appendChild(element);
  
  TweenLite.set(element, {
    attr: {
      x: bounds.x,
      y: bounds.y,
      width: bounds.width,
      height: bounds.height,
    },
    fill: "none",
    stroke: stroke || Math.random() * 0xffffff,
    strokeWidth: strokeWidth || 2,
  });
}
              
            
!
999px

Console