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

              
                <button onclick="do1();">Move 1</button><br/>
<button onclick="do2();">Move 2</button><br/>
<button onclick="do3();">Move 3</button><br/>
<button onclick="do4();">Move 4</button><br/>
<button onclick="do5();">Move 5</button><br/>
<button onclick="do6();">Move 6</button><br/>
<button onclick="do7();">Move 7</button><br/>
<button onclick="do8();">Move 8</button><br/>
<svg id="elSVG" viewBox="0 0 420 620" preserveAspectRatio="xMinYMin meet">
   <rect id="rect1" x="23" y="10" width="50" height="50" fill="#0000cc" />
   <rect id="rect2" x="360" y="230" width="50" height="50" fill="#00cc00" />
   <rect id="rect3" x="143" y="412" width="50" height="50" fill="#cc0000" />  
</svg>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="divLine"></div>
<div id="divSquare"></div>
              
            
!

CSS

              
                @charset "utf-8";	
	*{box-sizing: border-box;}

	#elSVG
	{
	  position: absolute;
	  left:25%;
	  top:15%;
	  width:22%;
	  height:50%;    
	  outline: 2px solid red;
    
    overflow: visible;
	}
	
	#div1 /*red*/
	{
		position:absolute;
		left:32%;
		top:85%;
		width:6px;		
		height:6px;
		border: solid 2px #CC0000;
		border-radius:50%;
		background-color:#FF6666;
		background-opacity:.3;
	}
	
	#div2  /*blue*/
	{
		position:absolute;
		left:67%;
		top:15%;
		width:6px;		
		height:6px;
		border: solid 2px #0033FF;
		border-radius:50%;
		background-color:#33CCFF;
		background-opacity:.3;
	}
	
	#div3  /*green*/
	{
		position:absolute;
		left:48%;
		top:41%;
		width:6px;		
		height:6px;
		border: solid 2px #336600;
		border-radius:50%;
		background-color: #66FFCC;
		background-opacity:.3;
	}
	
	#div4  /*orange*/
	{
		position:absolute;
		left:52%;
		top:71%;
		width:6px;		
		height:6px;
		border: solid 2px #FF9933;
		border-radius:50%;
		background-color:#FFCCCC;
		background-opacity:.3;
	}
	
	#divLine
	{
		position:absolute;
    display:none;
		left:326px;
		top:5%;
		width:4px;
		height:90%;
		background-color:#000000;
	}
	
	#divSquare
	{
		position:absolute;
    display:none;
		left:326px;
		top:100%;px;
		width:50px;
		height:50px;
		background-color:#000000;
	}
              
            
!

JS

              
                // Polyfill for getTransformToElement
SVGElement.prototype.getTransformToElement = SVGElement.prototype.getTransformToElement || function(toElement) {
  return toElement.getScreenCTM().inverse().multiply(this.getScreenCTM());  
};

var svg = null;
var rect1 = null;
var rect2 = null;
var rect3 = null;
var dot1 = null;
var dot2 = null;

$(function()
{
	console.clear();  
});

function do1()
{
	//move svg so top left is at blue dot center
  console.clear();
  var elSVG = document.querySelector("#elSVG");
  var rectDot = document.querySelector("#div2").getBoundingClientRect();
  var rectSVG = elSVG.getBoundingClientRect();
  elSVG.style.left = (rectDot.left + (rectDot.width/2)) + "px";
  elSVG.style.top = (rectDot.top + (rectDot.height/2)) + "px";
  
}

function do2()
{
  //move svg so bottom right is at orange dot center
  var elSVG = document.querySelector("#elSVG");
  var rectDot = document.querySelector("#div4").getBoundingClientRect();
 
  elSVG.style.left = rectDot.left + (rectDot.width/2) + "px";
  elSVG.style.top = rectDot.top + (rectDot.height/2) + "px"; 
}

