Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Transformation Example</title> <style> canvas { border: 1px solid black; } </style> </head> <body> <canvas id="transformCanvas" width="200" height="200"></canvas> <script> const canvas = document.getElementById('transformCanvas'); const ctx = canvas.getContext('2d'); ctx.translate(50, 50); // Move the canvas origin to (50, 50) ctx.rotate((Math.PI / 180) * 25); // Rotate the canvas 25 degrees ctx.fillStyle = 'blue'; // Set fill color to blue ctx.fillRect(0, 0, 100, 50); // Draw a rectangle </script> </body> </html>