Skip to content

Commit a16980a

Browse files
committed
Add input validation
1 parent 32cef8a commit a16980a

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

main.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
const DOM_CANVAS = document.querySelector("canvas");
44
const DOM_TRANSFROMATION_MATRIX = document.querySelector(".transformation-matrix");
5-
const DOM_MODE_BUTTONS = document.querySelectorAll(".header-bottom input")
6-
const DOM_INSTRUCTION_TEXT = document.querySelector(".instruction-text")
5+
const DOM_MODE_BUTTONS = document.querySelectorAll(".header-bottom input");
6+
const DOM_INSTRUCTION_TEXT = document.querySelector(".instruction-text");
7+
const DOM_PLAY_TRANSFORMATION_BUTTON = document.querySelector(".transformation-play-button");
78

89
// Colors
910

@@ -22,7 +23,7 @@ let BASIS = [null, null];
2223
let INITIAL_BASIS = [null, null];
2324
let BASIS_HANDLE_ACTIVE = [false, false];
2425
let MOUSE_TOLERANCE = 0.2;
25-
let CURRENT_MODE = "free-movement-mode"
26+
let CURRENT_MODE = "free-movement-mode";
2627

2728
// P5
2829

@@ -216,6 +217,9 @@ function domSetMatrix(element, matrix, childElementName) {
216217
if (childElementName === "input") {
217218
item.value = matrix[i][j];
218219
item.classList.add("matrix-input")
220+
item.addEventListener("input", () => {
221+
domValidateInputTransformation();
222+
})
219223
}
220224
else {
221225
item.innerText = matrix[i][j];
@@ -226,6 +230,9 @@ function domSetMatrix(element, matrix, childElementName) {
226230
}
227231

228232
function domChangeMode(mode) {
233+
BASIS[0].set(1, 0);
234+
BASIS[1].set(0, 1);
235+
229236
document.querySelector("body").dataset.mode = mode;
230237
if (mode === "free-movement-mode") {
231238
DOM_INSTRUCTION_TEXT.innerText = "Click and drag the heads of the basis vectors to see the required transformation";
@@ -238,6 +245,19 @@ function domChangeMode(mode) {
238245
CANVAS_HAS_CHANGED = true;
239246
}
240247

248+
function domValidateInputTransformation() {
249+
let isInvalid = false;
250+
251+
const inputs = document.querySelectorAll(".matrix-input");
252+
inputs.forEach(input => {
253+
if (isNaN(Number(input.value)) || input.value === "") {
254+
isInvalid = true;
255+
}
256+
})
257+
258+
DOM_PLAY_TRANSFORMATION_BUTTON.disabled = isInvalid;
259+
}
260+
241261
// Math utils
242262

243263
function basisToMatrix(basis) {

0 commit comments

Comments
 (0)