How to draw the red rectangle below black rectangle on canvas such that all 4 vertices of the rotated black rectangle touch all 4 edges of the red rectangle? I am using the following code to add the black rectangle:
const canvas = document.getElementById('myCanvas1');
const ctx = canvas.getContext('2d');
// Rectangle properties
const width = 50;
const height = 200;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// Rotation angle in degrees
const degrees = -10; // Anti-clockwise rotation
const radians = (degrees * Math.PI) / 180;
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Save the canvas state
ctx.save();
// Translate to the center of the rectangle
ctx.translate(centerX, centerY);
// Rotate the canvas
ctx.rotate(radians);
// Draw the rotated rectangle
ctx.fillStyle = 'black';
ctx.fillRect(-width / 2, -height / 2, width, height);
// Restore the canvas state
ctx.restore();
<canvas id="myCanvas1" width="600" height="250"></canvas>