-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.js
99 lines (85 loc) · 1.99 KB
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Parser } from 'expr-eval';
const parser = new Parser();
const fInput = document.getElementById('f');
const axisNumInput = document.getElementById('axisNum');
const speed = 0.1;
let keysPressed = {
KeyW: false,
KeyA: false,
KeyS: false,
KeyD: false,
};
let centerX = 0;
let centerY = 0;
export function getF() {
let returnVal;
const eq = $.trim(fInput.value);
if (eq === '') {
fInput.classList.remove('is-invalid');
fInput.classList.remove('is-valid');
return null;
}
try {
const f = parser.parse(eq);
const vars = f.variables();
if (vars.length > 1 || vars[0] !== 'x') {
returnVal = null;
} else {
returnVal = f.toJSFunction(['x']);
}
} catch (e) {
returnVal = null;
}
if (returnVal === null) {
fInput.classList.remove('is-valid');
fInput.classList.add('is-invalid');
} else {
fInput.classList.remove('is-invalid');
fInput.classList.add('is-valid');
}
return returnVal;
}
export function getAxis() {
return document.querySelector('input[name="axis"]:checked').value;
}
export function getAxisNum() {
const val = axisNumInput.value;
if (val === '') {
axisNumInput.classList.remove('is-invalid');
axisNumInput.classList.remove('is-valid');
return null;
}
if ($.isNumeric(val)) {
axisNumInput.classList.remove('is-invalid');
axisNumInput.classList.add('is-valid');
return parseFloat(val);
}
axisNumInput.classList.remove('is-valid');
axisNumInput.classList.add('is-invalid');
return null;
}
function moveCenter() {
if (keysPressed.KeyW) {
centerY += speed;
}
if (keysPressed.KeyA) {
centerX -= speed;
}
if (keysPressed.KeyS) {
centerY -= speed;
}
if (keysPressed.KeyD) {
centerX += speed;
}
}
document.addEventListener('keydown', (e) => {
keysPressed[e.code] = true;
moveCenter();
});
document.addEventListener('keyup', (e) => {
keysPressed[e.code] = false;
moveCenter();
});
export function getCenter() {
return [centerX, centerY, 0];
}