Skip to content

Commit 03e081c

Browse files
committed
Initialze P5 and add basis vectors
0 parents  commit 03e081c

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Linear Transformation Visualzer</title>
5+
<link rel="stylesheet" type="text/css" href="style.css" />
6+
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.2/lib/p5.min.js"></script>
7+
</head>
8+
<body>
9+
<canvas></canvas>
10+
<script src="main.js"></script>
11+
</body>
12+
</html>

main.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// DOM elements
2+
3+
const DOM_CANVAS = document.querySelector("canvas");
4+
5+
// Colors
6+
7+
const SPACE_COLOR = [63, 63, 70];
8+
const BASIS1_COLOR = [239, 68, 68];
9+
const BASIS2_COLOR = [34, 197, 94];
10+
11+
// Values
12+
13+
let CANVAS_HAS_CHANGED = true;
14+
let SCALE = 50;
15+
let BASIS = [
16+
[[1], [0]], // basis 1
17+
[[0], [1]] // bassi 2
18+
];
19+
20+
21+
// P5
22+
23+
function setup() {
24+
createCanvas(windowWidth + 200, windowHeight + 200, P2D, DOM_CANVAS);
25+
}
26+
27+
function draw() {
28+
if (!CANVAS_HAS_CHANGED) {
29+
return;
30+
}
31+
32+
translate(windowWidth / 2, windowHeight / 2);
33+
scale(1, -1);
34+
35+
background(SPACE_COLOR);
36+
drawBasisArrows(BASIS);
37+
38+
CANVAS_HAS_CHANGED = false;
39+
}
40+
41+
function windowResized() {
42+
resizeCanvas(windowWidth + 200, windowHeight + 200);
43+
CANVAS_HAS_CHANGED = true;
44+
}
45+
46+
// Canvas utils
47+
48+
function drawBasisArrows(basis) {
49+
drawArrow(0, 0, basis[0][0], basis[0][1], BASIS1_COLOR);
50+
drawArrow(0, 0, basis[1][0], basis[1][1], BASIS2_COLOR);
51+
}
52+
53+
function drawArrow(x1, y1, x2, y2, color) {
54+
stroke(color);
55+
strokeWeight(3);
56+
fill(color);
57+
58+
x1 = x1 * SCALE;
59+
x2 = x2 * SCALE;
60+
y1 = y1 * SCALE;
61+
y2 = y2 * SCALE;
62+
63+
let angle = atan2(y2 - y1, x2 - x1);
64+
65+
line(x1, y1, x2, y2);
66+
67+
let arrowSize = 8;
68+
69+
let x3 = x2 - arrowSize * cos(angle - PI / 6);
70+
let y3 = y2 - arrowSize * sin(angle - PI / 6);
71+
let x4 = x2 - arrowSize * cos(angle + PI / 6);
72+
let y4 = y2 - arrowSize * sin(angle + PI / 6);
73+
74+
triangle(x2, y2, x3, y3, x4, y4);
75+
}

style.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
6+
html,
7+
body {
8+
width: 100%;
9+
height: 100%;
10+
}
11+
12+
body {
13+
overflow: hidden;
14+
}

0 commit comments

Comments
 (0)