Here's a single HTML file that creates an interactive, squishy cube inspired by Adult Swim's Elastic Man:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Squishy Cube</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
overflow: hidden;
}
#cube {
width: 200px;
height: 200px;
background-color: #3498db;
position: relative;
transition: transform 0.1s ease-out;
transform-style: preserve-3d;
}
#cube > div {
position: absolute;
width: 100%;
height: 100%;
background-color: #3498db;
opacity: 0.8;
}
</style>
</head>
<body>
<div id="cube"></div>
<script>
const cube = document.getElementById('cube');
let isDragging = false;
let startX, startY;
let currentRotationX = 0;
let currentRotationY = 0;
function createCubeFaces() {
const directions = ['front', 'back', 'top', 'bottom', 'left', 'right'];
directions.forEach(dir => {
const face = document.createElement('div');
face.classList.add(dir);
cube.appendChild(face);
});
}
function updateCubeFaces() {
const faces = cube.querySelectorAll('div');
faces[0].style.transform = `translateZ(100px)`; // front
faces[1].style.transform = `rotateY(180deg) translateZ(100px)`; // back
faces[2].style.transform = `rotateX(90deg) translateZ(100px)`; // top
faces[3].style.transform = `rotateX(-90deg) translateZ(100px)`; // bottom
faces[4].style.transform = `rotateY(-90deg) translateZ(100px)`; // left
faces[5].style.transform = `rotateY(90deg) translateZ(100px)`; // right
}
function squishCube(e) {
if (!isDragging) return;
const deltaX = e.clientX - startX;
const deltaY = e.clientY - startY;
currentRotationY += deltaX * 0.5;
currentRotationX -= deltaY * 0.5;
// Add squish effect
const squishFactor = Math.abs(deltaX) * 0.002;
const scaleX = 1 + squishFactor;
const scaleY = 1 - squishFactor;
cube.style.transform = `
rotateX(${currentRotationX}deg)
rotateY(${currentRotationY}deg)
scaleX(${scaleX})
scaleY(${scaleY})
`;
startX = e.clientX;
startY = e.clientY;
}
function stopSquishy() {
isDragging = false;
cube.style.transform = `
rotateX(${currentRotationX}deg)
rotateY(${currentRotationY}deg)
`;
}
createCubeFaces();
updateCubeFaces();
cube.addEventListener('mousedown', (e) => {
isDragging = true;
startX = e.clientX;
startY = e.clientY;
});
document.addEventListener('mousemove', squishCube);
document.addEventListener('mouseup', stopSquishy);
document.addEventListener('mouseleave', stopSquishy);
</script>
</body>
</html>
This HTML file creates an interactive, 3D cube that you can drag and rotate. When you drag the cube, it will:
- Rotate based on mouse movement
- Squish and stretch dynamically
- Have a 3D perspective with visible faces
- Smoothly return to its original shape when released
Key features:
- Single HTML file
- Pure JavaScript and CSS
- Responsive design
- Interactive squishy effect
- 3D cube rendering
Just save this as an .html file and open it in a modern browser. Drag the cube around and enjoy the elastic, squishy interaction!