function do3()
{
  //Move svg so green square top left is at green dot center
  var elSVG = document.querySelector("#elSVG");
  var rectSVG = elSVG.getBoundingClientRect();
  var rectDot = document.querySelector("#div3").getBoundingClientRect();
  var rectSq = document.querySelector("#rect2").getBoundingClientRect();
  
  DotX = rectDot.left + (rectDot.width/2);
  DotY = rectDot.top + (rectDot.height/2);
  SqX = rectSq.left - rectSVG.left;
  SqY = rectSq.top - rectSVG.top;
  
  elSVG.style.left = DotX - SqX + "px";
  elSVG.style.top = DotY - SqY + "px";	
}

function do4()
{
	//Move svg so red square bottom right is at red dot center.
  //This doesn't always work exactly (given different screen sizes).
  var elSVG = document.querySelector("#elSVG");
  var rectSVG = elSVG.getBoundingClientRect();  
  var rectDot = document.querySelector("#div1").getBoundingClientRect();   
  var rectSq = document.querySelector("#rect3").getBoundingClientRect();
 
  var DotX = rectDot.left + (rectDot.width/2);
  var DotY = rectDot.top + (rectDot.height/2);
  var SqX = (rectSq.left + rectSq.width) - rectSVG.left;
  var SqY = (rectSq.top + rectSq.height) - rectSVG.top;
   
  elSVG.style.left = DotX - SqX + "px";
  elSVG.style.top = DotY - SqY + "px";  
}

function do5()
{
	//Move orange dot so center is on red square right bottom
  
  var elDot = document.querySelector("#div4");
  var rectDot = elDot.getBoundingClientRect();
  var rectSq = document.querySelector("#rect3").getBoundingClientRect();
  
 
  var sqX = rectSq.left + rectSq.width;
  var sqY = rectSq.top + rectSq.height;
  
  elDot.style.left = (sqX - (rectDot.width/2)) + "px";
  elDot.style.top = (sqY - (rectDot.height/2)) + "px";
  
}

function do6() {
  
  var svg = document.querySelector("#elSVG");
  var box = document.querySelector("#rect2");
  var div = document.querySelector("#div2");
    
  var svgBounds = svg.getBoundingClientRect();
  var boxBounds = box.getBoundingClientRect();
  var divBounds = div.getBoundingClientRect();
  
  var p1 = svg.createSVGPoint();
  var p2 = svg.createSVGPoint();
  
  p1.x = (divBounds.left + divBounds.width / 2) - svgBounds.left;
  p1.y = (divBounds.top + divBounds.height / 2) - svgBounds.top;
  
  p2.x = boxBounds.left - svgBounds.left;
  p2.y = boxBounds.top - svgBounds.top;
  
  var local1 = toLocalPoint(box, p1);
  var local2 = toLocalPoint(box, p2);
  
  var x = local1.x - local2.x;
  var y = local1.y - local2.y;
  
  TweenLite.to(box, 0.4, {
    x: "+=" + x,
    y: "+=" + y 
  });
}

function toLocalPoint(element, point) {
  
  var svg = element.ownerSVGElement;
  var globalPoint = point.matrixTransform(svg.getScreenCTM().inverse());
  var globalToLocalMatrix = element.getTransformToElement(svg).inverse();
  return globalPoint.matrixTransform(globalToLocalMatrix);
}

// function do6()
// {
// 	//Without moving svg, move green square so that top left is at blue dot center
//   var elSVG = document.querySelector("#elSVG");
//   var elSq = document.querySelector("#rect2");
//   var rectDot = document.querySelector("#div2");
// 	var point = elSVG.createSVGPoint();
//   var pntSq = point.matrixTransform(elSq.getScreenCTM().inverse());  
//   //Oh no, what now? I'm getting negative numbers for x & y.
// }

function do7()
{
	//Without moving the SVG, move the red square so the bottom right is center of orange dot	
  	
}

function do8()
{

}

function do9()
{

}
              
            
!
999px

Console