Pen Settings



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


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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


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.


                <h1>GSAP 3 getRelativePosition() Demo</h1><div id="outer">
  <div id="middle">
    <div id="inner"></div>
<div id="dot-container">
  <div id="dot"></div>

<link href='//,400' rel='stylesheet' type='text/css'>


                body {
  background-color: black;
  color: white;
  font-family: "Signika Negative", Arial, sans-serif;
  font-weight: 300;
  padding: 5px 15px;
#dot {
  position: absolute;
  top: 0;
  left: 0;
  width: 16px;
  height: 16px;
  background-color: red;
  border-radius: 50%;
#dot-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100px;
  height: 50px;
  border: 2px solid green;

#outer {
  width: 500px;
  height: 350px;
  background-color: #444;
#middle {
  width: 300px;
  height: 200px;
  background-color: #656565;
#inner {
  width: 100px;
  height: 60px;
  background-color: #ccc;


                // The red dot goes to the center of the light rectangle even though it's nested inside multiple elements with various transforms applied!

// First, let's add some wacky transforms to every element (nested!)
var tl = gsap.timeline({defaults: {duration:1, delay:0.5, ease: "power1.inOut"}})
  .to("#outer", {xPercent: 50, yPercent: 20, rotation: -20, y: -80})
  .to("#middle", {x: 100, y: 50, scale: 0.7, rotation: 60})
  .to("#inner", {x: 140, y: 100, transformOrigin: "50% 50%", rotation: 40})
  .to("#dot-container", {x:50, y:80, scale:0.5, rotation:-25})
  .call(function() {
    var inner = document.querySelector("#inner"),
        dot = document.querySelector("#dot"),
        delta = getRelativePosition(dot, inner, [0.5, 0.5], [0.5, 0.5]);, {
      x: "+=" + delta.x,
      y: "+=" + delta.y,
      duration: 2,
      delay: 1,
      repeat: -1,
      yoyo: true,
      ease: "power2.inOut"

// feed two elements to this method and it'll return the gap between them as a point {x, y} according to the coordinate system of the fromElement's parent. By default, it will align their centers, but you can define a different origin for each, like [0, 0] would be the top left corner, [0.5, 0.5] would be the center, [1, 1] would be the bottom right, etc. 
function getRelativePosition(fromElement, toElement, fromOrigin, toOrigin) {
	let matrix = MotionPathPlugin.getGlobalMatrix,
		originToPoint = (element, origin) => {
			let o = origin || [0.5, 0.5],
				svg = element.ownerSVGElement && element.getBBox(),
				w = svg ? svg.width : element.offsetWidth || 0,
				h = svg ? svg.height : element.offsetHeight || 0;
			return {x: o[0] * w, y: o[1] * h};
		parentMatrix = matrix(fromElement.parentNode, true),
		fromPoint = parentMatrix.apply(matrix(fromElement).apply(originToPoint(fromElement, fromOrigin))),
		toPoint = parentMatrix.apply(matrix(toElement).apply(originToPoint(toElement, toOrigin)));
	return {x: toPoint.x - fromPoint.x, y: toPoint.y - fromPoint.y};