Here's a complete implementation of a 3D rotating sphere made of alphabets with depth-based brightness:
html Copy code
<!DOCTYPE html>
<html>
<head>
<title>3D Alphabet Sphere</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: Arial, sans-serif;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Set canvas size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Configuration
const radius = Math.min(canvas.width, canvas.height) * 0.3;
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numPoints = 500;
const rotationSpeed = 0.005;
// Create points on a sphere
const points = [];
for (let i = 0; i < numPoints; i++) {
// Spherical coordinates
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos(2 * Math.random() - 1);
// Convert to Cartesian coordinates
const x = radius * Math.sin(phi) * Math.cos(theta);
const y = radius * Math.sin(phi) * Math.sin(theta);
const z = radius * Math.cos(phi);
// Random alphabet character
const char = alphabet[Math.floor(Math.random() * alphabet.length)];
points.push({ x, y, z, char });
}
// Animation variables
let angleX = 0;
let angleY = 0;
// Animation loop
function animate() {
// Clear canvas
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Center the sphere
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// Sort points by z for proper depth rendering
const sortedPoints = [...points].sort((a, b) => {
// Rotate points
const aRotated = rotatePoint(a, angleX, angleY);
const bRotated = rotatePoint(b, angleX, angleY);
return bRotated.z - aRotated.z;
});
// Draw each point
sortedPoints.forEach(point => {
const rotated = rotatePoint(point, angleX, angleY);
// Perspective projection
const scale = 500 / (500 + rotated.z);
const x2d = centerX + rotated.x * scale;
const y2d = centerY + rotated.y * scale;
// Calculate brightness based on z position
const normalizedZ = (rotated.z + radius) / (2 * radius);
const brightness = Math.floor(normalizedZ * 155 + 100);
const color = `rgb(${brightness}, ${brightness}, ${brightness})`;
// Draw the character
ctx.fillStyle = color;
ctx.font = `${Math.max(10, 20 * scale)}px Arial`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(point.char, x2d, y2d);
});
// Update rotation angles
angleX += rotationSpeed;
angleY += rotationSpeed * 0.7;
requestAnimationFrame(animate);
}
// Rotate a point in 3D space
function rotatePoint(point, angleX, angleY) {
// Rotate around X axis
const cosX = Math.cos(angleX);
const sinX = Math.sin(angleX);
const y1 = point.y * cosX - point.z * sinX;
const z1 = point.y * sinX + point.z * cosX;
// Rotate around Y axis
const cosY = Math.cos(angleY);
const sinY = Math.sin(angleY);
const x2 = point.x * cosY + z1 * sinY;
const z2 = -point.x * sinY + z1 * cosY;
return { x: x2, y: y1, z: z2, char: point.char };
}
// Handle window resize
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// Start animation
animate();
</script>
</body>
</html>
The sphere will continuously rotate, creating a visually appealing 3D effect with proper depth perception.