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

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

              
                .frame
	.card
		img.background-img(src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/544318/hills.jpeg")
              
            
!

CSS

              
                body
	height: 100vh
	background: linear-gradient(lightgray,  darkgray)
.frame
	position: absolute
	top: 50%
	left: 50%
	width: 600px
	height: 400px
	perspective: 1000px
	transform: translateX(-50%) translateY(-50%)
	.card
		position: relative
		display: flex
		align-items: center
		justify-content: center
		width: 100%
		height: 100%
		border-radius: 5px
		background: white
		box-shadow: 0 0 2px rgba(30,30,20,0.5)
		transform-origin: 50% 50%
		overflow: hidden
		img
			display: block
			position: relative
			width: 150%
			height: auto
			transform-origin: 50% 50%

              
            
!

JS

              
                "use strict";

const MAX_ROT = 30;
const ANIM_SPEED = 0.075;

let frame, card, img, mouse, userPos, hover, padding, center;

function lerp(n1, n2, speed) {
	return (1 - speed) * n1 + speed * n2;
}

Array.prototype.lerp = function(target, speed) {
	this.forEach((n, i) => this[i] = lerp(n, target[i], speed));
};

function resize() {
	let rect = card.getBoundingClientRect();
	center = [0.5 * card.clientWidth + rect.left, 0.5 * card.clientHeight + rect.top];
	userPos = [
		center[0],
		center[1]
	];
}

function loop() {
	let xPosNorm, yPosNorm, mouseDistNorm, theta, xRot, yRot, imgX, imgY;
	
	userPos.lerp(hover ? mouse : center, ANIM_SPEED);
	
	xPosNorm = (center[0] - userPos[0]) / center[0];
	yPosNorm = (center[1] - userPos[1]) / center[1];
	
	imgX = xPosNorm * padding[0];
	imgY = yPosNorm * padding[1];
	
	mouseDistNorm = Math.sqrt(xPosNorm * xPosNorm + yPosNorm * yPosNorm);
	theta = Math.atan2(userPos[1] - center[1], userPos[0] - center[0]);
	
	xRot = Math.sin(-theta) * MAX_ROT * mouseDistNorm; //card x-axis rotation based on mouse y-axis position
	yRot = Math.cos(theta) * MAX_ROT * mouseDistNorm;  
	
	card.style.transform = `rotateX(${xRot}deg) rotateY(${yRot}deg)`;
	img.style.transform = `translateX(${imgX}px) translateY(${imgY}px)`;
	window.requestAnimationFrame(loop);
}

function init() {
	frame = document.querySelector(".frame");
	card = document.querySelector(".card");
	img = document.querySelector(".background-img");
	mouse = [0, 0];
	hover = false;
	padding = [
		0.5 * (img.clientWidth - card.clientWidth),
		0.5 * (img.clientHeight - card.clientHeight)
	];

	resize();

	frame.addEventListener("mousemove", e => {
		mouse[0] = e.clientX;
		mouse[1] = e.clientY;
		hover = true;
	});

	frame.addEventListener("mouseleave", () => {
		hover = false;
	});
	loop();
}

window.onresize = resize;
window.onload = init;

              
            
!
999px

Console