To rotate the square using pure 3D transformations in JavaScript, we can use a transformation matrix to apply the rotations in 3D space. Applying a rotation around the X-axis, Y-axis, or Z-axis can be done by multiplying the current transformation matrix with a rotation matrix. Here's an example code snippet that creates a 4x4 matrix to represent the transformations, and applies the rotation angles around the X, Y, and Z axes:
const canvas = document.createElement('canvas');
canvas.width = 400;
canvas.height = 400;
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#FF0000';
ctx.fillRect(150, 150, 100, 100);
ctx.strokeStyle = '#000000';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(200, 150);
ctx.lineTo(200, 250);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(150, 200);
ctx.lineTo(250, 200);
ctx.stroke();
const container = document.getElementById('canvas-container');
document.body.appendChild(canvas);
const identityMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
const transformMatrix = identityMatrix.slice(); // copy identity matrix
let angleX = 0;
let angleY = 0;
let angleZ = 0;
let lastTimestamp;
function render() {
const now = performance.now();
const delta = lastTimestamp ? now - lastTimestamp : 0;
lastTimestamp = now;
angleX += delta * 0.001; // rotate 1 deg per second
angleY += delta * 0.0015; // rotate 1.5 deg per second
angleZ += delta * 0.0005; // rotate 0.5 deg per second
// create rotation matrices for X, Y, and Z axes
const xRot = [
1, 0, 0, 0,
0, Math.cos(angleX), Math.sin(angleX), 0,
0, -Math.sin(angleX), Math.cos(angleX), 0,
0, 0, 0, 1,
];
const yRot = [
Math.cos(angleY), 0, -Math.sin(angleY), 0,
0, 1, 0, 0,
Math.sin(angleY), 0, Math.cos(angleY), 0,
0, 0, 0, 1,
];
const zRot = [
Math.cos(angleZ), Math.sin(angleZ), 0, 0,
-Math.sin(angleZ), Math.cos(angleZ), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
];
// combine the rotation matrices into one matrix
transformMatrix.splice(0, 16, ...xRot.map((a, i) => yRot.reduce((acc, b, j) => acc + b * xRot[j4+i%4] * zRot[i%4+j4], 0)));
ctx.clearRect(0, 0, canvas.width, canvas.height);
// draw the red square and black lines
const path = new Path2D();
path.rect(150, 150, 100, 100);
path.moveTo(200, 150);
path.lineTo(200, 250);
path.moveTo(150, 200);
path.lineTo(250, 200);
ctx.stroke(path);
// apply the transformation matrix to the canvas context
ctx.setTransform(...transformMatrix);
// request next frame
requestAnimationFrame(render);
}
requestAnimationFrame(render);
In this example code, we first draw the red square at (150, 150) with a size of 100x100, and the two black lines as before. Then, we create an identity matrix and a transformMatrix that initially copies the identity matrix. In the render function, we create three rotation matrices for X, Y, and Z axes using the current angle values. We then combine these matrices into a single transformation matrix by multiplying them in the order of Z-Y-X rotations. Finally, we draw the square and lines using Path2D for better performance, apply the transformation matrix to the canvas context using setTransform, and request the next frame with requestAnimationFrame.