-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
808 lines (741 loc) · 30.7 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
'use strict';
import {gridPointsGenerator} from "./gridPointsGenerator.js";
import { degreeToRadians, radianToDegrees } from "./radianDegreeConversion.js";
import { parseFloat, parseInteger, parseProjection } from "./parsers.js";
import { autoFindXYBounds } from "./autoFindXYBounds.js";
import { pixelDimensionsFromXYAndCount } from "./pixelDimensionsFromXYAndCount.js";
import { allCountryCodes } from "./allCountryCodes.js";
import myThrottle from "./myThrottle.js";
// Config
const resolutions = [25*50, 50*50, 50*100, 100*100, 100*200];
// Iframe
const IFRAME = document.getElementById('myframe');
const IFRAME_WINDOW = IFRAME.contentWindow;
// Canvas element
const canvasElement = document.getElementById("myCanvas");
const ctx = canvasElement.getContext('2d');
canvasElement.style.height = '450px';
canvasElement.style.width = '900px';
// Initilize canvas
// ctx.font = '18px serif';
// ctx.fillText('This will take a while', 10, 40);
// ctx.fillText('your browser might ask you to cancel the page.', 10, 60);
// Geting parentElement and finding its dimensions. They should stay static
const parentElement = document.getElementById('parent');
const parentHeight = parentElement.clientHeight;
const parentWidth = parentElement.clientWidth;
const parentWidthHeightRatio = parentWidth/parentHeight;
// GLOBAL DATA
// Will hold the >mapvalue data for each pixel. This is the data that has been run through Nearest Neighbor if i decide to do that.
let PIXELGRID = null;
// Projection inputs
const xProjectionInput = document.getElementById('projection-inputX');
const yProjectionInput = document.getElementById('projection-inputY');
// BUTTONS
// Project button
const projectButton = document.getElementById('project-button');
// Getting canvas sizing inputs
// Actual dimensions of canvas
const pixelHeightInput = document.getElementById('height');
const pixelWidthInput = document.getElementById('width');
// Output bounds input
const minXInput = document.getElementById('minX');
const maxXInput = document.getElementById('maxX');
const minYInput = document.getElementById('minY');
const maxYInput = document.getElementById('maxY');
// Input Bounds input
const minThetaInput = document.getElementById('minTheta');
const maxThetaInput = document.getElementById('maxTheta');
const minPhiInput = document.getElementById('minPhi');
const maxPhiInput = document.getElementById('maxPhi');
// Unit radio inputs
const radians = document.getElementById('radians');
const degrees = document.getElementById('degrees');
// Resolution slider
const resolutionSlider = document.getElementById('resolution');
// MATH SETUP
// ADDING LN
math.import({ln:Math.log});
// Trig functions
const OGTRIG = {
sin: math.sin,
cos: math.cos,
tan: math.tan,
sec: math.sec,
csc: math.csc
};
const DEGTRIG = {
sin: function(x){return OGTRIG.sin(degreeToRadians(x))},
cos: function(x){return OGTRIG.cos(degreeToRadians(x))},
tan: function(x){return OGTRIG.tan(degreeToRadians(x))},
sec: function(x){return OGTRIG.sec(degreeToRadians(x))},
csc: function(x){return OGTRIG.csc(degreeToRadians(x))},
};
const OGINVERSETRIG = {
asin: math.asin,
acos: math.acos,
atan: math.atan,
asec: math.asec,
acsc: math.acsc
}
const DEGINVERSETRIG = {
asin: function(x){return radianToDegrees(OGINVERSETRIG.asin(x))},
acos: function(x){return radianToDegrees(OGINVERSETRIG.acos(x))},
atan: function(x){return radianToDegrees(OGINVERSETRIG.atan(x))},
asec: function(x){return radianToDegrees(OGINVERSETRIG.asec(x))},
acsc: function(x){return radianToDegrees(OGINVERSETRIG.acsc(x))},
};
// Current mode
let trigMode = "radians";
// Initialize color inputs
let colorInputs = [];
let colorInputsParent = document.getElementById('color-inputs-parent');
// First deal with seabase
let [seabaseInput, seabaseLabel] = createColorInputAndLabel('seabase', '#0000ff');
seabaseInput.addEventListener('input', myThrottle(renderMap,1000));
colorInputs.push(seabaseInput);
let seabaseLabelParent = document.createElement('div');
seabaseLabelParent.className = 'color-input-parent';
seabaseLabelParent.append(seabaseLabel);
colorInputsParent.appendChild(seabaseLabelParent);
let seabaseInputParent = document.createElement('div');
seabaseInputParent.className = 'color-input-parent';
seabaseInputParent.append(seabaseInput);
colorInputsParent.appendChild(seabaseInputParent);
for(let countryCode of allCountryCodes){
let [countryInput, countryLabel] = createColorInputAndLabel(countryCode, '#00ff00');
countryInput.addEventListener('input', myThrottle(renderMap,1000));
colorInputs.push(countryInput);
let countryLabelParent = document.createElement('div');
countryLabelParent.className = 'color-Label-parent';
countryLabelParent.append( countryLabel);
colorInputsParent.appendChild(countryLabelParent);
let countryInputParent = document.createElement('div');
countryInputParent.className = 'color-input-parent';
countryInputParent.append( countryInput);
colorInputsParent.appendChild(countryInputParent);
}
function createColorInputAndLabel(name, color){
let input = document.createElement('input');
input.setAttribute('type','color');
input.setAttribute('value',color);
input.setAttribute('name', name);
input.setAttribute('id', name);
let label = document.createElement('label');
label.innerText = name;
label.setAttribute('for',name);
return [input,label];
}
// UI functions
// MB remove
function minMaxCheckerBuilder(minElement, maxElement){
minElement.addEventListener('change',
function(e){
}
);
}
function makeProjectionFromParts(xFunction, yFunction){
return(
function(theta, phi){
if(trigMode === 'radians'){
theta = theta * Math.PI /180;
phi = phi * Math.PI /180;
}
return [xFunction(theta,phi), yFunction(theta,phi)];
}
);
}
// Probably want to move these somewhere else
function checkThetaBounds(theta){
return(-180 <= theta && theta <= 180);
}
function checkPhiBounds(phi){
return(-90 <= phi && phi <= 90);
}
function possiblyAutoSetXYBounds(){
// CHECK if all of the nessisary imputs are proper.
const minThetaValue = parseFloat(minThetaInput.value);
const maxThetaValue = parseFloat(maxThetaInput.value);
const minPhiValue = parseFloat(minPhiInput.value);
const maxPhiValue = parseFloat(maxPhiInput.value);
if(minThetaValue !== null && maxThetaValue !== null && minPhiValue !== null && maxPhiValue !== null){
if(minThetaValue < maxThetaValue && minPhiValue < maxPhiValue){
if(checkThetaBounds(minThetaValue) && checkThetaBounds(maxThetaValue) && checkPhiBounds(minPhiValue) && checkPhiBounds(maxPhiValue)){
// check if Projections are there and valid
const xProjection = parseProjection(xProjectionInput.value);
const yProjection = parseProjection(yProjectionInput.value);
if(xProjection !== null && yProjection !== null){
// Autofind and set
const projection = makeProjectionFromParts(xProjection, yProjection);
const {minX,maxX,minY,maxY} = autoFindXYBounds(projection, minThetaValue, maxThetaValue, minPhiValue, maxPhiValue);
minXInput.value = minX;
maxXInput.value = maxX;
minYInput.value = minY;
maxYInput.value = maxY;
maxYInput.dispatchEvent(new Event('change')); // This will trigger the pixel updates
}
}
}
}
}
minThetaInput.addEventListener('change',
function(e){
let floatValue = parseFloat(this.value); //1
let paredFloatValue = parseFloat(maxThetaInput.value); //2
if(floatValue !== null && checkThetaBounds(floatValue)){
// remove error style //1
this.classList.remove('Local-Error'); //1
if(paredFloatValue !== null){
if(paredFloatValue > floatValue){ // 3
// remove other error styles on this and paired //1,2
this.classList.remove('Nonlocal-Error');
maxThetaInput.classList.remove('Nonlocal-Error');
// initiate possible x,y update // common to theta and phi
possiblyAutoSetXYBounds();
}
else{
// add other error styles on this and paired //1,2
this.classList.add('Nonlocal-Error'); //1
maxThetaInput.classList.add('Nonlocal-Error'); //2
}
}
} else {
// add error style //1
this.classList.add('Local-Error');
}
}
);
maxThetaInput.addEventListener('change',
function(e){
let floatValue = parseFloat(this.value); //1
let paredFloatValue = parseFloat(minThetaInput.value); //2
if(floatValue !== null && checkThetaBounds(floatValue)){
// remove error style //1
this.classList.remove('Local-Error'); //1
if(paredFloatValue !== null){
if(paredFloatValue < floatValue){ // 3
// remove other error styles on this and paired //1,2
this.classList.remove('Nonlocal-Error');//1
minThetaInput.classList.remove('Nonlocal-Error'); //2
// initiate possible x,y update // common to theta and phi
possiblyAutoSetXYBounds();
}
else{
// add other error styles on this and paired //1,2
this.classList.add('Nonlocal-Error'); //1
minThetaInput.classList.add('Nonlocal-Error'); //2
}
}
} else {
// add error style //1
this.classList.add('Local-Error');
}
}
);
minPhiInput.addEventListener('change',
function(e){
let floatValue = parseFloat(this.value); //1
let paredFloatValue = parseFloat(maxPhiInput.value); //2
if(floatValue !== null && checkPhiBounds(floatValue)){
// remove error style //1
this.classList.remove('Local-Error'); //1
if(paredFloatValue !== null){
if(paredFloatValue > floatValue){ // 3
// remove other error styles on this and paired //1,2
this.classList.remove('Nonlocal-Error');//1
maxPhiInput.classList.remove('Nonlocal-Error'); //2
// initiate possible x,y update // common to theta and phi
possiblyAutoSetXYBounds();
}
else{
// add other error styles on this and paired //1,2
this.classList.add('Nonlocal-Error'); //1
maxPhiInput.classList.add('Nonlocal-Error'); //2
}
}
} else {
// add error style //1
this.classList.add('Local-Error');
}
}
);
maxPhiInput.addEventListener('change',
function(e){
let floatValue = parseFloat(this.value); //1
let paredFloatValue = parseFloat(minPhiInput.value); //2
if(floatValue !== null && checkPhiBounds(floatValue)){
// remove error style //1
this.classList.remove('Local-Error'); //1
if(paredFloatValue !== null){
if(paredFloatValue < floatValue){ // 3
// remove other error styles on this and paired //1,2
this.classList.remove('Nonlocal-Error');//1
minPhiInput.classList.remove('Nonlocal-Error'); //2
// initiate possible x,y update // common to theta and phi
possiblyAutoSetXYBounds();
}
else{
// add other error styles on this and paired //1,2
this.classList.add('Nonlocal-Error'); //1
minPhiInput.classList.add('Nonlocal-Error'); //2
}
}
} else {
// add error style //1
this.classList.add('Local-Error');
}
}
);
function changeCanvasDimensions(pixelWidth, pixelHeight){
// Resize the canvas.
canvasElement.height = pixelHeight;
canvasElement.width = pixelWidth;
// Resize the canvas css height and width to remain in the parent box while remaining centered and being as
// large as possible.
if(pixelWidth/pixelHeight >= parentWidthHeightRatio){
// left right edges will be flush.
canvasElement.style.width = `${parentWidth}px`;
canvasElement.style.height = `${(parentWidth/pixelWidth) * pixelHeight}px`;
} else{
// top bottom edges will be flush
canvasElement.style.height = `${parentHeight}px`;
canvasElement.style.width = `${(parentHeight/pixelHeight) * pixelWidth}px`;
}
}
function possiblePixelCountUpdate(){
// CHECK if all of the nessisary imputs are proper.
const minXValue = parseFloat(minXInput.value);
const maxXValue = parseFloat(maxXInput.value);
const minYValue = parseFloat(minYInput.value);
const maxYValue = parseFloat(maxYInput.value);
if(minXValue !== null && maxXValue !== null && minYValue !== null && maxYValue !== null){
if(minXValue < maxXValue && minYValue < maxYValue){
// Reactivate Slider
resolutionSlider.classList.add('Active');
// Take value from slider
const sliderPosition = parseInt(resolutionSlider.value);
const resolutionPixelCount = resolutions[sliderPosition];
const deltaX = maxXValue - minXValue;
const deltaY = maxYValue - minYValue;
const [pixelWidth, pixelHeight] = pixelDimensionsFromXYAndCount(resolutionPixelCount, deltaX, deltaY);
// Update inputs
pixelHeightInput.value = pixelHeight;
pixelWidthInput.value = pixelWidth;
// Update Canvas
//changeCanvasDimensions(pixelWidth, pixelHeight);
}
}
}
minXInput.addEventListener('change',
function(e){
const floatValue = parseFloat(this.value); //1
const paredElement = maxXInput; //2
const paredFloatValue = parseFloat(paredElement.value);
if(floatValue !== null){
// remove error style //1
this.classList.remove('Local-Error'); //1
if(paredFloatValue !== null){
if(paredFloatValue > floatValue){ // 3
// remove other error styles on this and paired //1,2
this.classList.remove('Nonlocal-Error'); //1
paredElement.classList.remove('Nonlocal-Error'); //2
// initiate possible pixel count update
possiblePixelCountUpdate();
}
else{
// add other error styles on this and paired //1,2
this.classList.add('Nonlocal-Error');
paredElement.classList.add('Nonlocal-Error');
}
}
} else {
// add error style //1
this.classList.add('Local-Error');
}
}
);
maxXInput.addEventListener('change',
function(e){
const floatValue = parseFloat(this.value); //1
const paredElement = minXInput; //2
const paredFloatValue = parseFloat(paredElement.value);
if(floatValue !== null){
// remove error style //1
this.classList.remove('Local-Error'); //1
if(paredFloatValue !== null){
if(paredFloatValue < floatValue){ // 3
// remove other error styles on this and paired //1,2
this.classList.remove('Nonlocal-Error'); //1
paredElement.classList.remove('Nonlocal-Error'); //2
// initiate possible pixel count update
possiblePixelCountUpdate();
}
else{
// add other error styles on this and paired //1,2
this.classList.add('Nonlocal-Error');
paredElement.classList.add('Nonlocal-Error');
}
}
} else {
// add error style //1
this.classList.add('Local-Error');
}
}
);
minYInput.addEventListener('change',
function(e){
const floatValue = parseFloat(this.value); //1
const paredElement = maxYInput; //2
const paredFloatValue = parseFloat(paredElement.value);
if(floatValue !== null){
// remove error style //1
this.classList.remove('Local-Error'); //1
if(paredFloatValue !== null){
if(paredFloatValue > floatValue){ // 3
// remove other error styles on this and paired //1,2
this.classList.remove('Nonlocal-Error'); //1
paredElement.classList.remove('Nonlocal-Error'); //2
// initiate possible pixel count update
possiblePixelCountUpdate();
}
else{
// add other error styles on this and paired //1,2
this.classList.add('Nonlocal-Error');
paredElement.classList.add('Nonlocal-Error');
}
}
} else {
// add error style //1
this.classList.add('Local-Error');
}
}
);
maxYInput.addEventListener('change',
function(e){
const floatValue = parseFloat(this.value); //1
const paredElement = minYInput; //2
const paredFloatValue = parseFloat(paredElement.value);
if(floatValue !== null){
// remove error style //1
this.classList.remove('Local-Error'); //1
if(paredFloatValue !== null){
if(paredFloatValue < floatValue){ // 3
// remove other error styles on this and paired //1,2
this.classList.remove('Nonlocal-Error'); //1
paredElement.classList.remove('Nonlocal-Error'); //2
// initiate possible pixel count update
possiblePixelCountUpdate();
}
else{
// add other error styles on this and paired //1,2
this.classList.add('Nonlocal-Error');
paredElement.classList.add('Nonlocal-Error');
}
}
} else {
// add error style //1
this.classList.add('Local-Error');
}
}
);
function checkIfPixelDimsMatchXYBounds(pixelHeight, pixelWidth){
const xMin = parseFloat(minXInput.value);
const xMax = parseFloat(maxXInput.value);
const yMin = parseFloat(minYInput.value);
const yMax = parseFloat(maxYInput.value);
// Check if all bounds are present
if(xMin !== null && xMax !== null && yMin !== null && yMax !== null){
// Check that bounds dont conflict
if(xMin < xMax && yMin < yMax){
// Check if no more than a little bit off
const deltaX = xMax - xMin;
const deltaY = yMax - yMin;
const xyRatio = deltaX/deltaY;
const pixelRatio = pixelWidth/pixelHeight;
const ratioDiff = Math.abs(xyRatio - pixelRatio);
const onlyALittleBitOff = Math.abs(ratioDiff) < 0.01;
// Check if there is no better at a similar res. Concretly this checks if there is a better match by just changing pixelHeight or pixelWidth
const withOneLessWidth = Math.abs(((pixelWidth-1)/pixelHeight) - xyRatio);
const withOneMoreWidth = Math.abs(((pixelWidth+1)/pixelHeight) - xyRatio);
const withOneLessHeight = Math.abs((pixelWidth/(pixelHeight-1)) - xyRatio);
const withOneMoreHeight = Math.abs((pixelWidth/(pixelHeight+1)) - xyRatio);
const thereIsBetterAtSimilarRes = withOneLessWidth < ratioDiff || withOneMoreWidth < ratioDiff || withOneLessHeight < ratioDiff || withOneMoreHeight < ratioDiff;
if(onlyALittleBitOff && !thereIsBetterAtSimilarRes){
return true;
}
else{
return false;
}
}
}
// If the x y bounds are invalid return false
return false;
}
pixelHeightInput.addEventListener('change',
function(e){
resolutionSlider.classList.remove('Active');
const intValue = parseInteger(this.value);
const paredElementValue = parseInteger(pixelWidthInput.value);
if(intValue !== null && intValue >= 20 && intValue <= 4000){
this.classList.remove('Local-Error');
if(paredElementValue !== null && paredElementValue >= 20 && paredElementValue <= 4000){
// Check if the pixel dimensions match the X & Y bounds
if(checkIfPixelDimsMatchXYBounds(intValue, paredElementValue)){//n
this.classList.remove('Nonlocal-Error');
pixelWidthInput.classList.remove('Nonlocal-Error');
//changeCanvasDimensions(paredElementValue ,intValue);//n
} else {//n
this.classList.add('Nonlocal-Error');
pixelWidthInput.classList.add('Nonlocal-Error');
}//n
}
} else{
this.classList.add('Local-Error');
}
}
);
pixelWidthInput.addEventListener('change',
function(e){
resolutionSlider.classList.remove('Active');
const intValue = parseInteger(this.value);
const paredElementValue = parseInteger(pixelHeightInput.value);
if(intValue !== null && intValue >= 20 && intValue <= 4000){
this.classList.remove('Local-Error');
if(paredElementValue !== null && paredElementValue >= 20 && paredElementValue <= 4000){
// Check if the pixel dimensions match the X & Y bounds
if(checkIfPixelDimsMatchXYBounds(paredElementValue, intValue)){//n
this.classList.remove('Nonlocal-Error');
pixelHeightInput.classList.remove('Nonlocal-Error');
//changeCanvasDimensions(intValue, paredElementValue);//n
} else {//n
this.classList.add('Nonlocal-Error');
pixelHeightInput.classList.add('Nonlocal-Error');
}//n
}
} else{
this.classList.add('Local-Error');
}
}
);
resolutionSlider.addEventListener('change',
function(e){
this.classList.add('Active');
possiblePixelCountUpdate();
}
);
xProjectionInput.addEventListener('change',
function(e){
const xProj = parseProjection(this.value);
const yProj = parseProjection(yProjectionInput.value);
if(xProj !== null){
this.classList.remove('Local-Error');
if(yProj !== null){
this.classList.remove('Nonlocal-Error');
yProjectionInput.classList.remove('Nonlocal-Error');
// AutosetXY
possiblyAutoSetXYBounds();
} else{
this.classList.add('Nonlocal-Error');
yProjectionInput.classList.add('Nonlocal-Error');
}
} else{
this.classList.add('Local-Error');
}
}
);
yProjectionInput.addEventListener('change',
function(e){
const yProj = parseProjection(this.value);
const xProj = parseProjection(xProjectionInput.value);
if(yProj !== null){
this.classList.remove('Local-Error');
if(xProj !== null){
this.classList.remove('Nonlocal-Error');
xProjectionInput.classList.remove('Nonlocal-Error');
// AutosetXY
possiblyAutoSetXYBounds();
} else{
this.classList.add('Nonlocal-Error');
xProjectionInput.classList.add('Nonlocal-Error');
}
} else{
this.classList.add('Local-Error');
}
}
);
projectButton.addEventListener('click',
function(e){
// Check that all of the inputs exist and are sensible.
const xProjection = parseProjection(xProjectionInput.value);
const yProjection = parseProjection(yProjectionInput.value);
const xMin = parseFloat(minXInput.value);
const xMax = parseFloat(maxXInput.value);
const yMin = parseFloat(minYInput.value);
const yMax = parseFloat(maxYInput.value);
const thetaMin = parseFloat(minThetaInput.value);
const thetaMax = parseFloat(maxThetaInput.value);
const phiMin = parseFloat(minPhiInput.value);
const phiMax = parseFloat(maxPhiInput.value);
const pixelWidth = parseInt(pixelWidthInput.value);
const pixelHeight = parseInt(pixelHeightInput.value);
// Check that no nessisary input is missing
if(xProjection === null || yProjection === null || xMin === null || xMax === null || yMin === null || yMax === null || thetaMin === null || thetaMax === null || phiMin === null || phiMax === null || pixelWidth === null || pixelHeight === null){
// Alert
alert("A nessisary input was missing.");
// TODO Highlight missing input
return;
}
// Check that all of the inputs are correct.
if(xMin >= xMax || yMin >= yMax || thetaMin >= thetaMax || phiMin >= phiMax){
alert("A minimum bound is above a maximum");
return;
}
if(thetaMin < -180 || thetaMin > 180 || phiMin < -90 || phiMin > 90){
alert("A theta/phi coordinate is out of bounds");
return;
}
if(pixelHeight < 20 || pixelHeight > 4000 || pixelWidth < 20 || pixelWidth > 4000){
alert("A pixel value is to big or small. Pixel values have to be in the range [20, 4000]");
return;
}
if(!checkIfPixelDimsMatchXYBounds(pixelHeight, pixelWidth)){
alert("The pixel dimensions and X/Y dimensions dont match up.");
return;
}
changeCanvasDimensions(pixelWidth, pixelHeight);
document.getElementById('loader-warning').classList.remove('display-none');
let projection = makeProjectionFromParts(xProjection, yProjection);
const temp = parseInteger(document.getElementById('testPointFactor').value);
const testPointsFactor = temp ? temp : 2;
// Do the projection and other stuff.
let gridPoints = gridPointsGenerator(phiMin,phiMax,thetaMin,thetaMax,Math.floor(pixelHeight * testPointsFactor),Math.floor(pixelWidth * testPointsFactor));
// Find corespondingXY
let corespondingXY = gridPoints.map(v=>{
let proj = projection(v[1],v[0]);
return [proj[0],proj[1]];
});
const xStep = (xMax-xMin)/pixelWidth;
const yStep = (yMax-yMin)/pixelHeight;
let corespondingColRow = corespondingXY.map(v=>{
const col = Math.floor((v[0]-xMin)/xStep);
const row = -(Math.floor((v[1]-yMin)/yStep)) + (pixelHeight - 1);
return [col,row];
});
// Do the point queries
// Render.
// Currently the steps above are done in the callback below.
window.addEventListener('message',callback);
// Get the point querys done by iframe.
IFRAME_WINDOW.postMessage(gridPoints);
function callback(e){
document.getElementById('loader-warning').classList.add('display-none');
let pointQueries = e.data;
PIXELGRID = [];
for(let i = 0; i< pixelHeight;i++){
let row = [];
for(let j =0; j < pixelWidth;j++){
row.push(null);
}
PIXELGRID.push(row);
}
if(corespondingColRow.length!==pointQueries.length){
throw new Error('ERROR: LENGTH DONT MATCH');
}
for(let i = 0; i < corespondingColRow.length;i++){
let [col,row] = corespondingColRow[i];
if(row < pixelHeight && col < pixelWidth && row>=0 && col >=0)
PIXELGRID[row][col] = pointQueries[i];
}
// Write data to canvas
renderMap()
// Remove callback from window
window.removeEventListener('message',callback);
}
}
);
function renderMap(){
// Get colors
if(!PIXELGRID){return;}
let colorMap = new Map();
for(let input of colorInputs){
colorMap.set(input.name,input.value);
}
// Map Query data is all ready in PIXELGRID
// Loop Over PIXELGRID, coloring every pixel using data in colorMap
for(let [rowNum, row] of PIXELGRID.entries()){
for(let [colNum, value] of row.entries()){
if(value === null){
ctx.fillStyle = 'red';
ctx.fillRect(colNum,rowNum,1,1);
}
else{
ctx.fillStyle = colorMap.get(value[0]);
ctx.fillRect(colNum,rowNum,1,1);
}
}
}
}
radians.addEventListener('change', function(e){
trigMode = 'radians';
math.import({...OGTRIG,...OGINVERSETRIG},{override:true});
possiblyAutoSetXYBounds();
});
degrees.addEventListener('change', function(e){
trigMode = 'degrees';
math.import({...DEGTRIG,...DEGINVERSETRIG},{override:true});
possiblyAutoSetXYBounds();
});
document.getElementById("save-projection-as-png").addEventListener('click',
function(e){
if(PIXELGRID === null){
alert("You need to complete a projection before saving anything");
}
else{
let imgDataUrl = canvasElement.toDataURL('image/png');
var a = document.createElement('a');
a.href = imgDataUrl;
a.download = 'imgDataUrl';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
}
)
let flip = true;
document.getElementById('toggle-darkmode').addEventListener('click',
function(){
if(flip){
document.documentElement.setAttribute('data-theme','dark');
}
else{
document.documentElement.setAttribute('data-theme','light');
}
flip = flip ? false:true;
}
)
// Click through guide
document.getElementById("help-button").addEventListener('click',
function(){
// Show help modal.
document.getElementById('help-overlay').classList.toggle('display-none');
}
);
document.getElementById("help-x-out").addEventListener('click',
function(){
// Show help modal.
document.getElementById('help-overlay').classList.toggle('display-none');
}
);
document.getElementById('help-overlay').addEventListener(
'click',
function(){
// Show help modal.
document.getElementById('help-overlay').classList.toggle('display-none');
}
);
document.getElementById('help').addEventListener(
'click',
function(e){
e.stopPropagation();
}
)