Here's a simple HTML page that demonstrates a 2D linear transformation using a matrix:
<!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;
}
</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>
<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 };
function drawGrid() {
ctx.clearRect(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();
}
}
}
// Initial draw
transform();
</script>
</body>
</html>
This webpage creates a visualization of a 2D linear transformation. Here's what it does:
- Shows a 2x2 matrix input where you can enter the transformation matrix values
- Displays a coordinate system with:
- Original basis vectors (in solid blue and red)
- Transformed basis vectors (in semi-transparent blue and red)
- A grid of transformed points
- Updates the visualization when you click the "Transform" button
The transformation is represented by the matrix:
[a11 a12]
[a21 a22]
Some interesting transformations to try:
- Rotation: [cos(θ) -sin(θ)] [sin(θ) cos(θ)]
- Scaling: [sx 0] [0 sy]
- Shear: [1 k] [0 1]
For example:
- 90° rotation: [0 -1] [1 0]
- Scale by 2: [2 0] [0 2]
- Horizontal shear: [1 1] [0 1]
The visualization shows how the transformation affects the basis vectors and a grid of points in the plane.