-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
148 lines (128 loc) · 3.61 KB
/
main.js
File metadata and controls
148 lines (128 loc) · 3.61 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// CANVAS SETUP
const canvas = /** @type {HTMLCanvasElement} */ document.getElementById('canvas');
const ctx = /** @type {CanvasRenderingContext2D} */canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
ctx.strokeStyle = '#666666';
ctx.lineWidth = 2;
// DOM VARIABLES
const input1Percent = document.querySelector('.input1');
const input2Degree = document.querySelector('.input2');
const input3Ratio = document.querySelector('.input3');
const slider = document.querySelector('.slider');
const inputs = [input1Percent, input2Degree, input3Ratio];
// DRAWING FUNCTIONS
const drawGround = function () {
ctx.beginPath();
ctx.moveTo(0, 599)
ctx.lineTo(800, 599);
ctx.stroke();
};
const draw = function (percentSlope) {
const length = 800;
const radians = Math.atan(percentSlope / 100);
const deltaX = length * Math.cos(radians);
const deltaY = length * Math.sin(radians);
ctx.beginPath();
ctx.moveTo(0, 599);
ctx.lineTo(deltaX, 599 - deltaY);
ctx.stroke();
};
// CALCULATION FUNCTIONS
const degreeToPercent = function (degree) {
const rad = degree * (Math.PI / 180)
const percent = (Math.tan(rad) * 100).toFixed(2)
return percent;
};
const ratioToPercent = function (ratio) {
const percent = ((1 / ratio) * 100).toFixed(2)
return percent
};
const updateFromPercent = function (percent) {
slider.value = percent;
input2Degree.value = (Math.atan(percent / 100) * 180 / Math.PI).toFixed(2);
input3Ratio.value = (1 / (percent / 100)).toFixed(2);
};
const updateFromDegree = function (degree) {
slider.value = degreeToPercent(degree);
input1Percent.value = degreeToPercent(degree);
input3Ratio.value = (1 / Math.tan(degree * Math.PI / 180)).toFixed(2);
};
const updateFromRatio = function (ratio) {
slider.value = ratioToPercent(ratio);
input1Percent.value = ratioToPercent(ratio);
input2Degree.value = (Math.atan(1 / ratio) * 180 / Math.PI).toFixed(2);
};
const updateFromSlider = function (percent) {
input1Percent.value = percent;
input2Degree.value = (Math.atan(percent / 100) * 180 / Math.PI).toFixed(2);
input3Ratio.value = (1 / (percent / 100)).toFixed(2);
};
// INIT
const init = function () {
drawGround();
input2Degree.value = '';
input1Percent.value = '';
input3Ratio.value = '';
};
const clearSlopeLine = function () {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGround();
};
// INPUT EVENT LISTENERS
input1Percent.addEventListener('keyup', () => {
const curVal = input1Percent.value;
// Clearing Canvas
clearSlopeLine();
// Drawing and updating
if (curVal) {
draw(curVal);
updateFromPercent(curVal);
}
else {
init();
}
});
input2Degree.addEventListener('keyup', () => {
const curVal = input2Degree.value;
const curPercent = degreeToPercent(curVal);
// Clearing Canvas
clearSlopeLine();
// Drawing and updating
if (curVal) {
draw(curPercent);
updateFromDegree(curVal);
}
else {
init();
}
});
input3Ratio.addEventListener('keyup', () => {
const curVal = input3Ratio.value;
const curPercent = ratioToPercent(curVal);
// Clearing Canvas
clearSlopeLine();
// Drawing and updating
if (curVal) {
draw(curPercent);
updateFromRatio(curVal);
}
else {
init();
}
});
slider.addEventListener('input', () => {
const curVal = slider.value;
// Clearing Canvas
ctx.clearRect(0, 0, canvas.width, canvas.height)
drawGround();
// Drawing and updating
if (curVal) {
draw(curVal);
updateFromSlider(curVal);
}
else {
init();
}
});
init();