Here's an updated version of the HTML page that includes a simple image in the background and presets for common transformations:
html Copy code
<!DOCTYPE html>
<html>
<head>
<title>2D Linear Transformation</title>
<style>
canvas {
border: 1px solid black;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
padding: 20px;
}
.matrix-inputs {
display: grid;
grid-template-columns: repeat(2, 50px);
gap: 5px;
}
input {
width: 50px;
}
.presets {
display: flex;
gap: 10px;
}
button {
padding: 5px 10px;
}
</style>
</head>
<body>
<div class="container">
<h2>2D Linear Transformation Visualizer</h2>
<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 onclick="presetIdentity()">Identity</button>
<button onclick="presetRotate(45)">Rotate 45°</button>
<button onclick="presetRotate(90)">Rotate 90°</button>
<button onclick="presetScale(2)">Scale 2x</button>
<button onclick="presetScale(0.5)">Scale 0.5x</button>
<button onclick="presetShear(1, 0)">Shear X</button>
<button onclick="presetShear(0, 1)">Shear Y</button>
<button onclick="presetFlipX()">Flip X</button>
<button onclick="presetFlipY()">Flip Y</button>
</div>
<button onclick="transform()">Transform</button>
<canvas id="canvas" width="400" height="400"></canvas>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const scale = 40; // pixels per unit
const center = { x: canvas.width / 2, y: canvas.height / 2 };
// Load background image
const img = new Image();
img.src = 'https://picsum.photos/400/400'; // Replace with your own image URL
img.onload = () => {
transform();
};
function drawGrid() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
// Draw axes
ctx.beginPath();
ctx.strokeStyle = '#ccc';
ctx.moveTo(0, center.y);
ctx.lineTo(canvas.width, center.y);
ctx.moveTo(center.x, 0);
ctx.lineTo(center.x, canvas.height);
ctx.stroke();
}
function drawVector(x, y, color) {
ctx.beginPath();
ctx.strokeStyle = color;
ctx.moveTo(center.x, center.y);
ctx.lineTo(center.x + x * scale, center.y - y * scale);
ctx.stroke();
}
function transform() {
// 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);
drawGrid();
// Draw original basis vectors
drawVector(1, 0, 'blue');
drawVector(0, 1, 'red');
// Draw transformed basis vectors
drawVector(a11, a21, 'rgba(0, 0, 255, 0.5)');
drawVector(a12, a22, 'rgba(255, 0, 0, 0.5)');
// Draw grid of points
ctx.fillStyle = 'black';
for (let i = -5; i <= 5; i++) {
for (let j = -5; j <= 5; j++) {
// Transform each point
const x = a11 * i + a12 * j;
const y = a21 * i + a22 * j;
// Draw transformed point
ctx.beginPath();
ctx.arc(
center.x + x * scale,
center.y - y * scale,
2,
0,
2 * Math.PI
);
ctx.fill();
}
}
}
// Preset functions
function presetIdentity() {
document.getElementById('a11').value = 1;
document.getElementById('a12').value = 0;
document.getElementById('a21').value = 0;
document.getElementById('a22').value = 1;
transform();
}
function presetRotate(angle) {
const rad = angle * Math.PI / 180;
document.getElementById('a11').value = Math.cos(rad);
document.getElementById('a12').value = -Math.sin(rad);
document.getElementById('a21').value = Math.sin(rad);
document.getElementById('a22').value = Math.cos(rad);
transform();
}
function presetScale(factor) {
document.getElementById('a11').value = factor;
document.getElementById('a12').value = 0;
document.getElementById('a21').value = 0;
document.getElementById('a22').value = factor;
transform();
}
function presetShear(kx, ky) {
document.getElementById('a11').value = 1;
document.getElementById('a12').value = kx;
document.getElementById('a21').value = ky;
document.getElementById('a22').value = 1;
transform();
}
function presetFlipX() {
document.getElementById('a11').value = -1;
document.getElementById('a12').value = 0;
document.getElementById('a21').value = 0;
document.getElementById('a22').value = 1;
transform();
}
function presetFlipY() {
document.getElementById('a11').value = 1;
document.getElementById('a12').value = 0;
document.getElementById('a21').value = 0;
document.getElementById('a22').value = -1;
transform();
}
</script>
</body>
</html>
When you click on a preset button, it updates the matrix values and applies the transformation. The background image helps visualize the effect of the transformation on the grid and the basis vectors.