-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconformal_plot.htm
3391 lines (3300 loc) · 115 KB
/
conformal_plot.htm
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>z+t - earth+</title>
<meta charset="utf-8">
<!-- by David Bau, durring snowstorm Nemo 2013 -->
<!-- a tool for visualizing complex functions, -->
<!-- a demonstration of canvas and web workers -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- STYLESHEET - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<style>
body { padding:0; margin: 0; background: black; }
div.fullpage { display:table; width: 100%; height:100%; }
div.fullpage > div.workspace { display: table-cell; }
canvas { position: absolute; }
img { display: none; }
#editcontrol {
display:inline-block;
width: 48%;
}
input.button {
width:100%;
height: 48px;
line-height: 44px;
padding: 0;
vertical-align: middle;
cursor: text;
}
#timecontrol {
position: relative;
display: none;
table-layout: fixed;
width:48%;
vertical-align: middle;
cursor: pointer;
color: white;
text-shadow: 1px 1px 5px #000;
font:12pt Arial;
font-weight:bold;
text-align:center;
-webkit-user-select: none;
-moz-user-select: none;
}
#timecontrol .layout {
display: table-cell;
vertical-align: top;
width: 48px;
}
#timeline.layout {
width: 99%;
padding: 30px 12px 0 12px;
}
#thinline {
height: 1px;
background-color: #bbb;
top: 10px;
}
#slider {
width: 128px;
margin-left: -65px;
}
#timecontrol .triangle {
display: inline-block;
width:0;
height:0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #bbb;
margin-bottom: 6px;
}
#timeline:hover .triangle {
border-bottom: 10px solid white;
}
#timecontrol .clicker {
text-align:center;
height:18px;
width:32px;
padding:16px 8px;
color: #bbb;
border-radius:10px;
bottom:0;
}
#timecontrol .clicker:hover {
color: white;
font-weight:bold;
background: rgba(0,0,0,.5);
}
.button {
font:18pt Arial;
font-weight:bold;
text-align:center;
padding:8px 24px;border-radius:10px;
color:white;background:rgba(0,0,0,.5);
transition:border-color 0.4s;
-webkit-transition:border-color 0.4s;
-moz-transition:border-color 0.4s;
-o-transition:border-color 0.4s;
border:3px solid transparent;
cursor: pointer;
}
.button:hover, .button:focus {
border-color: white;
}
div.allgroup {
position:absolute;
bottom:0;
width: 100%;
height: 148px;
}
.group {
position:absolute;
display: inline-block;
bottom:0;
padding: 20px;
vertical-align:bottom;
-webkit-user-select: none;
-moz-user-select: none;
}
.centergroup {
position: absolute;
display: inline-block;
bottom: 0;
padding: 0 0 20px 0;
vertical-align:bottom;
width: 100%;
text-align: center;
}
.loading {
background:url(data:image/gif;base64,R0lGODlhKwALAPEAAAAAAP8AAHwAAP8AACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAKwALAAACMoSOCMuW2diD88UKG95W88uF4DaGWFmhZid93pq+pwxnLUnXh8ou+sSz+T64oCAyTBUAACH5BAkKAAAALAAAAAArAAsAAAI9xI4IyyAPYWOxmoTHrHzzmGHe94xkmJifyqFKQ0pwLLgHa82xrekkDrIBZRQab1jyfY7KTtPimixiUsevAAAh+QQJCgAAACwAAAAAKwALAAACPYSOCMswD2FjqZpqW9xv4g8KE7d54XmMpNSgqLoOpgvC60xjNonnyc7p+VKamKw1zDCMR8rp8pksYlKorgAAIfkECQoAAAAsAAAAACsACwAAAkCEjgjLltnYmJS6Bxt+sfq5ZUyoNJ9HHlEqdCfFrqn7DrE2m7Wdj/2y45FkQ13t5itKdshFExC8YCLOEBX6AhQAADsAAAAAAAAAAAA=);
background-position: 95% center;
background-repeat: no-repeat;
}
#about {
top:0; left: 0;
width: 35%;
height: 42%;
z-index: 1;
}
#about .box {
background:rgba(0,0,0,.5);
padding: 12px;
border-radius:10px;
color:white;
width: 100%;
height: 100%;
}
#about .scroller {
height: 100%;
overflow-y: auto;
font:10pt Arial;
}
#about ::-webkit-scrollbar {
width: 12px;
}
#about ::-webkit-scrollbar-track-piece {
background: rgba(0, 0, 0, 0.4);
}
#about ::-webkit-scrollbar-thumb {
border-radius: 10px;
background: rgba(88,88,88,0.7);
}
#about h2:nth-of-type(1) {
margin-top: 0;
}
#about p {
margin: 0 0 8px 0;
line-height: 18px;
}
#about a {
color: white;
font-weight: bold;
text-decoration: none;
font-size: 12pt;
white-space: nowrap;
}
#about a:hover {
color: skyblue;
text-decoration: underline;
}
.quiet {
opacity: 0;
transition:border-color,opacity 0.2s;
-webkit-transition:border-color,opacity 0.2s;
-moz-transition:border-color,opacity 0.2s;
-o-transition:border-color,opacity 0.2s;
}
#about.quiet:hover {
opacity: 1;
}
div.allgroup:hover .quiet {
opacity: 1;
}
</style>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- HELP TEXT - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
</head><body><div id="about" class="quiet group"><div class="box"><div class="scroller">
<h2>Complex Function Viewer</h2>
<p>This tool visualizes any complex-valued
function as a conformal map by assigning a color to each point in
the complex plane according to the function's value at that point.</p>
<p>Enter any expression in z.</p>
<p>The identity function <a href="#z">z</a> shows how colors are assigned:
a gray ring at <a href="#z disk(16(|z|-1))">|z| = 1</a> and a black and
white circle around any <a href="#z disk(16|z|)">zero</a>
and colored circles around <a href="#z disk(16|z-1|)">1</a><a>,
</a><a href="#z disk(16|z-i|)">i</a>, <a href="#z disk(16|z+1|)">-1</a>,
and <a href="#z disk(16|z+i|)">-i</a>.
Checkers cover the plane in a 1/16th
<a href="#z disk(1/(16(|z|-1))) disk(1/(16|z|))">unit grid</a>.
Colors are turquoise in the
<a href="#z disk(|z-1.5|)">positive</a> direction,
red in the <a href="#z disk(|z+1.5|)">negative</a>, gold-green
towards <a href="#z disk(|z-1.5i|)">+i</a>, purplish
towards <a href="#z disk(|z+1.5i|)">-i</a>,
and darker towards <a href="#z disk(3/|z|)">infinity</a>.
There is also a colored circle towards infinity at
<a href="#8z disk(2/|z|)">|z| > 16</a>
that can be seen at any pole towards infinity such as
in <a href="#1/z">1/z</a>.
</p><p>Here are some example functions to try:</p>
<p style="text-align:center;">
<a href="#z^2">z^2</a><br>
<a href="#zz*">zz*</a><br>
<a href="#(z+1)/(z-1)">(z+1)/(z-1)</a><br>
<a href="#sin(z)">sin(z)</a><br>
<a href="#e^z">e^z</a><br>
<a href="#log(z)">log(z)</a><br>
<a href="#sech(z)">sech(z)</a><br>
<a href="#arctan(z)">arctan(z)</a><br>
<a href="#z^3-1">z^3-1</a><br>
<a href="#0.926(z+7.3857e-2 z^5+4.5458e-3 z^9)">0.926(z<wbr>+7.3857e-2 z^5<wbr>+4.5458e-3 z^9)</a><br>
Jacobi elliptic <a href="#sn(z,0.3)">sn(z, 0.3)</a><br>
Gamma function <a href="#gamma(z)">gamma(z)</a><br>
Iterated function <a href="#iter(z+z'^2,z,12)">iter(z+z'^2,z,12)</a>
</p>
<h2>Conformal Maps on the Globe</h2>
<p>Conformal maps have their history in 18th century mapmaking,
when new mathematical developments allowed mapmakers to understand
how to precisely eliminate local shape distortions in maps.
Click the <a href="#m&b=earth">⊕</a> button in the lower
right corner to switch to a conformal mapping of the surface
of the earth. Conformal maps preserve local angles everywhere,
although they may distort sizes to do so.
</p><p>The Mercator projection is an example. Try:</p>
<p align="center"><a href="#e^iz&b=earth">e^iz</a></p>
<p>The azimuthal stereographic projection is a beautiful ancient technique
that is also conformal, but it is usually broken into two hemispheres:</p>
<p align="center"><a href="#disk(z+1)i(z+1-i)/(z+1+i)-disk(z-1)i(z-1-i)/(z-1+i)&b=earth&z=3">...i(z+1-i)/(z+1+i)...</a>
</p><p>Lagrange advocated another conformal projection that squeezes the
entire globe into a single circle:</p>
<p align="center"><a href="#(disk(z)(z-i)/(z+i))^2&b=earth&z=2.5">(disk(z)(z-i)<wbr>/(z+i))^2</a></p>
<p>Read more about conformal projections in cartography on
Carlos A. Furuti's nicely illustrated
<a href="http://www.progonos.com/furuti/MapProj/Normal/ProjConf/projConf.html">mapmaking <wbr>website</a>.
Or Donald Fenna's mathematical mapmaking book,
<a href="http://books.google.com/books?id=8LZeu8RxOIsC">Cartographic <wbr>Science</a>.</p>
<h2>Animating Conformal Maps</h2>
<p>To visualize the relationships within families of complex functions,
parameterize them with the variables t, u, s, r, or n. The tool will render
a range of complex functions for values of the parameter, adjustable with
a slider or shown in an aimation. The parameter t will vary linearly
from 0 to 1; u will circle through complex units; s follows a sine wave
between -1 and 1; r follows a sine wave from 0 to 1 and back; and
n counts integers from 1 to 60.</p>
<p>For example, to see the relationship between <a href="#z^3">z^3</a> and
<a href="#z^3+1">z^3+1</a>, simply view:</p>
<p style="text-align:center;"><a href="#z^3+t&a=play">z^3+t</a></p>
<p>On the globe, multiplying by powers of unity will rotate the world on its
axis:</p>
<p style="text-align:center;"><a href="#u(z-i)/(z+i)&a=repeat&b=earth">u(z-i)/(z+i)</a></p>
<p>Because more than 300 frames are computed, parameterized expressions can
take a long time to fully render. A rough, blurry sketch is drawn quickly,
and finer-grained rendering will follow for several minutes. When done,
the frames will be antialiased and animated at 24 fps.</p>
<p>Simple families of rational function produce mesmerizing animations:
</p><p style="text-align:center;">
<a href="#z^2+s&a=repeat">z^2+s</a><br>
<a href="#z^3+1+u&a=repeat">z^3+1+u</a><br>
<a href="#z^5+uz+1&a=repeat">z^5+uz+1</a><br>
<a href="#z^2/(r+z)&a=repeat">z^2/(r+z)</a><br>
</p>
<p>Iterated functions and sums can also be animated. For example, the
following are well-known Taylor series for
<a href="#e^z">e^z</a>,
<a href="#sin(z)">sin(z)</a>,
<a href="#1/(1-z)">1/(1-z)</a>, and
<a href="#log(1-z)">log(1-z)</a>:
</p><p style="text-align:center;">
<a href="#sum(z^n/n!)&z=36&a=play">sum(z^n/n!)</a><br>
<a href="#sum((-1)^n/(2n+1)! z^(2n+1))&z=36&a=play">sum((-1)^n/(2n+1)! z^(2n+1))</a><br>
<a href="#sum(z^n)&a=play">sum(z^n)</a><br>
<a href="#sum(z^(n+1)/(n+1))&a=play">sum(z^(n+1)/(n+1))</a><br>
</p>
<p>The radii of convergence can clearly be seen
in the last two examples</p>
<address style="padding:16px;">tool by
<a href="http://davidbau.com/archives/2013/02/10/conformal_map_viewer.html">David Bau</a></address>
</div></div></div>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- UI CONTROLS - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<div class="fullpage">
<div class="workspace">
<canvas id="c" width="1366" height="654"></canvas>
<div class="allgroup">
<div class="centergroup">
<div id="timecontrol" style="display: inline-table;">
<div class="layout"><div id="play" class="quiet clicker">⇥</div></div>
<div id="timeline" class="layout">
<div id="thinline" class="quiet"></div>
<div id="slider" style="margin-left: -65px;">
<div class="triangle quiet"></div>
<div id="time" class="label">t = 0.000</div>
</div><!--slider-->
</div><!--timeline-->
<div class="layout"><div id="repeat" class="quiet clicker">↻</div></div>
</div><!--timecontrol-->
<br>
<div id="editcontrol" class="loading">
<input id="expr" class="button" style="text-align:center;" size="25" value="z+t">
</div>
</div><!--centergroup-->
<div class="quiet group" style="left:0;">
<div id="zoomin" class="button" title="zoom in">+</div>
<div id="zoomout" class="button" title="zoom out">-</div>
</div><!--group left-->
<div class="quiet group" style="right:0;">
<div id="earth" class="button" title="change map">⊕</div>
<div id="fullscreen" class="button" title="full screen">⇕</div>
</div><!--group right-->
</div><!--allgroup-->
</div><!--workspace-->
</div><!--fullpage-->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- UI SCRIPT - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<script>
function init() {
// Here is the UI.
var default_expr = 'z';
var default_extent = 4.5;
var t_quanta = 360;
var animation_fps = 24;
var change_delay = 300;
var sethash_delay = 3000;
var sethash_timer = null;
var blit_blocksize = 128;
var use_animation_frames = true;
var use_threads = true;
var in_memory_frames = (1 + t_quanta) * 2 + 100;
var use_temp_filesystem = 20 * 1024 * 1024 * 1024;
var min_filesystem_space = 1 * 1024 * 1024 * 1024;
var cache_version = 1;
var min_elapsed_to_cache = 10 * 1000;
var checkpoint_interval = 5 * 60 * 1000;
var current_worker = null;
var worker_script_url = null;
var cache_system = null;
var redraw_failure_retries = 3;
var redraw_failures = 0;
var bitmap_load_failure_retries = 3;
var bitmap_load_failures = 0;
var start_animation_failure_retires = 3;
var start_animation_retries = 0;
var reading_input = null;
var maincanvas = ebi('c');
var mainctx = maincanvas.getContext('2d');
var loaded_bitmaps = {};
var drawn = { };
var requested = {
fn: function(z) { return {r:0,i:0}; },
expr: '',
canon: '',
extent: default_extent,
width: 0,
height: 0,
parameterized: false,
animode: null,
mode: null
};
var timelineui = {
mousepos: 0,
animode: null,
accepting: false,
frame: 0,
interval: Math.floor(1000/animation_fps),
starttime: 0,
cancelTimer: (function() {
if (this.timer) { window.clearTimeout(this.timer); }
if (this.aframe) { window.cancelAnimationFrame(this.aframe); }
this.timer = null;
this.aframe = null;
}),
hasTimer: (function() {
return (this.timer !== null || this.aframe !== null);
}),
delayTimer: (function(cb, ms) {
this.cancelTimer();
var tui = this;
this.timer = window.setTimeout(
function() { tui.timer = null; cb(); }, ms);
}),
animationTimer: (function(cb, ms) {
this.cancelTimer();
var tui = this;
if (use_animation_frames && window.requestAnimationFrame) {
this.aframe = window.requestAnimationFrame(
function() { tui.aframe = null; cb(); });
} else {
this.timer = window.setTimeout(
function() { tui.timer = null; cb(); }, ms);
}
}),
timer: null,
aframe: null
};
var offscreen = {
frameshown: 0,
frames: null
};
function inspect() {
return { drawn: drawn, offscreen: offscreen };
}
window.debug_inspect = inspect;
function run() {
// Load web worker code if facilities are available.
if (use_threads && window.Worker && window.Blob
&& window.URL && window.URL.createObjectURL) {
worker_script_url = window.URL.createObjectURL(new Blob(
[ebi('dispatch').textContent,
ebi('render').textContent,
ebi('compute').textContent],
{type: "text/javascript"}));
}
// Init caching system if facilities are availble.
cache_system = init_cache_system(
cache_version, in_memory_frames,
use_temp_filesystem, min_filesystem_space);
// Listen to various ui gesture events.
bind(window, 'resize', redraw);
bind(window, 'hashchange', readhash);
bind(ebi('expr'), 'keyup', setfunc);
bind(ebi('expr'), 'blur', revertfunc);
bind(ebi('zoomin'), 'click', zoomin);
bind(ebi('zoomout'), 'click', zoomout);
bind(ebi('timeline'), 'mousemove', movetime);
bind(ebi('timecontrol'), 'mouseout', movetimereset);
bind(ebi('timeline'), 'click', cancelplay);
bind(ebi('repeat'), 'click', toggleplay);
bind(ebi('play'), 'click', toggleplay);
bind(ebi('earth'), 'click', togglebitmap);
// Enable fullscreen button if supported.
if (!(document.fullscreenEnabled ||
document.mozFullscreenEnabled ||
document.webkitFullscreenEnabled)) {
bind(ebi('fullscreen'), 'click', opennewwindow);
} else {
bind(ebi('fullscreen'), 'click', togglefullscreen);
}
// Load initial state from hash, or start with a default.
if (/^#/.exec(window.location.hash)) {
readhash();
} else {
ebi('expr').value = default_expr;
setfunc(true);
}
// Done.
}
function ebi(id) {
return document.getElementById(id);
}
function leftposition(e) {
for (var left = 0; e; e = e.offsetParent) { left += e.offsetLeft; }
return left;
}
function trim(s) {
if (s.trim) { return s.trim(); }
return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function bind(elt, ev, fn) {
if (elt.addEventListener) {
elt.addEventListener(ev, fn, false);
} else if (elt.attachEvent) {
elt.attachEvent('on' + ev, fn);
}
}
function setfunc(donow, noredraw) {
if (reading_input) {
window.clearTimeout(reading_input);
reading_input = null;
}
if (donow) {
readinput();
} else {
reading_input = window.setTimeout(readinput, change_delay);
}
function readinput() {
reading_input = null;
var s = trim(ebi('expr').value);
if (s == requested.expr) return;
var parsed = complex_expression(s);
if (parsed !== null) {
var animate = parsed.parameters.length > 0;
requested.fn = parsed.fn;
requested.expr = s;
requested.canon = parsed.fntext;
requested.extent = default_extent;
requested.parameterized = animate;
requested.caption = animate ? parsed.parameters[0].caption : null;
ebi('timecontrol').style.display = animate ? 'inline-table' : 'none';
if (!animate) { cancelplay(); }
if (noredraw) { return; }
sethash();
redraw();
}
}
}
function zoomin() {
requested.extent /= 2;
sethash();
redraw();
}
function zoomout() {
requested.extent *= 2;
sethash();
redraw();
}
function movetimereset(ev) {
if (ev.relatedTarget === null ||
!ebi('timeline').contains(ev.relatedTarget)) {
timelineui.accepting = false;
if (!timelineui.animode) {
timelineui.cancelTimer();
}
}
}
function movetime(ev) {
if (!timelineui.animode) {
timelineui.mousepos = ev.pageX - leftposition(ebi('thinline'));
if (timelineui.accepting) {
movetimeexec();
return;
}
if (!timelineui.hasTimer()) {
timelineui.delayTimer(movetimeexec, change_delay);
}
}
}
function movetimeexec() {
var timelinewidth = ebi('thinline').offsetWidth,
scaled = timelineui.mousepos / timelinewidth,
framenum = Math.min(t_quanta,
Math.max(0, Math.round(scaled * t_quanta)));
timelineui.accepting = true;
timelineui.frame = framenum;
shownearframe(framenum);
}
function movetimerefresh() {
// Called after animation frames have been cleared, to
// switch t to a frame that is included in the sample.
if (!timelineui.hasTimer()) {
timelineui.delayTimer(function() {
shownearframe(timelineui.frame);
}, change_delay * 2);
}
}
function cancelplay(ev) {
requested.animode = null;
stopanimation();
if (ev) { movetime(ev); }
sethash();
}
function toggleplay(ev) {
if (timelineui.animode) {
cancelplay();
} else {
stopanimation();
requested.animode = ev.target.id;
// if no frames, attempt to recompute and look again.
if (!offscreen.frames) {
redraw();
if (!offscreen.frames) {
requested.animode = null;
}
}
sethash();
if (requested.animode) {
startanimation()
}
}
}
function stopanimation() {
timelineui.cancelTimer();
timelineui.animode = null;
timelineui.timer = null;
syncbuffer();
movetimerefresh();
}
function startanimation() {
if (requested.animode === null) {
stopanimation();
return;
}
start_animation_failures = 0;
timelineui.cancelTimer();
timelineui.animode = requested.animode;
if (requested.animode != 'repeat') {
timelineui.starttime = (new Date).getTime();
showframe(0);
timelineui.frame = 0;
}
if (timelineui.frame + 1 >= offscreen.frames.length) {
showframe(0);
timelineui.frame = 0;
}
timelineui.animationTimer(play, timelineui.interval);
function play() {
if (!offscreen.frames) {
cancelplay();
return;
}
var newtime = (new Date).getTime(),
elapsed = newtime - timelineui.starttime,
elapsedframes = Math.floor(elapsed / timelineui.interval);
if (timelineui.animode == 'play' &&
elapsedframes >= offscreen.frames.length) {
timelineui.frame = offscreen.frames.length - 1;
shownearframe(timelineui.frame, true);
cancelplay();
return;
}
// Skip the first frame (same as the last) when looping animations.
timelineui.frame = (elapsedframes - 1)
% (offscreen.frames.length - 1) + 1;
shownearframe(timelineui.frame, true);
newtime = (new Date).getTime();
elapsed = newtime - timelineui.starttime;
timelineui.animationTimer(play, timelineui.interval -
(elapsed % timelineui.interval));
}
}
function shownearframe(framenum, animating) {
if (!offscreen.frames) { return; }
// Hunt for a frame that has been drawn rather than showing a blank frame.
for (var j = 0; j <= offscreen.frames.length; ++j) {
var k = animating ? -j : (1 - (j % 2) * 2) * (j + 1) >> 1;
var near = (framenum + offscreen.frames.length + k)
% offscreen.frames.length;
if (offscreen.frames[near].filled) {
showframe(near);
return;
}
}
showframe(framenum);
}
function showframe(framenum, force) {
if (!offscreen.frames) { return; }
var timelinewidth = ebi('thinline').offsetWidth,
quantized = framenum / t_quanta,
pegged = Math.max(0, Math.min(1, quantized)),
pos = Math.round(pegged * timelinewidth),
caption = ebi('time'),
slider = ebi('slider');
slider.style.marginLeft = (pos - slider.offsetWidth / 2 - 1) + 'px';
caption.innerHTML = requested.caption ? requested.caption(pegged) : '';
if (framenum == offscreen.frameshown && !force) { return; }
paintframe(offscreen.frames[framenum]);
offscreen.frameshown = framenum;
}
function paintframe(frame) {
if (frame.same_as !== null && offscreen.frames !== null) {
frame = offscreen.frames[frame.same_as];
}
if (frame.canvas !== null) {
mainctx.drawImage(frame.canvas, 0, 0);
} else {
mainctx.save();
if (frame.constant_rgb !== null) {
mainctx.fillStyle = 'rgb(' + frame.constant_rgb.join(',') + ')';
}
mainctx.fillRect(0, 0, drawn.width, drawn.height);
mainctx.restore();
}
}
// Returns the 2d drawing context for the given frame. If the given
// frame is also the one currently shown, then returns two contexts:
// one for the offscreen buffer, and one for the onscreen canvas.
function framectxes(framenum) {
if (!offscreen.frames) {
return [mainctx];
}
var frame = offscreen.frames[framenum];
if (frame.same_as !== null) { return []; }
if (frame.canvas === null) {
// Lazy creation of canvas
frame.canvas = document.createElement('canvas');
frame.canvas.width = drawn.width;
frame.canvas.height = drawn.height;
frame.ctx = frame.canvas.getContext('2d');
}
if (!timelineui.animode && (
offscreen.frameshown == framenum ||
offscreen.frames[offscreen.frameshown].same_as == framenum)) {
return [mainctx, frame.ctx];
}
return [frame.ctx];
}
// When animation timelineui.animode is flipped to false, the offscreen
// bitmap (if any) is synced with the main context, because it is allowed
// to get out of sync during offscreen.
function syncbuffer() {
if (!offscreen.frames) { return; }
showframe(offscreen.frameshown, true);
}
// Makes a copy of a canvas.
function clone_canvas(c) {
var newcanvas = document.createElement('canvas');
newcanvas.width = c.width;
newcanvas.height = c.height;
newcanvas.getContext('2d').drawImage(c, 0, 0);
window.cloned = newcanvas;
return newcanvas;
}
// Return an array of frames to cache.
function frames_to_cache() {
var caching_frames = offscreen.frames;
if (caching_frames === null) {
caching_frames = [{
canvas: clone_canvas(maincanvas),
drawn_pixels: maincanvas.width * maincanvas.height,
same_as: null,
constant_rgb: null,
filled: true
}];
}
return caching_frames;
}
function spawn_worker() {
try {
return new Worker(worker_script_url);
} catch (e) {
return null;
}
}
// The main redraw function. When any UI state may have changed,
// redraw is called to possibly restart the rendering jobs.
function redraw() {
// verify that what is being drawn is different from the request.
if (maincanvas.width != window.innerWidth ||
maincanvas.height != window.innerHeight) {
requested.width = maincanvas.width = window.innerWidth;
requested.height = maincanvas.height = window.innerHeight;
}
if (drawn.width == requested.width &&
drawn.height == requested.height &&
drawn.canon == requested.canon &&
drawn.mode == requested.mode &&
drawn.extent == requested.extent) {
return;
}
// before drawing, require that the bitmap be loaded.
if (!isbitmaploaded(requested.mode)) {
loadbitmap(requested.mode, redraw);
}
// cache away the previous render if not yet saved.
if (cache_system !== null && !drawn.finished
&& drawn.elapsed > min_elapsed_to_cache) {
var caching_drawn = drawn;
var caching_frames = frames_to_cache();
cache_system.save(caching_drawn, caching_frames);
}
// Try loading from cache. This is done asynchronously, so if the
// loading is successful, it kills and replaces current render.
if (cache_system !== null) {
cache_system.load(requested, function(m) {
restart_render_from_cache(m.drawn, m.frames);
}, function(e) {
// Not in the cache - ordinary, so nothing to do.
});
}
// Notate that we are working on drawing this request.
var new_job = {
width: requested.width,
height: requested.height,
canon: requested.canon,
mode: requested.mode,
extent: requested.extent,
expr: requested.expr,
state: null,
info: null,
starttime: (new Date).getTime(),
elapsed: 0,
finished: false,
};
// Sometimes when loading out of the browser cache, the canvas fails
// to work 100% on pageload; so just catch the exception and retry.
try {
start_render(new_job, null);
redraw_failures = 0;
} catch (e) {
redraw_failures += 1;
trace('redraw failure #' + redraw_failures + ': ' + e.message);
if (redraw_failures <= redraw_failure_retries) {
window.setTimeout(redraw, change_delay);
}
}
}
// Callback when the cache load was successful. This will
// kill the ongoing rendering job and replace with the new one
// if it is relevant.
function restart_render_from_cache(cached_job, cached_frames) {
// Only apply the cached data if it is relevant and helpful.
if (!(drawn.width == cached_job.width &&
drawn.height == cached_job.height &&
drawn.canon == cached_job.canon &&
drawn.mode == cached_job.mode &&
drawn.extent == cached_job.extent &&
cached_frames.length == (offscreen.frames ? offscreen.frames.length : 1)
)) {
trace('cached copy is not the same as what is being drawn');
trace('drawn: ' + JSON.stringify(drawn));
trace('cached: ' + JSON.stringify(cached_job));
return;
}
if (drawn.finished || (drawn.state !== null && !cached_job.finished &&
!(cached_job.state.opcount > drawn.state.opcount))) {
trace('cached copy (' + cached_job.state.opcount + ') ' +
'is less finished than what is drawn (' + drawn.state.opcount + ')');
return;
}
// First require that the bitmap be loaded.
if (!isbitmaploaded(requested.mode)) {
loadbitmap(requested.mode, function() {
restart_render_from_cache(cached_job, cached_frames);
});
return;
}
// Adjust the start time.
cached_job.starttime = (new Date).getTime() - cached_job.elapsed;
// Blit the shown frame to the screen.
if (cached_frames.length == 1) {
paintframe(cached_frames[0]);
}
start_render(cached_job, cached_frames);
}
// Driver for the main rendering loop.
function start_render(job_spec, cached_frames) {
// Overwrite current-job spec.
drawn = job_spec;
// Show in-progress UI.
ebi('editcontrol').className = 'loading';
// Prepare animation canvases if necessary.
var computation_frames = [0];
var drawing_frames = null;
var canvasarea = drawn.width * drawn.height;
if (requested.parameterized) {
computation_frames = [];
for (var frame = 0; frame <= t_quanta; ++frame) {
computation_frames.push(frame / t_quanta);
}
if (cached_frames) {
drawing_frames = cached_frames;
} else {
drawing_frames = [];
for (var frame = 0; frame <= t_quanta; ++frame) {
drawing_frames.push({
canvas: null,
ctx: null,
drawn_pixels: (frame == 0) ? canvasarea : 0,
same_as: null,
constant_rgb: null,
filled: (frame == 0)
});
}
}
offscreen.frames = drawing_frames;
startanimation();
} else {
offscreen.frames = null;
requested.animode = null;
stopanimation();
}
// Assemble inputs for the rendering worker.
var args = {
expression: drawn.expr,
extent: drawn.extent,
width: drawn.width,
height: drawn.height,
bitmap: modebitmap(drawn.mode),
modemix: modemix(drawn.mode),
frames: computation_frames,
imagedata: mainctx.createImageData(
Math.ceil(blit_blocksize * Math.sqrt(drawn.width / drawn.height)),
Math.ceil(blit_blocksize * Math.sqrt(drawn.height / drawn.width))),
state: drawn.state,
info: drawn.info
};
// Kill any ongoing rendering and then start a new rendering loop.
if (current_worker) { current_worker.terminate(); }
// Don't do any more work if already finished.
if (drawn.finished) {
ebi('editcontrol').className = '';
return;
}
var last_checkpoint_time = (new Date).getTime();
if (worker_script_url && window.Worker &&
(null !== (current_worker = spawn_worker()))) {
// Use web worker if available.
current_worker.onmessage = (function(event) { render(event.data); });
current_worker.postMessage(args);
} else {
// Otherwise just use a timer task.
current_worker = draw_conformal_map(requested.fn, args, 0, render);
}
// Allocate a tempeanvas for stretch blitting.
var tempcanvas = document.createElement('canvas');
// The callback for the rendering engine. Executes the actual
// drawing operations on the underlying canvases.
function render(msg) {
drawn.elapsed = (new Date).getTime() - drawn.starttime;
if (msg.action == 'blit') {
var imgdata = msg.imagedata,
ctxes = framectxes(msg.frame), j, pixels;
if (msg.scalex == 1 && msg.scaley == 1) {
for (j = 0; j < ctxes.length; j++) {
ctxes[j].putImageData(imgdata, msg.x, msg.y);
}
pixels = imgdata.width * imgdata.height;
} else {
tempcanvas.width = imgdata.width;
tempcanvas.height = imgdata.height;
tempcanvas.getContext('2d').putImageData(imgdata, 0, 0);
for (j = 0; j < ctxes.length; j++) {
ctxes[j].save();
ctxes[j].scale(msg.scalex, msg.scaley);
ctxes[j].imageSmoothingEnabled = false;
ctxes[j].drawImage(tempcanvas, msg.x, msg.y);
ctxes[j].restore();
}
pixels = imgdata.width * msg.scalex * imgdata.height * msg.scaley;
}
if (offscreen.frames) {
var frame = offscreen.frames[msg.frame];
if (!frame.filled && frame.drawn_pixels < canvasarea) {
frame.drawn_pixels += pixels;
if (Math.round(frame.drawn_pixels) >= canvasarea) {
frame.filled = true;
}
}
frame.last_opcount = msg.state.opcount;
}
drawn.state = msg.state;
} else if (msg.action == 'copy') {
if (offscreen.frames[msg.frame].same_as === null) {
offscreen.frames[msg.frame].canvas = null;
offscreen.frames[msg.frame].ctx = null;
offscreen.frames[msg.frame].same_as = msg.sourceframe;
offscreen.frames[msg.frame].filled = true;
if (offscreen.frameshown == msg.frame &&
offscreen.frames[msg.sourceframe].canvas !== null) {
mainctx.drawImage(offscreen.frames[msg.sourceframe].canvas, 0, 0);
}
}
} else if (msg.action == 'fill') {
var rgb = msg.rgb.map(Math.floor);
if (drawn.info && msg.frame < drawn.info.length) {
drawn.info[msg.frame].constant_rgb = rgb;
}
var ctxes = framectxes(msg.frame);
if (offscreen.frames) {
offscreen.frames[msg.frame].canvas = null;
offscreen.frames[msg.frame].ctx = null;
offscreen.frames[msg.frame].filled = true;
offscreen.frames[msg.frame].drawn_pixels = 0;
offscreen.frames[msg.frame].last_opcount = msg.state.opcount;
offscreen.frames[msg.frame].constant_rgb = rgb;
}
if (offscreen.frameshown == msg.frame) {
mainctx.save();
mainctx.fillStyle = 'rgb(' + rgb.join(',') + ')';
mainctx.fillRect(0, 0, maincanvas.width, maincanvas.height);
mainctx.restore();
}
drawn.state = msg.state;