-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslide_rule.html
399 lines (362 loc) · 9 KB
/
slide_rule.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html>
<html>
<head>
<title>Circular Slide Rule</title>
<link rel="icon" href="res/favicon.ico">
<script type="text/javascript">
const tau = 2 * Math.PI;
const zoomRate = 1.1;
const zoomSmooth = 0.2;
const zoomMin = 0.7;
let numA = 2;
let numB = 6;
let numC = 3;
let zoom = 0.7;
let zoomTarg = 0.7;
let posX = 0;
let posY = 0;
let posXTarg = 0;
let posYTarg = 0;
let mouseRawX = 0;
let mouseRawY = 0;
let mouseX = 0;
let mouseY = 0;
let click = false;
let pclick = false;
let clickAng = 0;
let clickRot = 0;
let clickOutLine = false;
let rot = 0;
let outLine = 0;
let domCanvas = null;
let domNumA = null;
let domNumB = null;
let domNumC = null;
function fmod(x, y = 1) {
const z = x / y;
return (z - Math.floor(z)) * y;
}
function arcMod(t) {
return fmod(t + Math.PI, tau) - Math.PI;
}
function safeGet(node, defaultVal) {
const x = parseFloat(node.value);
if (isNaN(x) || x <= 0) return defaultVal;
return x;
}
function fillNums() {
rot = tau * Math.log10(numA);
outLine = tau * Math.log10(numC);
domNumA.value = numA;
domNumB.value = numB;
domNumC.value = numC;
}
function numAChange() {
numA = safeGet(domNumA, numA);
numB = numC * numA;
fillNums();
}
function numBChange() {
numB = safeGet(domNumB, numB);
numA = numB / numC;
fillNums();
}
function numCChange() {
numC = safeGet(domNumC, numC);
numB = numC * numA;
fillNums();
}
function setOutLine(t) {
outLine = t;
numC = Math.pow(10, arcMod(t) / tau);
numB = numC * numA;
fillNums();
}
function setRot(t) {
rot = t;
numA = Math.pow(10, arcMod(t) / tau);
numB = numC * numA;
fillNums();
}
function onScroll(e) {
const oldZoom = zoomTarg;
zoomTarg *= Math.pow(zoomRate, e.deltaY / -100);
if (zoomTarg <= zoomMin) {
zoomTarg = zoomMin;
posXTarg = 0;
posYTarg = 0;
} else {
const dz = oldZoom / zoomTarg;
posXTarg = mouseX + dz * (posXTarg - mouseX);
posYTarg = mouseY + dz * (posYTarg - mouseY);
}
updateMouse();
}
function onMouseMove(e) {
mouseRawX = (2 * e.offsetY / domCanvas.height) - 1;
mouseRawY = (2 * e.offsetX / domCanvas.width) - 1;
updateMouse();
}
function onMouseDown(e) {
click = true;
}
function onMouseUp(e) {
click = false;
}
function updateMouse() {
mouseX = posXTarg - mouseRawX / zoomTarg;
mouseY = posYTarg + mouseRawY / zoomTarg;
}
function onLoad() {
domCanvas = document.getElementById("view");
domNumA = document.getElementById("num_a");
domNumB = document.getElementById("num_b");
domNumC = document.getElementById("num_c");
numAChange();
domCanvas.addEventListener('mousedown', onMouseDown);
domCanvas.addEventListener('mouseup', onMouseUp);
domCanvas.addEventListener('mouseout', onMouseUp);
const ctx = domCanvas.getContext('2d');
ctx.textAlign = "center";
ctx.textBaseline = "middle";
const halfWidth = domCanvas.width / 2;
const halfHeight = domCanvas.height / 2;
const drawCircle = (px, py, r, clr) => {
ctx.fillStyle = clr;
ctx.beginPath();
ctx.ellipse(py * zoom, -px * zoom, r * zoom, r * zoom, 0, 0, tau);
ctx.fill();
};
const drawLine = (px, py, qx, qy, clr) => {
ctx.strokeStyle = clr;
ctx.beginPath();
ctx.moveTo(py * zoom, -px * zoom);
ctx.lineTo(qy * zoom, -qx * zoom);
ctx.stroke();
};
const drawRadialLine = (r0, r1, t, clr) => {
const x = Math.cos(t);
const y = Math.sin(t);
drawLine(r0 * x, r0 * y, r1 * x, r1 * y, clr);
};
const drawText = (x, y, str, clr) => {
ctx.fillStyle = clr;
ctx.fillText(str, y * zoom, -x * zoom);
};
const drawNum = (x, y, num, size, clr) => {
ctx.strokeStyle = clr;
x -= 0.5 * size * num.length;
const gs = size * 0.8;
const gsh = 0.5 * gs;
const draw = (k) => {
ctx.beginPath();
switch(k) {
case 0:
ctx.moveTo(x - gsh, y - gs);
ctx.lineTo(x + gsh, y - gs);
ctx.lineTo(x + gsh, y + gs);
ctx.lineTo(x - gsh, y + gs);
break;
case 1:
ctx.moveTo(x, y - gs);
ctx.lineTo(x, y + gs);
break;
}
ctx.stroke();
x += size;
};
for (let i = 0; i < num.length; ++i) {
draw(num[i]);
if (i == 0) draw(-1);
}
};
const viewIn = (viewAng, viewArc, from, to) => {
const lo = arcMod(from - viewAng);
const hi = arcMod(to - viewAng);
return lo < viewArc && hi > -viewArc;
}
const drawScale = (a, b, rot, r, textSize, viewAng, viewArc, limit, clr,
showNum = true, str = '') => {
if (str == '') str += a;
const t = tau * Math.log10(a) - rot;
const bt = tau * Math.log10(b) - rot;
if (str.length > 3 && !viewIn(viewAng, viewArc, t, bt)) return;
const x = Math.cos(t);
const y = Math.sin(t);
drawLine(r * x, r * y, x, y, clr);
if (Math.abs(arcMod(bt - t)) < limit) return;
const rt = r - textSize;
if (showNum) drawText(rt * x, rt * y, str, clr);
if (str.length == 1) str += '.';
const d = 0.1 * (b - a);
let aa = a;
const rr = 1 - (1 - r) * 0.1;
for (let i = 0; i < 10;) {
const str2 = str + i;
++i;
const bb = i * d + a;
drawScale(
aa, bb, rot, rr, textSize, viewAng, viewArc, limit, clr, i > 1, str2);
aa = bb;
}
};
const update = () => {
zoom += zoomSmooth * (zoomTarg - zoom);
posX += zoomSmooth * (posXTarg - posX);
posY += zoomSmooth * (posYTarg - posY);
updateMouse();
if (click) {
const mouseAng = Math.atan2(mouseY, mouseX);
if (!pclick) {
clickAng = mouseAng;
clickRot = rot;
clickOutLine = Math.abs(arcMod(clickAng - outLine)) < 0.05 / zoom;
}
if (clickOutLine) {
setOutLine(mouseAng);
} else {
setRot(clickRot - mouseAng + clickAng);
}
}
ctx.setTransform(
halfWidth, 0, 0, halfHeight,
halfWidth * (1 - zoom * posY), halfHeight * (1 + zoom * posX));
const scale = 1 / ((halfWidth + halfHeight) * zoom);
ctx.lineWidth = 0.005;
const textSize = 50 * scale;
ctx.font = '0.07px sans-serif';
const viewAng = Math.atan2(posY, posX);
let viewArc = 0;
const arcs = [];
for (let cx = -1; cx < 2; cx += 2) {
for (let cy = -1; cy < 2; cy += 2) {
viewArc = Math.max(viewArc, Math.abs(arcMod(
viewAng - Math.atan2(posY + cy / zoom, posX + cx / zoom))));
}
}
drawCircle(0, 0, 3, '#212121');
drawCircle(0, 0, 1.3, '#FFFFFF20');
drawCircle(0, 0, 1, '#FFFFFF10');
ctx.lineWidth = 0.01;
drawRadialLine(0, 1.3, 0, '#FF0000');
drawRadialLine(0, 1.3, outLine, '#0000FF');
// drawCircle(mouseX, mouseY, 0.01, '#0F0');
// drawCircle(posX, posY, 0.01, '#0F0');
ctx.lineWidth = 0.005;
const zoomedOnCenter = (posX * posX + posY * posY) < 0.3 && zoom > 5;
if (!zoomedOnCenter) {
const limit = viewArc * 0.11;
for (let i = 1; i < 10; ++i) {
drawScale(i, i + 1, 0, 1.1, -textSize, viewAng, viewArc, limit, '#FFF');
drawScale(i, i + 1, rot, 0.8, textSize, viewAng, viewArc, limit, '#FFF');
}
}
drawCircle(0, 0, 0.05, '#FFF');
pclick = click;
window.requestAnimationFrame(update);
};
update();
}
window.addEventListener('load', onLoad);
document.addEventListener('wheel', onScroll);
</script>
<style>
body {
background-color: #212121;
margin: 0;
}
#head {
background-color: #424242;
width: 100%;
display: flex;
justify-content: space-around;
margin-bottom: 16px;
}
h1, #index {
color: #ffc107;
text-align: center;
font-family: monospace;
font-size: 42px;
flex-grow: 1;
padding: 16px;
margin: 0;
}
#index {
color: #ff5722;
text-decoration: none;
flex-grow: 0;
}
h2 {
color: #ff5722;
font-family: monospace;
}
a {
color: #ffc107;
font-family: monospace;
font-size: 16px;
cursor: pointer;
text-decoration: underline;
}
#wrap {
padding: 0 16px;
color: #f5f5f5;
font-family: monospace;
font-size: 16px;
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
#input_row {
display: flex;
flex-direction: row;
align-items: center;
}
#input_row > div {
display: flex;
flex-direction: column;
align-items: center;
}
#input_row > div > * {
resize: none;
margin-left: 8px;
margin-right: 8px;
}
#input_row > div > hr {
width: 60%;
}
</style>
</head>
<body>
<div id="head">
<a id="index" href="index.html"><</a>
<h1>Circular Slide Rule</h1>
</div>
<div id="wrap">
<div>
A circular version of a slide rule. Drag the inner wheel and blue line.
Scroll to zoom in/out. Inspired by
<a href="https://youtu.be/ZIQQvxSXLhI">this Mathologer video</a>.
<br>
<br>
</div>
<div id="input_row">
<div>
<input type="text" id="num_a" rows="1" cols="20" onchange="numAChange()">
<hr/>
<div>1</div>
</div>
<div>
=
</div>
<div>
<input type="text" id="num_b" rows="1" cols="20" onchange="numBChange()">
<hr/>
<input type="text" id="num_c" rows="1" cols="20" onchange="numCChange()">
</div>
</div>
<canvas id="view" width="800" height="800", onmousemove="onMouseMove(event)">
</canvas>
</div>
</body>
</html>