Creating a 3x3x3 Cube in Web Development
Here's how to create a 3D 3×3×3 cube using HTML, CSS, and JavaScript. This example uses Three.js, a popular 3D graphics library for the web.
HTML Structure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3x3x3 Cube</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #f0f0f0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/examples/js/controls/OrbitControls.js"></script>
<script src="app.js"></script>
</body>
</html>
JavaScript (app.js)
// Initialize the scene
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf0f0f0);
// Set up the camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 10;
// Create the renderer
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add orbit controls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
// Add ambient light
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
// Add directional light
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
// Create a group for the 3x3x3 cube
const cubeGroup = new THREE.Group();
scene.add(cubeGroup);
// Function to create a single small cube
function createSmallCube(x, y, z) {
const geometry = new THREE.BoxGeometry(1, 1, 1);
const edges = new THREE.EdgesGeometry(geometry);
const line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({ color: 0x000000 }));
// Random color for each cube
const color = new THREE.Color(Math.random(), Math.random(), Math.random());
const material = new THREE.MeshPhongMaterial({
color: color,
transparent: true,
opacity: 0.8
});
const cube = new THREE.Mesh(geometry, material);
cube.position.set(x, y, z);
cube.add(line);
return cube;
}
// Create the 3x3x3 cube structure
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
for (let z = -1; z <= 1; z++) {
const smallCube = createSmallCube(x * 1.1, y * 1.1, z * 1.1);
cubeGroup.add(smallCube);
}
}
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
controls.update();
cubeGroup.rotation.x += 0.005;
cubeGroup.rotation.y += 0.005;
renderer.render(scene, camera);
}
animate();
Alternative: Pure CSS 3D Cube
If you prefer a simpler CSS-only approach (though less interactive):
<!DOCTYPE html>
<html>
<head>
<style>
body {
perspective: 1000px;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #f0f0f0;
}
.cube-container {
width: 200px;
height: 200px;
position: relative;
transform-style: preserve-3d;
animation: rotate 15s infinite linear;
}
.cube {
width: 100%;
height: 100%;
position: relative;
transform-style: preserve-3d;
}
.small-cube {
position: absolute;
width: 60px;
height: 60px;
border: 1px solid #000;
display: flex;
justify-content: center;
align-items: center;
background: rgba(0, 0, 255, 0.5);
transform-style: preserve-3d;
}
@keyframes rotate {
from { transform: rotateX(0deg) rotateY(0deg); }
to { transform: rotateX(360deg) rotateY(360deg); }
}
</style>
</head>
<body>
<div class="cube-container">
<div class="cube">
<!-- This would need 27 small-cube divs positioned in 3D space -->
<!-- For simplicity, I'm showing just one - a full implementation would require more CSS -->
<div class="small-cube" style="transform: translateX(-30px) translateY(-30px) translateZ(30px);"></div>
</div>
</div>
</body>
</html>
The Three.js version is more complete and interactive, allowing you to rotate the cube with your mouse. The CSS version is simpler but would require more code to properly position all 27 small cubes in 3D space.