Rotating 3D Sphere Simulation in JavaScript
To create a rotating 3D sphere made up of alphabets, we will use the HTML5 Canvas API and JavaScript. The simulation will display a sphere with alphabets, where the closest letters appear brighter and the farthest ones appear grey.
Code
// Get the canvas element
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Set the canvas dimensions
canvas.width = 600;
canvas.height = 600;
// Define the sphere properties
const sphere = {
radius: 200,
rotationX: 0,
rotationY: 0,
};
// Define the alphabet properties
const alphabet = {
size: 20,
count: 100,
points: [],
};
// Generate random points on the sphere
for (let i = 0; i < alphabet.count; i++) {
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos(Math.random() * 2 - 1);
const x = sphere.radius * Math.sin(phi) * Math.cos(theta);
const y = sphere.radius * Math.sin(phi) * Math.sin(theta);
const z = sphere.radius * Math.cos(phi);
alphabet.points.push({ x, y, z, char: String.fromCharCode(65 + Math.floor(Math.random() * 26)) });
}
// Main animation loop
function animate() {
requestAnimationFrame(animate);
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Rotate the sphere
sphere.rotationX += 0.01;
sphere.rotationY += 0.01;
// Project the 3D points onto the 2D canvas
ctx.font = `${alphabet.size}px Arial`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
alphabet.points.forEach((point) => {
// Rotate the point around the X and Y axes
const rotatedX = point.x * Math.cos(sphere.rotationY) - point.z * Math.sin(sphere.rotationY);
const rotatedZ = point.x * Math.sin(sphere.rotationY) + point.z * Math.cos(sphere.rotationY);
const rotatedY = point.y * Math.cos(sphere.rotationX) + rotatedZ * Math.sin(sphere.rotationX);
const rotatedZ2 = -point.y * Math.sin(sphere.rotationX) + rotatedZ * Math.cos(sphere.rotationX);
// Project the 3D point onto the 2D canvas
const projectedX = rotatedX + canvas.width / 2;
const projectedY = rotatedY + canvas.height / 2;
// Calculate the brightness based on the Z-coordinate
const brightness = Math.max(0, Math.min(1, (rotatedZ2 + sphere.radius) / (2 * sphere.radius)));
ctx.fillStyle = `rgb(${Math.floor(brightness * 255)}, ${Math.floor(brightness * 255)}, ${Math.floor(brightness * 255)})`;
// Draw the character
ctx.fillText(point.char, projectedX, projectedY);
});
}
// Start the animation
animate();
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rotating 3D Sphere</title>
<style>
body {
background-color: #f0f0f0;
}
#canvas {
border: 1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="script.js"></script>
</body>
</html>
Explanation
- We first create a canvas element and get a reference to its 2D drawing context.
- We define the properties of the sphere, including its radius and rotation angles.
- We generate a set of random points on the surface of the sphere, along with a random character for each point.
- In the main animation loop, we clear the canvas, rotate the sphere, and project the 3D points onto the 2D canvas.
- We calculate the brightness of each character based on its Z-coordinate, with characters closer to the viewer appearing brighter.
- We draw each character on the canvas using the
fillText method, with its color determined by its brightness.
Usage
- Create an HTML file and copy the HTML code into it.
- Create a JavaScript file (e.g.,
script.js) and copy the JavaScript code into it.
- Open the HTML file in a web browser to view the rotating 3D sphere simulation.
Note: This code uses the HTML5 Canvas API, which is supported by most modern web browsers.