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 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

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

              
                <div class="pointer">
	<img src="https://assets.codepen.io/756881/pointer.svg" />
	<p class="screen-log">transform: translate(0%,0%)</p>
</div>

              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Signika:wght@700&display=swap");

:root {
	--mouse-x: 0;
	--mouse-y: 0;
}

body {
	min-height: 100vh;
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: #6fdcbf;
	font-family: "Signika", sans-serif;
	overflow: hidden;
}

.pointer {
	position: relative;
	transform: translate(var(--mouse-x), var(--mouse-y));
	text-align: center;
}

.pointer img {
	width: 100px;
	display: block;
	margin: 0 auto;
}

.screen-log {
	position: absolute;
	bottom: -3rem;
	width: 300%;
	left: -100%;
	padding-top: 1rem;
	text-align: center;
}
              
            
!

JS

              
                (function () {
	const safeToAnimate = window.matchMedia('(prefers-reduced-motion: no-preference)').matches;
	
 if (!safeToAnimate) return;
	
	let screenLog = document.querySelector('.screen-log');
	
	let xPosition;
	let yPosition;

	let storedXPosition;
	let storedYPosition;

	let windowHeight = window.innerHeight;
	let windowWidth = window.innerWidth;

	function percentage(partialValue, totalValue) {
		return (100 * partialValue) / totalValue;
	}

	// update the CSS vars within request animation frame
	function movePointer() {
		window.requestAnimationFrame(movePointer);
	
		// important, only recalculating if the value changes
		if (storedXPosition === xPosition && storedYPosition === yPosition) return;

		// shift the range from 0 to 100 to -50 to 50 to keep the movement centralised
		x = percentage(xPosition, windowWidth) - 50;
		y = percentage(yPosition, windowHeight) - 50;

		// log the values out to the screen
		if(x && y){ 
			screenLog.innerText = `transform: translate(${Math.round(x)}%, ${Math.round(y)}%)`;
		}

		// update the css vars
		document.documentElement.style.setProperty("--mouse-x", `${x}%`);
		document.documentElement.style.setProperty("--mouse-y", `${y}%`);

		// update the stored positions with the current positions
		storedXPosition = xPosition;
		storedYPosition = yPosition;
	}
	window.requestAnimationFrame(movePointer);

	// updating the mouse coordinates
	function updateMouseCoords(event) {
		xPosition = event.clientX;
		yPosition = event.clientY;
	}
	window.addEventListener("mousemove", updateMouseCoords);

	// update if browser resizes
	function updateWindowSize() {
		windowHeight = window.innerHeight;
		windowWidth = window.innerWidth;
	}
	window.addEventListener("resize", updateWindowSize);
})();

              
            
!
999px

Console