<!
DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="[Link]
<meta name="description" content="CS4406 Computer Graphics - Exercise #3" />
<meta charset="utf-8" />
<title>3D Bouncing Torus Knot</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script src="[Link]
<script>
// Initialize scene, camera, and renderer
const scene = new [Link]();
const camera = new [Link](75, [Link] / [Link],
0.1, 1000);
const renderer = new [Link]();
[Link]([Link], [Link]);
[Link]([Link]);
// Create a torus knot geometry and material
const geometry = new [Link](1, 0.3, 100, 16);
let material = new [Link]({ color: 0x00ff00 });
const torusKnot = new [Link](geometry, material);
[Link](torusKnot);
// Position the camera
[Link].z = 5;
// Add a light source
const light = new [Link](0xFFFFAA, 1, 100);
[Link](-10, 10, 10);
[Link](light);
// Variables for animation
let xSpeed = 0.05;
let ySpeed = 0.03;
// Function to change color
function changeColor() {
const color = [Link]() * 0xffffff;
[Link](color);
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Move the torus knot
[Link].x += xSpeed;
[Link].y += ySpeed;
// Bounce off the edges and change color
if ([Link].x > 2 || [Link].x < -2) {
xSpeed = -xSpeed;
changeColor();
if ([Link].y > 2 || [Link].y < -2) {
ySpeed = -ySpeed;
changeColor();
[Link](scene, camera);
// Start the animation
animate();
// Handle window resize
[Link]('resize', () => {
[Link] = [Link] / [Link];
[Link]();
[Link]([Link], [Link]);
});
</script>
</body>
</html>