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

              
                <div>
  <div class="svg-floater">
<svg width="400px" height="350px" id="svg">
  <circle id="cursor-tracker" cx="0", cy="0" r="10"/>
   <polygon id="pointer" points="180,350 200,300 220,350" style="fill:red;stroke-width:1" />
  <line id="dx" x1="0" y1="0" x2="200" y2="0" />
  <line id="dy" x1="200" y1="0" x2="200" y2="350"/>
</svg>
  </div>
  <div class="labels">
    <div id="x-label"><strong>Opposite</strong>(dx): <span id="dx-txt">200</span></div>
    <div id="y-label"><strong>Adjacent</strong>(dy): <span id="dy-txt">350</span></div>
  </div>
</div>
              
            
!

CSS

              
                html {
  width: 100%;
  position: relative;
  font-family: sans-serif;
}

.svg-floater, .labels {
  width: 400px;
  margin: 0px auto;
  touch-action: none;
}

.labels div {
  display: inline;
  width: 50%;
  float: right;
}

line {
  stroke-dasharray: 1 5;
  stroke-width: 3;
  stroke-linecap: round;
  stroke: black;
}
              
            
!

JS

              
                // Collect all our items that we need
var circle = document.getElementById('cursor-tracker');
var svg = document.getElementById('svg');
var dxLine = document.getElementById('dx');
var dyLine = document.getElementById('dy');
var dxTxt = document.getElementById('dx-txt');
var dyTxt = document.getElementById('dy-txt');

// Define some variables
var arrowLoc = {
  x: 200,
  y: 350
}

svg.addEventListener('mousemove', function(event) {
  //console.log(event.clientX, event.clientY);
  updateComponents(event.clientX, event.clientY);
});

svg.addEventListener('touchmove', function(event) {
  //console.log(event);
  updateComponents(event.changedTouches[0].clientX, event.changedTouches[0].clientY);
})


function updateComponents(x, y) {
  // First, we need to do a coordinate change because the x and y origin of the SVG is offset from that of the page.
  x -= 5 + svg.getBoundingClientRect().x;
  y -= 5 + svg.getBoundingClientRect().y;
  
  // Now we move the circle as well as the lines so they demonstrate the triangle.
  circle.setAttribute("cx", x);
  circle.setAttribute("cy", y);
  dxLine.setAttribute("x1", x);
  dxLine.setAttribute("y1", y);
  dxLine.setAttribute("y2", y);
  dyLine.setAttribute("y1", y);
  
  // Now round the values and display them
  dxTxt.innerHTML = Math.round(x - arrowLoc.x);
  dyTxt.innerHTML = Math.round(y - arrowLoc.y);
}
              
            
!
999px

Console