-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorCalculator.html
235 lines (205 loc) · 11.6 KB
/
VectorCalculator.html
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vector calculator</title>
<link id="favicon" rel="icon" type="image/x-icon" href="">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Agbalumo&display=swap" rel="stylesheet">
<style>
html, body{
display: grid;
}
canvas {
border: 2px black solid;
}
* {
justify-self: center;
align-self: center;
}
ul#results {
list-style: none;
padding: 0;
}
ul#results li:nth-child(odd) {
background-color: aqua;
width: fit-content;
font-family: 'Agbalumo', sans-serif;
}
ul#results li{
background-color: white;
width: fit-content;
font-family: 'Agbalumo', sans-serif;
}
ul#results li:first-child {
background-color: rgb(89, 253, 89);
color: black;
font-family: 'Agbalumo', sans-serif;
}
.icon {
cursor: pointer;
margin-left: 10px;
}
</style>
</head>
<body>
<h1 id="ABResult">Result: enter A & B values</h1>
<form method="dialog">
<input type="number" step="any" placeholder="A length" id="Al">
<input type="number" step="any" placeholder="A angle" id="A0">
<br><br>
<input type="number" step="any" placeholder="B length" id="Bl">
<input type="number" step="any" placeholder="B angle" id="B0">
<br><br>
<input type="number" step="1" placeholder="number after the dot" id="nd" value="2">
<button id="button">Calculate</button>
</form>
<br><br>
<canvas id="vectorCanvas"></canvas>
<h1 id="ABResult">last results</h1>
<ul id="results">
<!-- Results will be pushed here -->
</ul>
<script>
const canvas = document.getElementById("vectorCanvas");
const ctx = canvas.getContext("2d");
var drawScale = 8
// vector canvas
function drawVector(length, angle, color, bool){
if (bool){
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
const drawingWidth = length * Math.cos(dtr(angle)) * drawScale;
const drawingHeight = length * Math.sin(dtr(angle)) * drawScale;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const endX = centerX + drawingWidth;
const endY = centerY - drawingHeight;
ctx.beginPath();
ctx.moveTo(centerX, 0);
ctx.lineTo(centerX, canvas.height);
ctx.moveTo(0, centerY);
ctx.lineTo(canvas.width, centerY);
ctx.strokeStyle = "gray";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(centerX, centerY);
ctx.lineTo(endX, endY);
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.stroke();
}
// vector calcs
const Al = document.getElementById("Al");
const A0 = document.getElementById("A0");
const Bl = document.getElementById("Bl");
const B0 = document.getElementById("B0");
var numberAfter = document.getElementById("nd").value;
const button = document.getElementById("button");
const resultsList = document.getElementById("results");
function dtr(degrees) {
return degrees * (Math.PI / 180);
}
function copyToClipboard(text) {
// Create a temporary textarea element to hold the text
const textArea = document.createElement("textarea");
textArea.value = text;
// Append the textarea to the document
document.body.appendChild(textArea);
// Select the text in the textarea
textArea.select();
// Copy the selected text to the clipboard
document.execCommand("copy");
// Remove the temporary textarea
document.body.removeChild(textArea);
}
function calculate() {
if (Al.value != "" && Al.value != '' && Bl.value != "" && B0.value != ''){
const AlValue = parseFloat(Al.value);
const A0Value = parseFloat(A0.value);
const BlValue = parseFloat(Bl.value);
const B0Value = parseFloat(B0.value);
Ax = AlValue * Math.cos(dtr(A0Value))
Ay = AlValue * Math.sin(dtr(A0Value))
Bx = BlValue * Math.cos(dtr(B0Value))
By = BlValue * Math.sin(dtr(B0Value))
// const AB_length = Math.sqrt(AlValue**2 + BlValue**2 + 2 * AlValue * BlValue * Math.cos(dtr(A0Value - B0Value)));
const AB_length = Math.sqrt( ( ( Ax + Bx ) ** 2 ) + ( ( Ay + By) ** 2 ) )
// const AB_angle = A0Value + Math.acos((AlValue * Math.cos(dtr(B0Value - A0Value) + BlValue)) / AB_length) * (180 / Math.PI);
const pre_AB_angle = Math.atan( ( (Ay) + (By) ) / ( (Ax) + (Bx) ) ) * ( 180 / Math.PI)
const AB_angle = 180 - Math.abs(pre_AB_angle)
const listItem = document.createElement("li");
listItem.setAttribute("al", AB_length.toFixed(numberAfter))
listItem.setAttribute("ab", AB_angle.toFixed(numberAfter))
listItem.textContent = `AB length is ${AB_length.toFixed(numberAfter)}, and the angle is ${AB_angle.toFixed(numberAfter)} degrees`;
const copyIconA = document.createElement("span");
copyIconA.className = "icon";
copyIconA.innerHTML = 'A <svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M208 0H332.1c12.7 0 24.9 5.1 33.9 14.1l67.9 67.9c9 9 14.1 21.2 14.1 33.9V336c0 26.5-21.5 48-48 48H208c-26.5 0-48-21.5-48-48V48c0-26.5 21.5-48 48-48zM48 128h80v64H64V448H256V416h64v48c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V176c0-26.5 21.5-48 48-48z"/></svg>'
copyIconA.addEventListener("click", () => {
Al.value = listItem.getAttribute("al");
A0.value = listItem.getAttribute("ab");
});
const copyIconB = document.createElement("span");
copyIconB.className = "icon";
copyIconB.innerHTML = 'B <svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M208 0H332.1c12.7 0 24.9 5.1 33.9 14.1l67.9 67.9c9 9 14.1 21.2 14.1 33.9V336c0 26.5-21.5 48-48 48H208c-26.5 0-48-21.5-48-48V48c0-26.5 21.5-48 48-48zM48 128h80v64H64V448H256V416h64v48c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V176c0-26.5 21.5-48 48-48z"/></svg>'
copyIconB.addEventListener("click", () => {
Bl.value = listItem.getAttribute("al");
B0.value = listItem.getAttribute("ab");
});
const copyIcon = document.createElement("span");
copyIcon.style.cursor = 'copy'
copyIcon.className = "icon";
copyIcon.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 384 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M192 0c-41.8 0-77.4 26.7-90.5 64H64C28.7 64 0 92.7 0 128V448c0 35.3 28.7 64 64 64H320c35.3 0 64-28.7 64-64V128c0-35.3-28.7-64-64-64H282.5C269.4 26.7 233.8 0 192 0zm0 64a32 32 0 1 1 0 64 32 32 0 1 1 0-64zM112 192H272c8.8 0 16 7.2 16 16s-7.2 16-16 16H112c-8.8 0-16-7.2-16-16s7.2-16 16-16z"/></svg>'
copyIcon.addEventListener("click", () => {
text = `length: ${listItem.getAttribute("al")}, angle: ${listItem.getAttribute("ab")}`
copyToClipboard(text)
});
// const copyImgIcon = document.createElement("span");
// copyImgIcon.style.cursor = 'context-menu'
// copyImgIcon.className = "icon";
// copyImgIcon.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 384 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M192 0c-41.8 0-77.4 26.7-90.5 64H64C28.7 64 0 92.7 0 128V448c0 35.3 28.7 64 64 64H320c35.3 0 64-28.7 64-64V128c0-35.3-28.7-64-64-64H282.5C269.4 26.7 233.8 0 192 0zm0 64a32 32 0 1 1 0 64 32 32 0 1 1 0-64zM112 192H272c8.8 0 16 7.2 16 16s-7.2 16-16 16H112c-8.8 0-16-7.2-16-16s7.2-16 16-16z"/></svg>'
// copyImgIcon.addEventListener("click", () => {
// imgSrc = canvas.toDataURL()
// copyImageToClipboard(imgSrc)
// document.getElementById("favicon").href = imgSrc
// });
const removeIcon = document.createElement("span");
removeIcon.className = "icon";
removeIcon.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M135.2 17.7L128 32H32C14.3 32 0 46.3 0 64S14.3 96 32 96H416c17.7 0 32-14.3 32-32s-14.3-32-32-32H320l-7.2-14.3C307.4 6.8 296.3 0 284.2 0H163.8c-12.1 0-23.2 6.8-28.6 17.7zM416 128H32L53.2 467c1.6 25.3 22.6 45 47.9 45H346.9c25.3 0 46.3-19.7 47.9-45L416 128z"/></svg>';
removeIcon.addEventListener("click", () => {
resultsList.removeChild(listItem);
});
listItem.appendChild(copyIconA);
listItem.appendChild(copyIconB);
listItem.appendChild(copyIcon);
listItem.appendChild(removeIcon);
drawVector(AlValue, A0Value, "red", true)
drawVector(BlValue, B0Value, "blue")
drawVector(listItem.getAttribute("al"), listItem.getAttribute("ab"), "purple")
if (resultsList.firstChild) {
resultsList.insertBefore(listItem, resultsList.firstChild);
} else {
resultsList.appendChild(listItem);
}
imgSrc = canvas.toDataURL()
document.getElementById("favicon").href = imgSrc
return true
} else {
return false
}
}
button.addEventListener("click", calculate);
document.getElementById("nd").addEventListener("change", () => { numberAfter = document.getElementById("nd").value; });
window.addEventListener("blur", () => {
document.querySelector("title").textContent = "We miss you!"
})
window.addEventListener("focus", () => {
document.querySelector("title").textContent = "Vector calculator"
})
</script>
</body>
</html>