-
Notifications
You must be signed in to change notification settings - Fork 0
/
dots.html
542 lines (535 loc) · 19.4 KB
/
dots.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<!DOCTYPE html>
<html>
<head>
<title>Fourier</title>
<script type="text/javascript" src="math.js"></script>
<script type="text/javascript">
class State {
constructor() {
this.state = "BUILDING"
this.input_mode = undefined
this.input_func = undefined
this.svg_dom = undefined
this.svg_pieces = undefined
this.fourier_params = undefined
this.real_t = undefined
this.scaled_t = undefined
this.t_start = undefined
this.t_end = undefined
this.t_rate = undefined
this.original_len = undefined
this.num_series = undefined
this.delta_t = undefined
this.max = undefined
}
set_t_start(t_start) {
this.t_start = math.evaluate(t_start)
}
set_t_end(t_end) {
this.t_end = math.evaluate(t_end)
}
set_t_rate(t_rate) {
this.t_rate = math.evaluate(t_rate)
}
set_num_series(num_series) {
this.num_series = Math.floor(parseInt(num_series) / 2) * 2 + 1
}
set_delta_t(delta_t) {
this.delta_t = parseFloat(delta_t)
}
estimate_fourier() {
this.fourier_params = new Map()
var init_ft = true
var fts = new Map()
var max = 0
for (let i = 0; i < this.num_series; i++) {
var n = i - Math.floor(this.num_series / 2)
let sum = math.complex()
var common_exponent_terms = -2 * Math.PI * n
for (let a = 0; a < 1; a += this.delta_t) {
var t = this.t_start + a * (this.t_end - this.t_start)
let ft;
if (init_ft) {
ft = this.current_point(t)
fts.set(a, ft)
max = Math.max(Math.abs(math.im(ft)), Math.abs(math.re(ft)), max)
} else {
ft = fts.get(a)
}
var exponent = (common_exponent_terms * a)
var c2 = math.complex(this.delta_t * Math.cos(exponent), this.delta_t * Math.sin(exponent))
var val = math.multiply(ft, c2)
sum = math.add(sum, val)
}
this.fourier_params.set(n, sum)
init_ft = false
}
this.max = max
console.log(this.fourier_params)
}
async process_svg(text) {
DomProxy.svg_div.style = ""
this.svg_dom = (new DOMParser).parseFromString(text, "image/svg+xml").documentElement
this.svg_dom.style="width:800px; height:auto"
DomProxy.svg_container.append(this.svg_dom)
this.input_mode = "SVG"
this.svg_pieces = []
var total_length = 0
for (var el of this.svg_dom.querySelectorAll("*")) {
if (el instanceof SVGGeometryElement) {
var len = el.getTotalLength()
var start = total_length
total_length += len
var end = total_length
this.svg_pieces.push({start: start, end: end, el: el})
}
}
this.t_start = 0
this.t_end = total_length
this.t_rate = this.t_end / 10
this.original_len = total_length
DomProxy.t_start.value = this.t_start
DomProxy.t_end.value = this.t_end
DomProxy.t_rate.value = this.t_rate
this.estimate_fourier()
set_zoom_to_suggested()
this.state = "DONE"
}
async set_uploaded_file(file) {
await file.text().then(text => this.process_svg(text))
}
async set_svg_path(svg_path) {
await fetch(svg_path)
.then(response => response.text())
.then(text => this.process_svg(text))
}
set_input_func(function_text) {
this.input_mode = "FUNC"
this.input_func = math.compile(function_text)
this.estimate_fourier()
this.state = "DONE"
}
reset_t() {
this.real_t = undefined
this.scaled_t = undefined
}
advance_t(new_time) {
// ms -> s
new_time /= 1000
if (this.real_t === undefined) {
this.real_t = new_time
this.scaled_t = this.t_start
} else {
let real_time_advance = new_time - this.real_t
this.real_t = new_time
var direction = Math.sign(this.t_end - this.t_start)
this.scaled_t += (real_time_advance) * this.t_rate * direction
while (this.scaled_t > this.t_end) {
this.scaled_t -= this.t_end - this.t_start
}
}
DomProxy.t.value = this.scaled_t
}
t() {
return this.scaled_t
}
i() {
if (this.input_mode == "SVG") {
return this.scaled_t / this.original_len
}
return (this.scaled_t - this.t_start) / this.t_end - this.t_start
}
current_fourier_approx() {
var ret = []
var common_exponent_terms = 2 * Math.PI * this.i()
var gen = function(n) {
var c = this.fourier_params.get(n)
var exponent = common_exponent_terms * n
var exp = math.complex(Math.cos(exponent), Math.sin(exponent))
var val = math.multiply(c, exp)
ret.push({n: n, val: val})
}.bind(this)
gen(0)
for (let i = 1; i <= (this.num_series - 1) / 2; i++) {
gen(i)
gen(-i)
}
return ret
}
find_svg_piece(t) {
var min = 0;
var max = this.svg_pieces.length - 1;
while (min <= max) {
var i = Math.floor((min + max) / 2)
var el = this.svg_pieces[i]
if (el.end <= t) {
min = i + 1
} else if (el.start > t) {
max = i - 1
} else {
return el
}
}
return null
}
current_point(t) {
if (t === undefined) {
t = this.t()
}
if (this.input_mode == "FUNC")
return math.complex(this.input_func.evaluate({t: t}))
else {
var found = null
if (this.svg_pieces.length > 3) {
found = this.find_svg_piece(t)
} else {
for (let el of this.svg_pieces) {
if (t >= el.end) {
continue
}
found = el
break;
}
}
var point = found.el.getPointAtLength(t - found.start)
return math.complex(point.x, point.y)
}
console.log("Nothing matched in current_point!")
debugger;
return
}
}
const DomProxy = new Proxy(Object.freeze({}), {
get: function(target, prop, receiver) {
return document.getElementById(prop)
}
})
const ZOOM_BASE = 1.5
function calc_zoom(val) {
let float = parseFloat(val)
if (float < 0) {
float = ZOOM_BASE ** float
}
return float
}
function append_point(history, point) {
history.push(point)
while (history.length > Math.max(HISTORY_LEN, 1)) {
history.shift()
}
}
function draw_graph(context, width, height, zoom, view_width, view_height) {
context.clearRect(0, 0, width, height)
context.strokeRect(0, 0, width, height)
context.setTransform(zoom, 0, 0, zoom, width/2, height/2)
context.beginPath()
context.moveTo(-view_width/2, -CENTER_Y)
context.lineTo(view_width/2, -CENTER_Y)
context.moveTo(-CENTER_X, -view_height/2)
context.lineTo(-CENTER_X, view_height/2)
context.stroke()
}
function draw_points(context, history, dot_rad) {
context.beginPath()
for (let el of history) {
var x = math.re(el) - CENTER_X
var y = math.im(el) - CENTER_Y
context.moveTo(x+dot_rad, y)
context.arc(x, y, dot_rad, 0, Math.PI * 2)
}
context.stroke()
var old_style = context.strokeStyle
var last = history[history.length - 1]
context.strokeStyle = "yellow"
context.beginPath()
context.arc(math.re(last) - CENTER_X, math.im(last) - CENTER_Y, dot_rad, 0, Math.PI * 2)
context.stroke()
context.strokeStyle = old_style
}
let STATE;
var time_plot_ctx;
var fourier_ctx;
var anim_frame;
var time_tail = []
var fourier_tail = []
var HISTORY_LEN = 100
var CENTER_X = 0
var CENTER_Y = 0
var FOLLOW = false
function draw_plot(ts) {
//console.log("Drawing...")
STATE.advance_t(ts)
if (STATE.state == "DONE") {
const WIDTH = DomProxy.time_plot.width
const HEIGHT = DomProxy.time_plot.height
const ZOOM = calc_zoom(DomProxy.zoom_text.value)
const VIEW_WIDTH = WIDTH / ZOOM
const VIEW_HEIGHT = HEIGHT / ZOOM
const DOT_RAD = Math.min(VIEW_HEIGHT / 240, VIEW_WIDTH / 240)
var val = STATE.current_point()
if (FOLLOW) {
CENTER_X = Math.round(math.re(val))
CENTER_Y = Math.round(math.im(val))
DomProxy.graph_x.value = CENTER_X
DomProxy.graph_y.value = CENTER_Y
}
time_plot_ctx.save()
draw_graph(time_plot_ctx, WIDTH, HEIGHT, ZOOM, VIEW_WIDTH, VIEW_HEIGHT)
append_point(time_tail, val)
draw_points(time_plot_ctx, time_tail, DOT_RAD)
time_plot_ctx.restore()
fourier_ctx.save()
draw_graph(fourier_ctx, WIDTH, HEIGHT, ZOOM, VIEW_WIDTH, VIEW_HEIGHT)
var fourier_fragments = STATE.current_fourier_approx()
var sum = math.complex()
fourier_ctx.beginPath()
fourier_ctx.moveTo(-CENTER_X, -CENTER_Y)
for (var frag of fourier_fragments) {
sum = math.add(sum, frag.val)
fourier_ctx.lineTo(math.re(sum) - CENTER_X, math.im(sum) - CENTER_Y)
}
fourier_ctx.stroke()
append_point(fourier_tail, sum)
draw_points(fourier_ctx, fourier_tail, DOT_RAD)
fourier_ctx.restore()
}
anim_frame = window.requestAnimationFrame(draw_plot)
}
function stop() {
if (anim_frame !== undefined) {
window.cancelAnimationFrame(anim_frame)
anim_frame = undefined
}
}
function play() {
stop()
anim_frame = window.requestAnimationFrame(draw_plot)
}
function reset_t() {
STATE.reset_t()
}
var LAST_WHEEL = 0
const WHEEL_MIN_INTERVAL = 100
function handle_wheel(e) {
e.preventDefault()
var ts = e.timeStamp
if (ts - LAST_WHEEL > WHEEL_MIN_INTERVAL) {
LAST_WHEEL = ts
if (Math.abs(e.deltaX) > Math.abs(e.deltaY)) {
return
}
var zoom = parseFloat(DomProxy.zoom_text.value)
if (e.deltaY > 0) {
zoom -= 1
} else if (e.deltaY < 0) {
zoom += 1
}
DomProxy.zoom_text.value = zoom
DomProxy.zoom.value = zoom
}
}
function update_t() {
// TODO
}
function set_zoom_to_suggested() {
const WIDTH = DomProxy.time_plot.width
const HEIGHT = DomProxy.time_plot.height
const MIN = Math.min(WIDTH, HEIGHT)
var max_ft = STATE.max * 1.4
var zoom;
if (MIN < max_ft) {
zoom = Math.log(MIN/max_ft) / Math.log(ZOOM_BASE)
} else {
zoom = MIN / max_ft
}
zoom = Math.sign(zoom) * Math.ceil(Math.abs(zoom))
DomProxy.zoom_text.value = zoom
DomProxy.zoom.value = zoom
CENTER_X = 0
CENTER_Y = 0
DomProxy.graph_x.value = 0
DomProxy.graph_y.value = 0
}
function handle_pointermove(e) {
e.preventDefault()
var x = e.movementX
var y = e.movementY
const ZOOM = calc_zoom(DomProxy.zoom_text.value)
// 1 = primary button
if (e.buttons & 1) {
CENTER_X = Math.round(CENTER_X - x / ZOOM)
CENTER_Y = Math.round(CENTER_Y - y / ZOOM)
DomProxy.graph_x.value = CENTER_X
DomProxy.graph_y.value = CENTER_Y
}
}
function remove_children(node) {
while (node.firstChild) {
node.removeChild(node.firstChild)
}
}
async function reinit(e) {
stop()
STATE = new State()
time_tail =[]
fourier_tail = []
DomProxy.svg_div.style = "display: none"
remove_children(DomProxy.svg_container)
STATE.set_t_start(DomProxy.t_start.value)
STATE.set_t_end(DomProxy.t_end.value)
STATE.set_t_rate(DomProxy.t_rate.value)
STATE.set_num_series(DomProxy.num_series.value)
STATE.set_delta_t(DomProxy.delta_t.value)
STATE.reset_t()
if (DomProxy.svg_input_radio.checked) {
await STATE.set_svg_path(DomProxy.svg_src.value)
} else if (DomProxy.func_input_radio.checked) {
STATE.set_input_func(DomProxy.input_func.value)
} else {
await STATE.set_uploaded_file(DomProxy.file_input.files[0])
}
var param_container = DomProxy.param_container
remove_children(param_container)
for (const [key, value] of STATE.fourier_params) {
var new_node = document.createElement("code")
new_node.textContent = String.prototype.concat("Frequency ", key,": ", value, "; ")
param_container.append(new_node)
}
play()
}
function onload() {
time_plot_ctx = DomProxy.time_plot.getContext('2d')
fourier_ctx = DomProxy.fourier_plot.getContext('2d')
DomProxy.svg_src.addEventListener('change', () => {
DomProxy.svg_input_radio.checked = true
reinit()
})
DomProxy.input_func.addEventListener('change', () => {
DomProxy.func_input_radio.checked = true
reinit()
})
DomProxy.t_start.addEventListener('change', (e) => STATE.set_t_start(e.target.value))
DomProxy.t_end.addEventListener('change', (e) => STATE.set_t_end(e.target.value))
DomProxy.t_rate.addEventListener('change', (e) => STATE.set_t_rate(e.target.value))
DomProxy.num_series.addEventListener('change', reinit)
DomProxy.t.addEventListener('change', update_t)
DomProxy.reinit.addEventListener('click', (e) => {
DomProxy.t_start.value = "0"
DomProxy.t_end.value = "2*pi"
DomProxy.t_rate.value = "2*pi"
reinit()
})
DomProxy.delta_t.addEventListener('change', reinit)
DomProxy.use_suggested.addEventListener('click', set_zoom_to_suggested)
for (let el of document.querySelectorAll("input[type=radio]")) {
el.addEventListener('change', reinit)
}
DomProxy.file_input.addEventListener('change', () => {
DomProxy.file_input_radio.checked = true // Doesn't trigger event listener?
reinit()
})
DomProxy.follow.addEventListener('change', (e) => FOLLOW = e.target.checked)
DomProxy.reset.addEventListener('click', reset_t)
DomProxy.pause.addEventListener('click', stop)
DomProxy.play.addEventListener('click', play)
DomProxy.zoom_text.addEventListener('change', (e) => DomProxy.zoom.value = e.target.value)
DomProxy.zoom.addEventListener('input', (e) => DomProxy.zoom_text.value = e.target.value)
DomProxy.graph_x.addEventListener('change', (e) => CENTER_X = parseInt(e.target.value))
DomProxy.graph_y.addEventListener('change', (e) => CENTER_Y = parseInt(e.target.value))
DomProxy.tail_len.addEventListener('change', (e) => HISTORY_LEN = e.target.value)
DomProxy.time_plot.addEventListener('wheel', handle_wheel)
DomProxy.time_plot.addEventListener('pointermove', handle_pointermove)
DomProxy.fourier_plot.addEventListener('wheel', handle_wheel)
DomProxy.fourier_plot.addEventListener('pointermove', handle_pointermove)
reinit()
}
document.addEventListener("DOMContentLoaded", onload)
</script>
</head>
<body>
<h1>Dot Plot</h1>
<h2>About</h2>
<p>
Plot a function or an SVG with dots! Be aware that the SVG URL feature depends on the server hosting the URL allowing loads via CORS, so it is unreliable. It's better to just download any SVG you want to test then upload it. This page is entirely local, it doesn't communicate with any server except to load any SVG URLs you enter. Source code is available <a href="https://github.com/ch33zer/fourier_playground" target="_blank">here</a>. This makes use of <a href="https://mathjs.org/" target="_blank"><code>math.js</code></a>
</p>
<p>Here are a few interesting functions you can plug in:</p>
<ul>
<li><code>20 * (sin(2*t) + i cos(t))</code></li>
<li><code>20 * round(t)(1 + i)</code></li>
</ul>
<p>Here are a few SVGs for you to try out:</p>
<ul>
<li>Dodecahedron: <a href="https://commons.wikimedia.org/wiki/Category:SVG_files#/media/File:Dodecahedron_schlegel.svg" target="_blank">https://commons.wikimedia.org/wiki/Category:SVG_files#/media/File:Dodecahedron_schlegel.svg</a></li>
<li>Fourier portrait: <a href="https://www.dropbox.com/s/tnim0el257lfzom/Biocinematics_Fourier_Vector.svg?dl=0" target="_blank">https://www.dropbox.com/s/tnim0el257lfzom/Biocinematics_Fourier_Vector.svg</a></li>
<li>SVG Repository: <a href="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/" target="_blank">https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/</a></li>
</ul>
<h2>Input</h2>
<div>
<label>
<input type="radio" id="func_input_radio" name="input_radio" value="func" checked>
<label> Formula <input type="text" id="input_func" value="20 * e ^ (-i * t) + 20"> </label>
<a href="https://mathjs.org/docs/expressions/syntax.html" target="_blank">Syntax</a>
</label>
</div>
<div>
<label>
<input type="radio" id="svg_input_radio" name="input_radio" value="svg">
<label> SVG URL <input type="text" id="svg_src" value=""> </label>
</label>
</div>
<div>
<label>
<input type="radio" id="file_input_radio" name="input_radio" value="file">
SVG File
<input type="file" id="file_input" accept=".svg">
</label>
</div>
<br/>
<label> T Rate (change per second) <input type="text" id="t_rate" value="2*pi"> </label>
<label> T <input type="text" id="t" value="-1"> </label>
<div style="display: none">
Changing these fields for SVGs may cause strange behavior
<br />
<span style="border-style: solid; border-color: red">
<label> T Start <input type="text" id="t_start" value="0"> </label>
<label> T End <input type="text" id="t_end" value="2*pi"> </label>
</span>
<input type="button" id="reset" value="Reset T to 0">
<input type="button" id="pause" value="Pause">
<input type="button" id="play" value="Play">
<input type="button" id="reinit" value="Reinitialize bounds (resets animation)">
</div>
<br/>
<label> Tail Length <input type="text" id="tail_len" value="100"> </label>
<div style="display: none">
<label> Zoom
<input type="range" id="zoom" min="-10" max="10" value="1">
<input type="text" id="zoom_text" value="1">
<input type="button" id="use_suggested" value="Use suggested">
</label>
<label> Graph X <input type="text" id="graph_x" value="0"> </label>
<label> Graph Y <input type="text" id="graph_y" value="0"> </label>
<label> Follow plot <input type="checkbox" id="follow"> </label>
</div>
<div id="svg_div" style="display: none">
<h2> SVG </h2>
<span id="svg_container"></span>
</div>
<h2>Function</h2>
<div>
<span>
<canvas id="time_plot" width="800px" height="400px" style="touch-action: none"></canvas>
</span>
</div>
<div style="display: none">
<h2>Fourier</h2>
<label> Num frequencies <input type="text" id="num_series" value="101"> </label>
<label> Integral accuracy <input type="text" id="delta_t" value=".001"> </label>
<br />
<canvas id="fourier_plot" width="800px" height="400px" style="touch-action: none"></canvas>
<br />
<div id="param_container" style="width: 800px; overflow: scroll; white-space: nowrap">
</div>
</div>
</body>
</html>