Pen Settings

HTML

CSS

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

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

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.

HTML

              
                <h1>You need to transform screen coordinates when using them in a SVG</h1>
<div class="flex">
  <div class="half-width">
    <svg
      width="100%"
      height="300"
      viewBox="0 0 1055 581"
      version="1.1"
      xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink"
    >
      <path stroke="black" fill="none" d="M 117 52 L 483 161 697 168"></path>
      <circle cx="117" cy="52" r="15"></circle>
      <text x="117" y="22">(117,52)</text>
      <circle cx="483" cy="161" r="15"></circle>
      <text x="483" y="131">(483,161)</text>
      <circle cx="697" cy="168" r="15"></circle>
      <text x="697" y="138">(697,168)</text>
      <text id="dynText" x="0" y="-20"></text>
      <circle
        fill="hsla(120,50%,85%,.5)"
        r="15"
        id="svgClientX"
        stroke="hsla(120,50%,45%,.75)"
      />
    </svg>
  </div>
  <div>
    <div class="flex flex--fixed">
      <input type="checkbox" class="ctm" />
      <h2>Use CTM transform</h2>
    </div>
    <div class="usedCode"><em>Try to move mouse over SVG drawing</em></div>
  </div>
  <div class="container"></div>
</div>

              
            
!

CSS

              
                * {
	box-sizing: border-box;
}
html,
body {
  font-family: sans-serif;
  width: 100%;
  height: 100%;
  padding: 5px;
  margin: 0;
  font-size: 12pt;
}
h1 {
	font-weight: 100;
}
h2 {
	line-height: 100%;
	margin: 5px 0 0;
}
.usedCode {
 font-family: monospace;
}

.flex {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: flex-start;
}
.flex--vertical {
  flex-direction: column;
}
.row {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  width: 100%;
  margin: 5px 0;
}
.half-width {
  width: 50%;
	padding: 15px;
}

svg {
  outline: solid 1px black;
}

input {
  padding: 0.5em;
  width: 2em;
  height: 2em;
}

input[type="checkbox"] {
  display: block;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #cca92c;
  cursor: pointer;
  box-shadow: 0 0 0 rgba(204, 169, 44, 0.4);
  animation: pulse 2s infinite;
	align-self: center;
	margin-right: 10px;
}
input[type="checkbox"]:hover,
input[type="checkbox"]:checked {
  animation: none;
}

@-webkit-keyframes pulse {
  0% {
    -webkit-box-shadow: 0 0 0 0 rgba(204, 169, 44, 1);
  }
  70% {
    -webkit-box-shadow: 0 0 0 10px rgba(204, 169, 44, 0);
  }
  100% {
    -webkit-box-shadow: 0 0 0 0 rgba(204, 169, 44, 0);
  }
}
@keyframes pulse {
  0% {
    -moz-box-shadow: 0 0 0 0 rgba(204, 169, 44, 1);
    box-shadow: 0 0 0 0 rgba(204, 169, 44, 0.4);
  }
  70% {
    -moz-box-shadow: 0 0 0 10px rgba(204, 169, 44, 0);
    box-shadow: 0 0 0 10px rgba(204, 169, 44, 0);
  }
  100% {
    -moz-box-shadow: 0 0 0 0 rgba(204, 169, 44, 0);
    box-shadow: 0 0 0 0 rgba(204, 169, 44, 0);
  }
}

@media (max-width: 1000px) {
	.flex:not(.flex--fixed) {
  display: flex;
  flex-direction: column;
}
	svg {
		height: 120px;
	}
}
              
            
!

JS

              
                const optimizedResize = (function() {

    var callbacks = [],
        running = false;

    // fired on resize event
    function resize() {
        if (!running) {
            running = true;
            if (window.requestAnimationFrame) {
                window.requestAnimationFrame(runCallbacks);
            } else {
                setTimeout(runCallbacks, 66);
            }
        }

    }

    // run the actual callbacks
    function runCallbacks() {

        callbacks.forEach(function(callback) {
            callback();
        });

        running = false;
    }

    // adds callback to loop
    function addCallback(callback) {

        if (callback) {
            callbacks.push(callback);
        }

    }

    return {
        // public method to add additional callback
        add: function(callback) {
            if (!callbacks.length) {
                window.addEventListener('resize', resize);
            }
            addCallback(callback);
        }
    }
}());


function onMouseMove(evt) {
	const {clientX, clientY} = evt || {clientX: 0, clientY: 0}
	point.x = clientX;
	point.y = clientY;
	let useCTMTransform = '';
	let transformedCoordinate = `(${Math.round(point.x)}, ${Math.round(point.y)})`;
	if (ctm.checked) {
		point = point.matrixTransform(svg.getScreenCTM().inverse());
		transformedCoordinate = `(${Math.round(point.x)}, ${Math.round(point.y)})`;
		useCTMTransform = `\n  point = point.matrixTransform(eleSvg.getScreenCTM().inverse());
  // point = ${transformedCoordinate}`
	}
	eleDynText.setAttribute('x', point.x+15);
	eleDynText.setAttribute('y', point.y+25);
	eleDynText.innerHTML = transformedCoordinate;
	svgClientX.setAttribute('cx', point.x);
	svgClientX.setAttribute('cy', point.y);
	eleCode.innerHTML = `<pre>const eleSvg = document.querySelector('svg');
eleSvg.addEventListener('mousemove', ({clientX, clientY}) => {
  let point = eleSvg.createSVGPoint();
  point.x = clientX; // ${clientX}
  point.y = clientY; // ${clientY}${useCTMTransform}\n})</pre>`
}

var ctm = document.querySelector('.ctm'),
		svg = document.querySelector('svg'),
		ctmcode = document.querySelector('.ctmcode'),
		eleCode = document.querySelector('.usedCode'),
		eleDynText = document.querySelector('#dynText');

svg.addEventListener('mousemove', onMouseMove);
var point = svg.createSVGPoint();
onMouseMove();
              
            
!
999px

Console