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 500 500">
 <polygon id="target1" points="83.97 253.74 87.35 260.6 94.91 261.69 89.44 267.03 90.73 274.56 83.97 271.01 77.2 274.56 78.49 267.03 73.02 261.69 80.58 260.6 83.97 253.74" />
  <polygon id="target2" points="283.45 253.74 286.67 260.27 293.88 261.32 288.66 266.4 289.89 273.58 283.45 270.19 277 273.58 278.23 266.4 273.02 261.32 280.23 260.27 283.45 253.74" />
  <path id="star1" d="M87.07,76l3.38,6.85L98,84l-5.48,5.34,1.3,7.53-6.77-3.56L80.3,96.82l1.3-7.53L76.12,84l7.57-1.1Z" />
  <path id="star2" d="M114.74,78.53,118,85.06l7.2,1L120,91.19l1.23,7.18L114.74,95l-6.44,3.39,1.23-7.18-5.22-5.08,7.21-1Z" />
</svg>
              
            
!

CSS

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

#star1 {
  fill: #0000b7;
  stroke: #0000b7;
}

#star2 {
  fill: #06ba17;
  stroke: #06ba17;
}

#star1, #star2 {
  fill-opacity: 0.3;
  vector-effect: non-scaling-stroke;
}

#target1, #target2 {
  fill: #bbb;
}


              
            
!

JS

              
                console.clear();

var target1 = document.querySelector("#target1");
var target2 = document.querySelector("#target2");
var star1 = document.querySelector("#star1");
var star2 = document.querySelector("#star2");

// Make your life easier
TweenLite.set([target1, target2, star1, star2], {
  transformOrigin: "center"
});

TweenLite.set(target1, {
  x: 100,
  y: -25,
  rotation: 45,
  scale: 2
});

TweenLite.set(target2, {
  x: 50,
  y: -50,
  rotation: -130,
  scale: 2.5
});

animateStar(star1, target1);
animateStar(star2, target2);

function animateStar(star, target) {
   
  var box1 = getBBox(star);
  var box2 = getBBox(target);
  
  // cx and cy are centerX and centerY
  var x = box2.cx - box1.cx;
  var y = box2.cy - box1.cy;
    
  // GSAP object on element to get transform values
  var transform = target._gsTransform;
  
  return TweenMax.to(star, 3, {
    x: x,
    y: y,
    scaleX: transform.scaleX,
    scaleY: transform.scaleY,
    rotation: transform.rotation + "_short",
    ease: Power1.easeInOut,
    repeat: -1,
    repeatDelay: 0.5,
    yoyo: true
  });  
}

// Posted here
// https://greensock.com/forums/topic/13681-svg-gotchas/?page=2&tab=comments#comment-72060

/**
 * @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, cx: 0, cy: 0, width: 0, height: 0 };
  }
  
  var r = element.getBBox(); 
  
  if (withoutTransforms) {
    return {
      x: r.x,
      y: r.y,
      width: r.width,
      height: r.height,        
      cx: r.x + r.width / 2,
      cy: r.y + r.height / 2
    };
  }
  
  var p = svg.createSVGPoint(); 
      
  var matrix = (toElement || svg).getScreenCTM().inverse().multiply(element.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);

  var minX = Math.min(a.x, b.x, c.x, d.x);
  var maxX = Math.max(a.x, b.x, c.x, d.x);
  var minY = Math.min(a.y, b.y, c.y, d.y);
  var maxY = Math.max(a.y, b.y, c.y, d.y);
    
  var width = maxX - minX;
  var height = maxY - minY;
  
  return {
    x: minX,
    y: minY,
    width: width,
    height: height,        
    cx: minX + width / 2,
    cy: minY + height / 2
  };
}

              
            
!
999px

Console