forked from aantthony/graph.tk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·822 lines (696 loc) · 28 KB
/
main.js
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
var usr=(function(){
var usr={"eval":function(_____d){
return eval(_____d);
},"clear":function(){return app.ui.console.clear();}};
return usr;
})();
var graphs=[];
function track(event) {
//Google Analytics. If anyone wants access to the
//data, I can give your google account permssion.
if (window.pageTracker) {
setTimeout(function () {
pageTracker._trackEvent("Graph", event)
}, 20)
}
};
randfuncs = "y=x^2@y^2=1-x^2@y<2e^{-x}@\\theta=r@2x+3@\\frac{d}{dx}\\left(\\frac{1}{x}\\right)@r<\\sin\\left(4\\theta\\right)@\\int x.dx@\\frac{d}{dx}\\left(sin\\left(x\\right)\\right)@\\lambda=3@e^{-\\lambda\\cdot x}@\\left|x^2-4\\right|+2@\\frac1x@x^{-2}@x!@\\ln x@\\sum_{n=1}^{\\infinity}\\frac{x^n}{n}@\\sin x@\\tan\\left(x\\right)@\\left(x-2\\right)^2@\\Gamma\\left(x\\right)@\\sqrt x".split("@");
var randfunc_index = 0;
//Not actually random.
function randfunc() {
return randfuncs[(randfunc_index++) % randfuncs.length];
}
function compile(n){
//eq is the equation (CAS)
var eq;
if(typeof n=="string"){
//n is latex. Make it not.
n=clean(n);
}
eq=p(n);
//dependence is an array of variables that eq should be updated on changes.
var dependence=eq.dependence?eq.dependence():[];
eq=eq.simplify();
//Functions of x
var funcs=[];
//Functions of y
var yfuncs=[];
//Functions of theta
var tfuncs=[];
//Functions of r
var rfuncs=[];
//Defenitions of functions defined by the graph. i.e. "f(x)=x" will defined funcdefs.f
var funcdefs={};
//Variables defined. Similar to above funcdefs.
var vars={};
//Variables changed in the process of this compile()
var changed=[];
if(eq===undefined){
}else if(eq.type==eqtype.equality || eq.type==eqtype.lessthan || eq.type==eqtype.greaterthan){
if(eq.type==eqtype.lessthan || eq.type==eqtype.greaterthan){
eq[1]=[eq[1]].setType(eq.type);
}
if(typeof eq[0]=="string"){
if(eq[0]=="y"){
//y=f(x)
//TODO: check the f(y) does not depend on y.
funcs.push(eq[1]);
}else if(eq[0]=="x"){
//x=f(y)
//TODO: check the f(y) does not depend on x.
yfuncs.push(eq[1]);
}else if(dirty(eq[0])=="theta"){
rfuncs.push(eq[1]);
}else if(dirty(eq[0])=="r"){
tfuncs.push(eq[1]);
}else if(eq[0]!=""){
//a *variable* is defined by this graph. Add it to vars.
var varname=dirty(eq[0]);
vars[varname]=eq[1].eval();
if(isNaN(vars[varname])){
throw(MessageStrings.nonconstantconstant);
}
changed.push(varname);
}
}else if(eq[0].length==2 && eq[0].type==eqtype.fn && typeof eq[0][0]=="string" && typeof eq[0][1]=="string"){
//name(x)=...
//Add it to funcdefs.
var mm=eq[1].dreplace(eq[0][1],"x").simplify();
var jsc=mm.getString(0,true);
funcdefs[eq[0][0]]=new Function("x","return "+jsc);
//funcdefs[eq[0][0]]=eval("("+"function(x){return "+jsc+";})");
funcdefs[eq[0][0]].math=mm;
//Trigger changes soon.
changed.push(eq[0][0]);
}else if(eq[0].search("y")){
//If LHS has a y
try{
//Rearrange so that LHS=y, then push RHS to funcs.
funcs.push(eq[0].dreplace(/^y$/g,"x").simplify().inverses().simplify().dreplace(/x/g,eq[1].simplify()));
}catch(ex){
//Could not rearrange.
//TODO: try wolfram alpha API maybe.
//TODO: otherwise, use a discrete/trial-and-error method.
throw("CAS: "+ex);
}
}else{
//No y on lhs.
//TODO: search for an x on the LHS, and do something similar to above,
// but for yfuncs.
throw("CAS: Failure");
}
}else{
if(eq.simplify){
eq=eq.simplify();
}
funcs.push(eq);
}
var ret={"f":function(){throw("Not a function");}};
var builder="";//Javascript code (body) for the plot() function.
//Create an empty vector which will contain vectors as components.
//TODO: just use an array of vectors. Simpler.
ret.pt=[].setType(eqtype.discretevector);
//Singularites
//Singularites arise from:
// Division by zero
// * Logs
// ->± Infinity
// What else?
var first=true;
if(funcs.length){
for(var i=0;i<funcs.length;i++){
var fn=funcs[i].simplify();
//Get javascript expression.
if(fn.type==eqtype.lessthan || fn.type==eqtype.greaterthan){
var jsc=fn[0].getString(0,true);
}else{
var jsc=fn.getString(0,true);
}
if(first){
ret.f=new Function("x","return "+jsc);
first=false;
}
var singularities=[];
try{
var singularities=fn.singularities();
//We don't care about singularites way out there:
singularities.remove(Infinity);
singularities.remove(-Infinity);
}catch(ex){
if(__debug(1,0)){
//when debugging we want to see the error
throw(ex);
}
}
//begin the path. Colour (fill and stroke) is already done for us in ui.js
builder+="ctx.beginPath();var x=boundleft;ctx.move(x,"+jsc+");";
if(0 && singularities.length){
//Better plot logic.
//For example, this would allow us to plot y=sqrt(1-x^2) from [-1,1] exactly with no abrupt endings.
}else{
//The loop is within the function because calling a function is too slow in javascript (last time I checked)
//Plot each point: (jsc is the expression generated by CAS)
builder+="for(var x=boundleft;x<boundright;x+=(boundright-boundleft)/width){"+"ctx.line(x,"+jsc+");}ctx.stroke();";
if(fn.type==eqtype.greaterthan){
builder+="ctx.line(boundright+2,boundtop+2);ctx.line(boundleft-2,boundtop+2);";
builder+="ctx.globalAlpha=0.2;ctx.fill();ctx.globalAlpha=1.0;";
}else if(fn.type==eqtype.lessthan){
builder+="ctx.line(boundright+2,boundbottom-2);ctx.line(boundleft-2,boundbottom-2);";
builder+="ctx.globalAlpha=0.2;ctx.fill();ctx.globalAlpha=1.0;";
}
builder+="ctx.stroke();";
}
try{
//Find stationary points:
var rts=fn.differentiate().simplify().roots();
for(var r=0;r<rts.length;r++){
var array=[0,0].setType(eqtype.discretevector);
//console.log(fn.getString());
array[0]=rts[r];
array[1]=fn.dreplace("x",array[0]).simplify();
ret.pt.push(array);
}
}catch(ex){
if(__debug(0 && 1,0)){
throw(ex);
}
}
try{
var array=[0,0].setType(eqtype.discretevector);
//console.log(fn.getString());
//Find y intercept(s). I.e. f(0), or just work for multi-valued functions.
array_math=fn.dreplace(/^x$/,0);
array[1]=array_math.simplify();
//.getString(0,1,1);
ret.pt.push(array);
try{
//Find x-intercepts.
var rts=fn.roots();
for(var rid=0;rid<rts.length;rid++){
var array=[0,0].setType(eqtype.discretevector);
array[0]=rts[rid].simplify();;
//.getString(0,1,1);
ret.pt.push(array);
}
}catch(ex){
//could not find roots using cas.
//Use wolfram alpha? (needs to be async)
//Use newtons method:
}
//console.log("ok");
//ret.xc.push(funcs[i].inverse().dreplace(/^x$/,0).simplify().simplify());
}catch(ex){
//alert(ex);
}
}
}
if(yfuncs.length){
for(var i=0;i<yfuncs.length;i++){
var fn=yfuncs[i].simplify();
//Get javascript expression.
if(fn.type==eqtype.lessthan || fn.type==eqtype.greaterthan){
var jsc=fn[0].getString(0,true);
}else{
var jsc=fn.getString(0,true);
}
var singularities=[];
try{
var singularities=fn.singularities();
//We don't care about singularites way out there:
singularities.remove(Infinity);
singularities.remove(-Infinity);
}catch(ex){
if(__debug(1,0)){
//when debugging we want to see the error
throw(ex);
}
}
//begin the path. Colour (fill and stroke) is already done for us in ui.js
builder+="ctx.beginPath();var y=boundbottom;ctx.move("+jsc+",y);";
if(0 && singularities.length){
//Better plot logic.
//For example, this would allow us to plot y=sqrt(1-x^2) from [-1,1] exactly with no abrupt endings.
}else{
//The loop is within the function because calling a function is too slow in javascript (last time I checked)
//Plot each point: (jsc is the expression generated by CAS)
builder+="for(var y=boundbottom;y<boundtop;y+=(boundtop-boundbottom)/height){"+"ctx.line("+jsc+",y);}";
if(fn.type==eqtype.greaterthan){
builder+="ctx.line(boundright+2,boundtop+2);ctx.line(boundright+2,boundbottom-2);";
builder+="ctx.globalAlpha=0.2;ctx.fill();ctx.globalAlpha=1.0;";
}else if(fn.type==eqtype.lessthan){
builder+="ctx.line(boundleft-2,boundtop+2);ctx.line(boundleft-2,boundbottom-2);";
builder+="ctx.globalAlpha=0.2;ctx.fill();ctx.globalAlpha=1.0;";
}
builder+="ctx.stroke();";
}
try{
//Find stationary points:
var rts=fn.dreplace("y","x").differentiate().simplify().roots();
for(var r=0;r<rts.length;r++){
var array=[0,0].setType(eqtype.discretevector);
//console.log(fn.getString());
array[0]=rts[r];
array[1]=fn.dreplace("x",array[0]).simplify();
ret.pt.push(array);
}
}catch(ex){
if(__debug(0 && 1,0)){
throw(ex);
}
}
try{
var array=[0,0].setType(eqtype.discretevector);
//console.log(fn.getString());
//Find y intercept(s). I.e. f(0), or just work for multi-valued functions.
array_math=fn.dreplace(/^y$/,0);
array[0]=array_math.simplify();
//.getString(0,1,1);
ret.pt.push(array);
try{
//Find x-intercepts.
var rts=fn.roots();
for(var rid=0;rid<rts.length;rid++){
var array=[0,0].setType(eqtype.discretevector);
array[0]=rts[rid].simplify();;
//.getString(0,1,1);
ret.pt.push(array);
}
}catch(ex){
//could not find roots using cas.
//Use wolfram alpha? (needs to be async)
//Use newtons method:
}
//console.log("ok");
//ret.xc.push(funcs[i].inverse().dreplace(/^x$/,0).simplify().simplify());
}catch(ex){
//alert(ex);
}
}
}
if(rfuncs.length){
for(var i=0;i<rfuncs.length;i++){
var fn=rfuncs[i].simplify();
//Get javascript expression.
if(fn.type==eqtype.lessthan || fn.type==eqtype.greaterthan){
var jsc=fn[0].getString(0,true);
}else{
var jsc=fn.getString(0,true);
}
jsc=jsc.replace(/app\.variables\[\"(x|y|z|r|theta)\"\]/g,"$1");
if(first){
//ret.f=eval("("+"function(x){return "+jsc+";})");
//The first value of a multi-valued function shall be its
//primary value.
ret.f=new Function("r","return "+jsc);
first=false;
}
var singularities=[];
try{
var singularities=fn.singularities();
//We don't care about singularites way out there:
singularities.remove(Infinity);
singularities.remove(-Infinity);
}catch(ex){
if(__debug(1,0)){
//when debugging we want to see the error
throw(ex);
}
}
//begin the path. Colour (fill and stroke) is already done for us in ui.js
builder+="ctx.beginPath();var r=0;ctx.move(0,0);";
if(0 && singularities.length){
//Better plot logic.
//For example, this would allow us to plot y=sqrt(1-x^2) from [-1,1] exactly with no abrupt endings.
}else{
//The loop is within the function because calling a function is too slow in javascript (last time I checked)
//Plot each point: (jsc is the expression generated by CAS)
/*
We want to plot all points (x,y) ∈ ([boundleft,boundright],[boundbottom,boundtop])
r*(sin(x),cos(y)) ∈ (r*cos[boundleft,boundright],r*sin[boundbottom,boundtop])
x=r.cos(f(r)), y=r.sin(f(r))
f(r)=acos(x/r)
y=r.sin(acos(x/r))
r=atan(y/x)
y=r.sin(f(atan(y/x)))
asin(y/r)=f(atan(y/x))
*/
builder+="var rinc=(rmax-rmin)/width;for(var r=rmin;r<rmax;r+=rinc){var thtmp=("+jsc+");ctx.line(r*cos(thtmp),r*sin(thtmp));}";
if(fn.type==eqtype.greaterthan){
builder+="ctx.line(boundright+2,boundtop+2);ctx.line(boundright+2,boundbottom-2);";
builder+="ctx.globalAlpha=0.2;ctx.fill();ctx.globalAlpha=1.0;";
}else if(fn.type==eqtype.lessthan){
builder+="var thtmp=("+jsc+")%(2*pi);if(thtmp>7*pi/4){ctx.line(boundright+2,boundbottom-2);}if(thtmp>5*pi/4){ctx.line(boundleft-2,boundbottom-2);}if(thtmp>pi/4){ctx.line(boundleft-2,boundtop+2);ctx.line(boundright+2,boundtop+2);}ctx.line(boundright+2,0);ctx.line(0,0);";
builder+="ctx.globalAlpha=0.2;ctx.fill();ctx.globalAlpha=1.0;";
}
builder+="ctx.stroke();";
}
}
}
if(tfuncs.length){
for(var i=0;i<tfuncs.length;i++){
var fn=tfuncs[i].simplify();
//Get javascript expression.
if(fn.type==eqtype.lessthan || fn.type==eqtype.greaterthan){
var jsc=fn[0].getString(0,true);
}else{
var jsc=fn.getString(0,true);
}
//A little messy:
jsc=jsc.replace(/app\.variables\[\"(x|y|z|r|theta)\"\]/g,"$1");
if(first){
//The first value of a multi-valued function shall be its
//primary value.
ret.f=new Function("theta","return "+jsc);
first=false;
}
var singularities=[];
try{
var singularities=fn.singularities();
//We don't care about singularites way out there:
singularities.remove(Infinity);
singularities.remove(-Infinity);
}catch(ex){
if(__debug(1,0)){
//when debugging we want to see the error
throw(ex);
}
}
//begin the path. Colour (fill and stroke) is already done for us in ui.js
builder+="ctx.beginPath();var theta=0;ctx.move("+jsc+"*cos(theta),"+jsc+"*sin(theta));";
if(0 && singularities.length){
//Better plot logic.
//For example, this would allow us to plot y=sqrt(1-x^2) from [-1,1] exactly with no abrupt endings.
}else{
//The loop is within the function because calling a function is too slow in javascript (last time I checked)
//Plot each point: (jsc is the expression generated by CAS)
/*
tinc: reciprcal of pixels along max *circle* (ellipse to complicated) around screen area?
*/
builder+="var tinc=1/max(width,height);for(var theta=0;theta<(2*pi);theta+=tinc){var rtmp=("+jsc+");ctx.line(rtmp*cos(theta),rtmp*sin(theta));}ctx.line("+jsc+",0);";
if(fn.type==eqtype.greaterthan){
builder+="ctx.line(boundright+2,0);ctx.line(boundright+2,boundbottom-2);ctx.line(boundleft-2,boundbottom-2);ctx.line(boundleft-2,boundtop+2);ctx.line(boundright+2,boundtop+2);ctx.line(boundright+2,0);";
builder+="ctx.globalAlpha=0.2;ctx.fill();ctx.globalAlpha=1.0;";
}else if(fn.type==eqtype.lessthan){
builder+="var theta=0;ctx.line("+jsc+"*cos(theta),"+jsc+"*sin(theta));";
builder+="ctx.globalAlpha=0.2;ctx.fill();ctx.globalAlpha=1.0;";
}
builder+="ctx.stroke()";
}
}
}
//Add our vars and funcdefs to the global CAS variables (app.variables)
//TODO: should not be app, but cas.variables. (javascript-cas will support variables natively someday)
if(window && window.app && window.app.variables){
for(i in vars){
if(vars.hasOwnProperty(i)){
if(i=="e" || i=="pi"){
throw(MessageStrings.protected);
return;
}
window.app.variables[i]=vars[i];
}
}
for(i in funcdefs){
if(funcdefs.hasOwnProperty(i)){
if(functions.indexOf(i)!=-1){
throw(MessageStrings.protected);
return;
}
window.app.variables[i]=funcdefs[i];
}
}
}
//Give out this stuff. It probably won't be used anyway.
ret.math=funcs;
ret.funcdefs=funcdefs;
ret.vars=vars;
//This will be searched on window.app.refresh()
ret.dependence=dependence;
//This will be called from ui.js. Currently, all graphs share a context. I think this is sufficent, but it may not be.
ret.plot=new Function("ctx",builder);
//Trigger all changes
if(window && window.app && window.app.refresh){
window.app.refresh(changed);
}
return ret;
}
//function management
var Graph=function(n,disabled){
var latex=n;
var auto=0;
if(n){
auto=true;
}else{
//Sample function
latex=randfunc();
}
var t={};
var c=compile(latex);
//Copy properties (we will extend the object)
//TODO: why don't we just use t=c? lol
for(i in c){
if(c.hasOwnProperty(i)){
t[i]=c[i];
}
}
//For debugging purposes. I think.
t.equation=latex;
//Graph id: random
t.gid=random_hash();
t.disabled=disabled;
t.color=app.ui.colors.free.pop();
if(t.color==undefined){
//We ran out of colours!
t.color="#000";
}
if(auto){t.auto=true;}
//Create html <li> node.
t.node=app.ui.add(t);
return t;
};
//CAS variables.
//TODO: use cas.variables in javascript-cas.
app.variables={};
//Updated automatically by shell script.
app.version="GIT_VERSION";
app.add=function(n,disabled){
var graph = new Graph(n,disabled);
graph.disabled = disabled;
graphs.push(graph);
track("New");
app.ui.refresh();
};
app.png=function(){
app.ui.png();
};
app.console=function(){
app.ui.console.toggle();
};
app.get_state=function() {
var res = {};
res.graphs = {};
$.each(graphs, function() {
res.graphs[this.equation] = !this.disabled ? 1 : 0;
});
res.scale = $.map(app.ui.get_scale(), function(i) { return parseInt(i); });
res.camera = $.map(app.ui.get_camera(), function(i) { return parseInt(i); });
res.legend = app.ui.legend() ? 1 : 0;
res.block = app.ui.block() ? 1 : 0;
return res;
}
//TODO: a name like mathDidChange would be much more appropriate
app.refresh=function(changes){
var redraw=false;
for(var i=0;i<graphs.length;i++){
for(var d=0;d<graphs[i].dependence.length;d++){
if(changes.indexOf(graphs[i].dependence[d])!=-1){
//app.ui.console.log("auto-update");
var c=compile(graphs[i].equation);
//Copy properties from c to graphs[i]
for(_k in c){
if(c.hasOwnProperty(_k)){
graphs[i][_k]=c[_k];
}
}
redraw=true;
}
}
}
if(redraw){
//This is an actual refresh, see above TODO.
app.ui.refresh();
}
};
app.remove=function(n){
if(typeof n !="string"){
//Remove a function by html node id.
var id = n.id.substring(3);
app.ui.remove(n);
n=id;
}
//Now n is a gid.
for(var i=0;i<graphs.length;i++){
if(graphs[i].gid==n){
//Give colour back to system.
app.ui.colors.free.push(graphs[i].color);
graphs.splice(i,1);
break;
}
}
app.ui.refresh();
};
app.empty=function(n){
//Remove all graphs.
for(var i=0;i<graphs.length;i++){
app.ui.colors.free.push(graphs[i].color);
graphs.splice(i,1);
}
var ul=document.getElementById("graphs");
while(ul.firstChild){
ul.removeChild(ul.firstChild);
}
app.ui.refresh();
};
function message(m){
//For postMessage API. ANY WEBSITE CAN CALL THIS!
var s=m.data.split(/[:,]/);
switch(s[0]){
case "add":
app.add(s[1]);
break;
case "block":
app.ui.block(1);
break;
case "unblock":
app.ui.block(0);
break;
case "translate":
app.ui.translate(s[1],s[2],s[3]);
break;
case "set":
app.variables[s[1]]=Number(s[2]);
app.ui.redraw();
break;
case "locale":
app.locale = s[1];
app.locale_updated();
case "empty":
app.empty();
break;
case "screenshot":
m.source.postMessage(app.ui.png(false), m.origin);
break;
case "bounds":
app.ui.bounds(s[1],s[2],s[3],s[4]);
break;
case "scale":
app.ui.scale(s[1],s[2],s[3]);
break;
case "center":
app.ui.center(s[1],s[2],s[3]);
break;
}
}
if(!window.addEventListener){
//TODO: CHECK THIS. I.E. doesn't work anyway atm. (stack overflow)
window.addEventListener=function(eventname,fn,mode){
return window.attachEvent("on"+eventname, fn,mode);
};
}
if(window.addEventListener){
window.addEventListener("message", message, false);
}else{
window.attachEvent("onmessage", message);
}
function hashDidChange(){
app.empty();
if((location.hash || location.search).match(/^#json=/)) {
data = JSON.parse(decodeURIComponent((location.hash || location.search).substring(6)));
for(var idx in data.graphs) {
if(data.graphs.hasOwnProperty(idx)){
app.add(idx, !data.graphs[idx]);
}
}
if(data.locale) {
app.locale = data.locale;
}
if(data.scale) {
app.ui.set_scale.apply(this, data.scale);
}
if(data.camera) {
app.ui.set_camera.apply(this, data.camera);
}
if(data.block != undefined) {
app.ui.block(!!data.block);
}
if(data.console != undefined) {
app.ui.button('>_', data.console);
}
if(data.reload != undefined) {
app.ui.button('reload', data.reload);
}
if(data.legend != undefined) {
app.ui.legend(!!data.legend);
}
} else {
app.add(decodeURIComponent((location.hash || location.search).substring(1)));
}
}
window.addEventListener("hashchange", hashDidChange, false);
app.translate=function(x,y,z){
app.ui.translate(Number(x),Number(y),Number(z));
};
app.scale=function(x,y,z){
app.ui.scale(Number(x),Number(y),Number(z));
};
app.locale_updated = function(locale) {
if(locale) { app.locale = locale; }
if(!app.locale) { return; };
var script = document.createElement('script');
script.src = 'langs/' + app.locale.toLowerCase() + '.js';
document.getElementsByTagName('head')[0].appendChild(script);
};
app.init=function (){
var fullscreen=!window.parent.length;
if(window && window.parent != window) {
//this causes an error when in an iframe: SYNTAX_ERR: DOM Exception 12: An invalid or illegal string was specified.
//window.parent.postMessage('locale check');
}
app.locale = $.map(['language', 'browserLanguage', 'systemLanguage', 'userLanguage'], function(key) { return window.navigator && window.navigator[key] })[0];
app.view_configured = (location.hash || location.search).match(/^#json=/);
app.ui.init(fullscreen);
app.locale_updated();
if((location.hash || location.search).length>1){
hashDidChange();
}else if(fullscreen){
var preferences;
if(window.localStorage){
preferences=localStorage.getItem("graph.tk");
if(preferences){
try{
preferences=JSON.parse(preferences);
}catch(ex){}
}
}
app.add("\\frac{d}{dx}\\left(sin\\left(x\\right)+log\\left(x+1\\right)\\right)");
}
var div=document.createElement("div");
var logo=new Image();
logo.src="favicon.ico";
logo.style.float="left";
div.appendChild(logo);
var elem = document.createElement('span');
elem.innerHTML = "graph.tk - <span id='version_text'>"+app.ui.messages.version+"</span> "+app.version;
div.appendChild(elem);
//div.style.height="4em";
var span=document.createElement("span");
span.className="mathquill-rendered-math mathquill-editable";
span.innerHTML='<br /><span id="example_text">'+app.ui.messages.example+'</span>: <span id="type_text">'+app.ui.messages.type+'</span> <span class="textarea"><textarea></textarea></span><span class="fraction"><span class="numerator"><var>d</var></span><span class="denominator"><var>d</var><var>x</var></span><span style="width:0"> </span></span><span><span class="paren" style="font-size: 1.89542em; ">(</span><span class=""><span class="fraction"><span class="numerator"><span>1</span></span><span class="denominator"><var>x</var></span><span style="width:0"> </span></span></span><span class="paren" style="font-size: 1.89542em; ">)</span></span>';
div.appendChild(span);
app.ui.console.log(div,true);
//app.ui.console.log("Example: Type d/dx (1/x)",true);
//app.add("x^2");
};
if(!/noboot$/.test(location.search)){
app.init();
}