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="right" class="active" height="600px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" data-pos="right" style="" viewBox="0 0 1000 600"><image xlink:href="http://varsitycart.presentation.site/image/catalog/Jackets/Black-Burnt-Orange-Burnt-Orange_mens_varsity_sright-ide.jpg" x="0" y="0" height="100%" width="100%"></image>
   <line id="line" x1="480" y1="150" x2="500" y2="350" stroke="black"></line>
  <g id="patch_218_454">

  <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="426" y="160" width="108" height="72" viewBox="-0.199 -0.234 600.482 600.482" enable-background="new -0.199 -0.234 600.482 600.482" xml:space="preserve" class="right-shoulder" data-patch-height="3.00" data-patch-width="4.50" id="box">
<g id="Layer_1">
  <polygon id="Bottom_Felt" fill="url(#Layer_1_4)" stroke="none" stroke-width="0" stroke-miterlimit="10" points="197.429,25.006 
    425.629,25.006 425.629,186.788 415.179,186.788 509.211,399.651 574.825,399.651 574.825,575.006 325.321,575.006 
    325.321,427.326 270.156,427.326 270.156,574.542 25.258,574.542 25.258,399.186 88.369,399.186 179.897,186.292 167.342,186.292 
    167.342,25.006 197.027,25.006   " fill-rule="evenodd"></polygon>
<defs><pattern id="Layer_1_4" patternUnits="userSpaceOnUse" width="100" height="100"><image xlink:href="http://varsitycart.presentation.site/image/colors/5b0c527b85f12.png" width="100" height="100" x="0" y="0"></image></pattern></defs></g>
<g id="Layer_2">
  <path id="Top_Felt" fill="url(#Layer_2_5)" stroke="none" stroke-width="0" stroke-miterlimit="10" d="M222.507,44.704H408.5v131.292
    h-16.946l106.154,242.641h59.987v136.921H342.45V418.636h31.57l-3.152-11.008H227.732l-4.607,10.792h29.685v136.425H42.605V418.636
    h56.215l105.504-242.641h-19.418V44.92h37.384L222.507,44.704z M299.625,235.056l27.985,54.391h-55.566l27.798-54.391H299.625z" fill-rule="evenodd"></path>
<defs><pattern id="Layer_2_5" patternUnits="userSpaceOnUse" width="100" height="100"><image xlink:href="http://varsitycart.presentation.site/image/colors/5b0c59744698f.png" width="100" height="100" x="0" y="0"></image></pattern></defs></g>
<g id="Layer_3">
  <path id="Attaching_Chain" fill="url(#Layer_3_6)" stroke="none" stroke-width="0" stroke-miterlimit="10" d="M200.583,55.495h198.484
    v109.462H376.31l115.121,264.007h56.647v115.8H352.469V428.5h33.644l-7.915-31.416H221.053L209.767,428.5h33.858v115.584H52.222
    v-115.12h52.659L219.6,164.957h-25.294V55.495l33.024-0.248L200.583,55.495z M255.314,300.021l44.527-86.054l44.28,85.807
    l-89.023,0.247l16.512-31.199L255.314,300.021z" fill-rule="evenodd"></path>
<defs><pattern id="Layer_3_6" patternUnits="userSpaceOnUse" width="100" height="100"><image xlink:href="http://varsitycart.presentation.site/image/colors/5b0c59746fff9.png" width="100" height="100" x="0" y="0"></image></pattern></defs></g>
<g id="Layer_4">
  <path id="Fill" fill="url(#Layer_4_7)" stroke="none" stroke-width="0" stroke-miterlimit="10" d="M297.738,207.041l-51.113,98.669
    H352.81l-52.443-101.917l-2.628,3.216V207.041z M199.47,61.247h194.433v98.299h-26.098L488.74,434.932h54.608v104.36H357.294
    v-104.36l35.715-0.68l-11.103-43.105l-164.409,1.361l-15.615,41.744h36.611v104.359H57.263v-103.68h50.401l120.038-275.386H199.47
    V61.247L199.47,61.247z" fill-rule="evenodd"></path>
<defs><pattern id="Layer_4_7" patternUnits="userSpaceOnUse" width="100" height="100"><image xlink:href="http://varsitycart.presentation.site/image/colors/5b0c59744395f.png" width="100" height="100" x="0" y="0"></image></pattern></defs></g>
</svg>
</g></svg>
              
            
!

CSS

              
                
              
            
!

JS

              
                var box = document.querySelector("#box");
var line = document.querySelector("#line");


var x1 = line.x1.baseVal.value || 0;
var y1 = line.y1.baseVal.value || 0;
var x2 = line.x2.baseVal.value || 0;
var y2 = line.y2.baseVal.value || 0;

var C = x2 - x1;
var D = y2 - y1;

var distSq  = C * C + D * D;


TweenLite.set(box, {
  xPercent: -50,
  yPercent: -50
});

var draggable = new Draggable(box, {
  allowContextMenu: true,
  throwProps: true,
   // bounds:{minX:-14, maxX:16, minY:0, maxY:120},
  liveSnap: {
    points: getClosestPoint
  }  
});

TweenLite.set(box, getClosestPoint({ 

  x: draggable.x, 
  y: draggable.y 
}));

draggable.update(true)

function getClosestPoint(point) {
  
  var A = point.x - x1;
  var B = point.y - y1;

  var dotProd = A * C + B * D;
  var determ  = dotProd / distSq;

  var x, y;

  if (determ < 0 || (x1 === x2 && y1 === y2)) {
    x = x1;
    y = y1;

  } else if (determ > 1) {
    x = x2;
    y = y2;

  } else {
    x = x1 + determ * C;
    y = y1 + determ * D;        
  }
  

  return {
    x: x,
    y: y
  };
}
              
            
!
999px

Console