Make sure to select a complicated image as the background, make sure that the image…
- User
Make sure to select a complicated image as the background, make sure that the image changes properly with the transformation
- Assistant
Make sure to select a complicated image as the background, make sure that the image changes properly with the transformation
To ensure the background image is transformed along with the linear transformation (rather than just being redrawn statically), we need to apply the transformation matrix to the canvas context itself using ctx.setTransform(). This way, the image, grid, and vectors are all transformed together.
Below is a complete HTML page that:
✅ Uses a complex image as the background (e.g. a cityscape from Unsplash)
✅ Applies the transformation matrix to the canvas context
✅ Includes presets for common transformations
✅ Draws transformed axes and grid points
📄 Full HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>2D Linear Transformation Visualizer</title>
<style>
body {
font-family: sans-serif;
text-align: center;
background: #f0f0f0;
}
canvas {
border: 1px solid #333;
background: #fff;
}
.controls {
margin: 20px auto;
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
.matrix-inputs {
display: grid;
grid-template-columns: repeat(2, 60px);
gap: 5px;
}
input {
width: 60px;
padding: 5px;
text-align: center;
}
.presets {
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
}
.preset-button {
padding: 5px 10px;
background: #ddd;
border: none;
border-radius: 4px;
cursor: pointer;
}
.preset-button:hover {
background: #bbb;
}
</style>
</head>
<body>
<h2>2D Linear Transformation Visualizer</h2>
<div class="controls">
<div class="matrix-inputs">
<input type="number" id="a11" value="1" step="0.1">
<input type="number" id="a12" value="0" step="0.1">
<input type="number" id="a21" value="0" step="0.1">
<input type="number" id="a22" value="1" step="0.1">
</div>
<div class="presets">
<button class="preset-button" onclick="setPreset('identity')">Identity</button>
<button class="preset-button" onclick="setPreset('rotate90')">Rotate 90°</button>
<button class="preset-button" onclick="setPreset('rotate180')">Rotate 180°</button>
<button class="preset-button" onclick="setPreset('rotate270')">Rotate 270°</button>
<button class="preset-button" onclick="setPreset('scale2')">Scale 2x</button>
<button class="preset-button" onclick="setPreset('scale05')">Scale 0.5x</button>
<button class="preset-button" onclick="setPreset('shearX')">Shear X</button>
<button class="preset-button" onclick="setPreset('shearY')">Shear Y</button>
</div>
<button onclick="draw()">Transform</button>
</div>
<canvas id="canvas" width="500" height="500"></canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const center = { x: canvas.width / 2, y: canvas.height / 2 };
const scale = 40;
// Load a complex background image
const image = new Image();
image.crossOrigin = "anonymous";
image.src = "https://images.unsplash.com/photo-1508923567004-3a6b8004f3d3?auto=format&fit=crop&w=500&q=80"; // Cityscape
image.onload = () => {
draw();
};
function draw() {
// Get matrix values
const a11 = parseFloat(document.getElementById("a11").value);
const a12 = parseFloat(document.getElementById("a12").value);
const a21 = parseFloat(document.getElementById("a21").value);
const a22 = parseFloat(document.getElementById("a22").value);
// Clear canvas
ctx.setTransform(1, 0, 0, 1, 0, 0); // Reset transform
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Move origin to center
ctx.translate(center.x, center.y);
// Apply transformation matrix
ctx.transform(a11, a21, a12, a22, 0, 0);
// Draw background image (centered)
ctx.save();
ctx.translate(-image.width / 2, -image.height / 2);
ctx.drawImage(image, 0, 0);
ctx.restore();
// Draw grid points
ctx.fillStyle = "black";
for (let i = -5; i <= 5; i++) {
for (let j = -5; j <= 5; j++) {
ctx.beginPath();
ctx.arc(i * scale, -j * scale, 2, 0, 2 * Math.PI);
ctx.fill();
}
}
// Draw transformed basis vectors
drawVector(1, 0, "blue");
drawVector(0, 1, "red");
}
function drawVector(x, y, color) {
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.moveTo(0, 0);
ctx.lineTo(x * scale, -y * scale);
ctx.stroke();
}
function setPreset(preset) {
const m = {
identity: [1, 0, 0, 1],
rotate90: [0, -1, 1, 0],
rotate180: [-1, 0, 0, -1],
rotate270: [0, 1, -1, 0],
scale2: [2, 0, 0, 2],
scale05: [0.5, 0, 0, 0.5],
shearX: [1, 1, 0, 1],
shearY: [1, 0, 1, 1],
}[preset];
document.getElementById("a11").value = m[0];
document.getElementById("a12").value = m[1];
document.getElementById("a21").value = m[2];
document.getElementById("a22").value = m[3];
draw();
}
</script>
</body>
</html>
✅ Features:
🧪 Try It:
Let me know if you'd like to add animation or inverse transformations!