-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_draw.go
1016 lines (840 loc) · 37.2 KB
/
js_draw.go
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
//go:build js && wasm
package config
import (
"fmt"
"math"
"time"
"math/rand"
)
// Struct to represent color stops for gradients.
type colorStop struct {
Position float64
Color string
}
// Helper function to draw a circle with a radial gradient.
func drawCircleWithGradient(cx, cy, radius float64, colorStops []colorStop) {
gradient := canvasObjectContext.Call("createRadialGradient", cx, cy, radius*0.3, cx, cy, radius)
for _, stop := range colorStops {
gradient.Call("addColorStop", stop.Position, stop.Color)
}
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
}
// Helper function to draw an arc.
func drawArc(cx, cy, radius float64, color string) {
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", cx, cy, radius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Set("fillStyle", color)
canvasObjectContext.Call("fill")
}
// Helper function to create a radial gradient with color stops.
func createRadialGradient(cx, cy, innerRadius, outerRadius float64, colorStops []colorStop) interface{} {
gradient := canvasObjectContext.Call("createRadialGradient", cx, cy, innerRadius, cx, cy, outerRadius)
for _, stop := range colorStops {
gradient.Call("addColorStop", stop.Position, stop.Color)
}
return gradient
}
// DrawAnomalyBlackHole draws a black hole on the document.
func DrawAnomalyBlackHole(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
scale := 1.0 + rand.Float64()/10
// Clear a larger area to enhance the effect of the black hole
clearRadius := radius * 1.3
canvasObjectContext.Call("clearRect", cx-clearRadius, cy-clearRadius, clearRadius*2, clearRadius*2)
// Draw the dark core of the black hole
drawArc(cx, cy, scale*0.6*radius, "black")
// Draw a subtle glow around the black hole to simulate light bending
glowGradient := createRadialGradient(cx, cy, scale*0.6*radius, scale*radius, []colorStop{
{0, "rgba(0, 0, 0, 0.0)"},
{1, "rgba(0, 0, 0, 0.3)"},
})
canvasObjectContext.Set("fillStyle", glowGradient)
drawArc(cx, cy, scale*radius, "")
// Draw the accretion disk around the black hole
gradient := createRadialGradient(cx, cy, scale*0.6*radius, scale*radius, []colorStop{
{0, "rgba(0, 0, 0, 0.0)"},
{0.15, "rgba(128, 0, 128, 0.2)"},
{0.35, "rgba(78, 0, 78, 0.6)"},
{0.6, "rgba(128, 0, 78, 0.8)"},
{0.8, "rgba(128, 0, 128, 0.6)"},
{1, "rgba(0, 0, 0, 0.0)"},
})
canvasObjectContext.Set("fillStyle", gradient)
drawArc(cx, cy, scale*radius, "")
}
// DrawAnomalySupernova draws a supernova on the document.
func DrawAnomalySupernova(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
scale := 1.0 + rand.Float64()/10
// Draw the epicenter using a radial gradient
epicenterGradient := createRadialGradient(cx, cy, radius*0.1, radius, []colorStop{
{0, "rgba(255, 255, 255, 1)"},
{0.3, "rgba(255, 215, 0, 0.9)"},
{0.6, "rgba(255, 165, 0, 0.4)"},
{0.8, "rgba(255, 69, 0, 0.2)"},
{1, "rgba(128, 0, 128, 0.0)"},
})
canvasObjectContext.Set("fillStyle", epicenterGradient)
drawArc(cx, cy, radius, "")
// Draw the first shockwave as a ring around the epicenter
firstShockwaveGradient := createRadialGradient(cx, cy, radius*1.1, radius*1.5, []colorStop{
{0, "rgba(255, 69, 0, 0.0)"},
{0.5, "rgba(255, 140, 0, 0.4)"},
{1, "rgba(255, 255, 0, 0.6)"},
})
canvasObjectContext.Set("fillStyle", firstShockwaveGradient)
drawArc(cx, cy, scale*radius*1.5, "")
// Draw the second shockwave as a larger ring further from the epicenter
secondShockwaveGradient := createRadialGradient(cx, cy, radius*1.7, radius*2.2, []colorStop{
{0, "rgba(255, 69, 0, 0.0)"},
{0.5, "rgba(255, 140, 0, 0.3)"},
{1, "rgba(255, 255, 255, 0.4)"},
})
canvasObjectContext.Set("fillStyle", secondShockwaveGradient)
drawArc(cx, cy, scale*radius*2.2, "")
}
// DrawBackground is a function that draws the background of the document.
// The background is drawn with the specified speed.
func DrawBackground(speed float64) {
if !*Config.Control.BackgroundAnimationEnabled {
canvasObjectContext.Call("drawImage", invisibleCanvas, 0, 0)
return
}
canvasDimensions := CanvasBoundingBox()
// Apply the speed
invisibleCanvasScrollY += speed
if invisibleCanvasScrollY/canvasDimensions.OriginalHeight > 1 {
invisibleCanvasScrollY -= canvasDimensions.OriginalHeight
}
canvasObjectContext.Call("drawImage", invisibleCanvas, 0, invisibleCanvasScrollY)
canvasObjectContext.Call("drawImage", invisibleCanvas, 0, invisibleCanvasScrollY-canvasDimensions.OriginalHeight)
}
// DrawLine is a function that draws a line on the document.
func DrawLine(start, end [2]float64, color string, thickness float64) {
defaultLineWidth := canvasObjectContext.Get("lineWidth")
defer canvasObjectContext.Set("lineWidth", defaultLineWidth)
canvasObjectContext.Set("strokeStyle", color)
canvasObjectContext.Set("lineWidth", thickness)
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("moveTo", start[0], start[1])
canvasObjectContext.Call("lineTo", end[0], end[1])
canvasObjectContext.Call("stroke")
}
// DrawPlanetEarth is a function that draws Earth on the document.
func DrawPlanetEarth(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
// Begin drawing the Earth
drawArc(cx, cy, radius, "")
// Use a blue gradient to represent the oceans
gradient := createRadialGradient(cx, cy, radius*0.2, radius, []colorStop{
{0, "#00BFFF"},
{1, "#1E90FF"},
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
// Add an atmospheric glow around the Earth
atmosphereGradient := createRadialGradient(cx, cy, radius, radius*1.2, []colorStop{
{0, "rgba(30, 144, 255, 0.1)"},
{1, "rgba(30, 144, 255, 0.0)"},
})
canvasObjectContext.Set("fillStyle", atmosphereGradient)
drawArc(cx, cy, radius*1.2, "")
// Clip to the planet's circle to restrict drawing within the Earth
canvasObjectContext.Call("save")
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", cx, cy, radius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip") // Apply clipping here before drawing the north pole and other elements
// Draw the north pole
poleRadiusInner := radius * 0.15
poleRadiusOuter := radius * 0.25
rotationAngle := math.Pi / 12
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("ellipse", cx, cy-radius, poleRadiusOuter, poleRadiusInner, rotationAngle, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Set("fillStyle", "#FFFFFF") // White for the pole
canvasObjectContext.Call("fill")
// Add more complex land masses with gradients for a realistic look
landColors := []string{"#228B22", "#8B4513"}
landPatches := [][5]float64{
{cx - radius*0.2, cy - radius*0.3, radius * 0.4, radius * 0.35, math.Pi / 45},
{cx + radius*0.1, cy + radius*0.2, radius * 0.35, radius * 0.3, math.Pi / 30},
{cx + radius*0.15, cy - radius*0.1, radius * 0.25, radius * 0.4, math.Pi / 60},
{cx + radius*0.85, cy + radius*0.2, radius * 0.3, radius * 0.25, math.Pi / 40},
}
for i, patch := range landPatches {
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("ellipse", patch[0], patch[1], patch[2], patch[3], patch[4], 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
landGradient := createRadialGradient(patch[0], patch[1], patch[2]*0.5, patch[2], []colorStop{
{0, landColors[i%len(landColors)]},
{1, "#556B2F"},
})
canvasObjectContext.Set("fillStyle", landGradient)
canvasObjectContext.Call("fill")
}
// Add more dynamic clouds with some variation
clouds := [][4]float64{
{cx - radius*0.4, cy - radius*0.1, radius * 0.6, radius * 0.2},
{cx + radius*0.3, cy + radius*0.2, radius * 0.5, radius * 0.25},
{cx - radius*0.2, cy + radius*0.1, radius * 0.4, radius * 0.15},
}
for _, cloud := range clouds {
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("ellipse", cloud[0], cloud[1], cloud[2], cloud[3], 0, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
cloudGradient := createRadialGradient(cloud[0], cloud[1], cloud[2]*0.5, cloud[2], []colorStop{
{0, "rgba(255, 255, 255, 0.8)"},
{1, "rgba(255, 255, 255, 0.4)"},
})
canvasObjectContext.Set("fillStyle", cloudGradient)
canvasObjectContext.Call("fill")
}
canvasObjectContext.Call("restore") // Restore the drawing state, removing the clip
// Draw the Moon orbiting Earth
moonRadius := radius * 0.27
moonDistance := radius * 60.3 / 30
// Calculate the moon's current position based on phase
const siderealMonth = 27.321661
referenceTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
elapsedDays := time.Now().UTC().Sub(referenceTime).Hours() / 24
phase := (elapsedDays / siderealMonth) * 2 * math.Pi
phase = math.Mod(phase, 2*math.Pi)
moonX := cx + moonDistance*math.Cos(phase)
moonY := cy + moonDistance*math.Sin(phase)
drawArc(moonX, moonY, moonRadius, "#F0F0F0")
// Add a crater to the Moon
craterX := moonX + moonRadius*0.2 // Position the crater slightly offset from the Moon's center
craterY := moonY + moonRadius*0.1
craterRadius := moonRadius * 0.3 // Crater is 30% the size of the Moon
canvasObjectContext.Call("save") // Save the drawing state to clip the Moon
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", craterX, craterY, craterRadius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip") // Clip to the Moon's circle
drawArc(craterX, craterY, craterRadius, "#A9A9A9")
canvasObjectContext.Call("restore") // Restore the drawing state to remove the clipping
}
// DrawPlanetJupiter is a function that draws Jupiter on the document.
func DrawPlanetJupiter(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1] // Center position
// Draw the main body of Jupiter (a sphere)
drawArc(cx, cy, radius, "")
// Create a radial gradient to simulate the planet's lighting and subtle pole banding
gradient := createRadialGradient(cx, cy, radius*0.3, radius, []colorStop{
{0, "#FFF4C3"},
{0.7, "#E2B56D"},
{1, "#B58A4C"},
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
// Clip the drawing area to the circle of the planet
canvasObjectContext.Call("save") // Save the current drawing state
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", cx, cy, radius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip") // Clip to the planet's circle
// Add bands to simulate Jupiter's gas bands
bandColors := []string{
"rgba(243, 210, 158, 0.7)", // #F3D29E (Pale Goldenrod) with 70% opacity
"rgba(234, 178, 119, 0.7)", // #EAB277 (Sandy Brown) with 70% opacity
"rgba(229, 170, 102, 0.7)", // #E5AA66 (Light Salmon) with 70% opacity
"rgba(223, 154, 85, 0.7)", // #DF9A55 (Moccasin) with 70% opacity
"rgba(217, 138, 68, 0.7)", // #D98A44 (Dark Salmon) with 70% opacity
"rgba(208, 122, 51, 0.7)", // #D07A33 (Chocolate) with 70% opacity
"rgba(201, 105, 34, 0.7)", // #C96922 (Peru) with 70% opacity
"rgba(194, 88, 17, 0.7)", // #C25811 (Sienna) with 70% opacity
"rgba(187, 71, 0, 0.7)", // #BB4700 (Dark Orange) with 70% opacity
}
bandHeight := (radius * 2) / float64(len(bandColors))
for i, color := range bandColors {
y := cy - radius + float64(i)*bandHeight
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("rect", cx-radius, y, radius*2, bandHeight)
canvasObjectContext.Set("fillStyle", color)
canvasObjectContext.Call("fill")
canvasObjectContext.Call("closePath")
}
// Add the Great Red Spot (simply a circle here)
drawArc(cx+radius*0.5, cy+radius*0.4, radius*0.3, "")
redSpotGradient := createRadialGradient(cx+radius*0.5, cy+radius*0.4, 0, radius*0.3, []colorStop{
{0, "#8B0000"},
{0.75, "#CD5C5C"},
{1, "#FF6347"},
})
canvasObjectContext.Set("fillStyle", redSpotGradient)
canvasObjectContext.Call("fill")
canvasObjectContext.Call("restore") // Restore the drawing state to remove the clipping
}
// DrawPlanetMars is a function that draws Mars on the document.
func DrawPlanetMars(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
drawArc(cx, cy, radius, "")
// Use a reddish color to represent Mars
gradient := createRadialGradient(cx, cy, radius*0.2, radius, []colorStop{
{0, "#FF7F50"},
{0.5, "#FF6347"},
{1, "#8B0000"},
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
// Darker patch representing a region like Syrtis Major
drawArc(cx-radius*0.2, cy-radius*0.1, radius*0.3, "")
patchGradient := createRadialGradient(cx-radius*0.2, cy-radius*0.1, 0, radius*0.3, []colorStop{
{0, "#8B0000"},
{1, "#A52A2A"},
})
canvasObjectContext.Set("fillStyle", patchGradient)
canvasObjectContext.Call("fill")
// Clip to the planet's circle to restrict the features within Mercury's shape
canvasObjectContext.Call("save")
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", cx, cy, radius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip") // Apply clipping here before drawing craters
// Draw crater-like features on Mars with shading
craters := [][3]float64{
{cx - radius*0.3, cy - radius*0.3, radius * 0.1},
{cx + radius*0.2, cy - radius*0.1, radius * 0.15},
{cx, cy + radius*0.3, radius * 0.05},
}
for _, crater := range craters {
drawArc(crater[0], crater[1], crater[2], "")
craterGradient := createRadialGradient(crater[0], crater[1], 0, crater[2], []colorStop{
{0, "#8B4513"},
{0.8, "#8B4513"},
{1, "#A0522D"},
})
canvasObjectContext.Set("fillStyle", craterGradient)
canvasObjectContext.Call("fill")
}
canvasObjectContext.Call("restore") // Restore the drawing state, removing the clip
}
// DrawPlanetMercury is a function that draws Mercury on the document.
func DrawPlanetMercury(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
drawArc(cx, cy, radius, "")
// Use a simple gray gradient to represent Mercury
gradient := createRadialGradient(cx, cy, radius*0.2, radius, []colorStop{
{0, "#C0C0C0"},
{0.7, "#A9A9A9"},
{1, "#808080"},
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
// Clip to the planet's circle to restrict the features within Mercury's shape
canvasObjectContext.Call("save")
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", cx, cy, radius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip") // Apply clipping here before drawing craters
// Draw crater-like features on Mercury with shading
craters := [][3]float64{
{cx - radius*0.3, cy - radius*0.3, radius * 0.1},
{cx + radius*0.2, cy - radius*0.1, radius * 0.15},
{cx, cy + radius*0.3, radius * 0.05},
}
for _, crater := range craters {
drawArc(crater[0], crater[1], crater[2], "")
craterGradient := createRadialGradient(crater[0], crater[1], 0, crater[2], []colorStop{
{0, "#696969"},
{0.9, "#A0A0A0"},
{1, "rgba(160, 160, 160, 0)"},
})
canvasObjectContext.Set("fillStyle", craterGradient)
canvasObjectContext.Call("fill")
}
canvasObjectContext.Call("restore") // Restore the drawing state, removing the clip
}
// DrawPlanetNeptune is a function that draws Neptune on the document.
func DrawPlanetNeptune(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
drawArc(cx, cy, radius, "")
// Deep blue color for Neptune
gradient := createRadialGradient(cx, cy, radius*0.3, radius, []colorStop{
{0, "#4682B4"},
{0.5, "#4169E1"},
{1, "#00008B"},
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
// Clip to the planet's circle to restrict the gas bands within Neptune's shape
canvasObjectContext.Call("save") // Save the current drawing state before clipping
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", cx, cy, radius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip")
// Add gas bands
bandColors := []string{
"rgba(100, 149, 237, 0.7)", // #6495ED (Cornflower Blue) with 70% opacity
"rgba(70, 130, 180, 0.7)", // #4682B4 (Steel Blue) with 70% opacity
"rgba(30, 144, 255, 0.7)", // #1E90FF (Dodger Blue) with 70% opacity
"rgba(135, 206, 250, 0.7)", // #87CEFA (Light Sky Blue) with 70% opacity
}
bandHeight := radius * 2 / float64(len(bandColors)) // Height of each band
for i, color := range bandColors {
y := cy - radius + float64(i)*bandHeight
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("rect", cx-radius, y, radius*2, bandHeight)
canvasObjectContext.Set("fillStyle", color)
canvasObjectContext.Call("fill")
canvasObjectContext.Call("closePath")
}
// Optionally, add a dark spot to represent one of Neptune's storms
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("ellipse", cx+radius*0.3, cy-radius*0.2, radius*0.2, radius*0.1, math.Pi/4, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Set("fillStyle", "rgba(0, 0, 139, 0.8)") // Dark blue spot
canvasObjectContext.Call("fill")
canvasObjectContext.Call("restore") // Restore the drawing state, removing the clip
}
// DrawPlanetPluto is a function that draws Pluto on the document.
func DrawPlanetPluto(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
// Draw the main body of Pluto with a gradient to simulate the icy surface
drawArc(cx, cy, radius, "")
// Create a gradient to represent Pluto's surface with icy and rocky textures
gradient := createRadialGradient(cx, cy, radius*0.2, radius, []colorStop{
{0, "#E8E8E8"}, // Light Gray for the center
{0.5, "#C0C0C0"}, // Silver for mid-range
{1, "#A9A9A9"}, // Dark Gray at the edges
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
// Clip to the planet's circle to restrict drawing within Pluto's shape
canvasObjectContext.Call("save") // Save the current drawing state before clipping
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", cx, cy, radius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip")
// Define static craters with fixed positions and sizes
craters := [][4]float64{
{cx - radius*0.3, cy - radius*0.2, radius * 0.12, 0}, // x, y, size, rotation (not used)
{cx + radius*0.2, cy + radius*0.1, radius * 0.15, 0},
{cx - radius*0.15, cy + radius*0.25, radius * 0.08, 0},
{cx + radius*0.35, cy - radius*0.3, radius * 0.1, 0},
{cx, cy - radius*0.35, radius * 0.18, 0},
}
craterColors := []string{
"#B0B0B0", // Light Gray
"#A9A9A9", // Dark Gray
"#8B8B8B", // Gray
}
// Draw the static craters
for _, crater := range craters {
drawArc(crater[0], crater[1], crater[2], "")
canvasObjectContext.Set("fillStyle", craterColors[int(crater[3])%len(craterColors)]) // Use fixed color
canvasObjectContext.Call("fill")
}
canvasObjectContext.Call("restore") // Restore the drawing state, removing the clip
}
// DrawPlanetSaturn is a function that draws Saturn on the document.
func DrawPlanetSaturn(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
// Define ring properties
innerRingRadius := radius * 1.2
outerRingRadius := radius * 2.0
ringTiltAngle := math.Pi / 6
ringThickness := radius * 0.15
// Save the context and rotate for the ring's tilt
canvasObjectContext.Call("save")
canvasObjectContext.Call("translate", cx, cy)
canvasObjectContext.Call("rotate", ringTiltAngle)
canvasObjectContext.Call("translate", -cx, -cy)
// Draw the upper half of the rings
for i := 0; i < 3; i++ {
// Clip the lower half of the ellipse to draw only the upper half
canvasObjectContext.Call("save")
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("rect", cx-outerRingRadius, cy-outerRingRadius, 2*outerRingRadius, outerRingRadius)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip")
// Draw the full ellipse, but only the upper half will be visible due to clipping
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("ellipse", cx, cy, outerRingRadius, innerRingRadius*0.4, 0, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Set("fillStyle", "rgba(210, 180, 140, 0.7)") // Consistent opacity for rings
canvasObjectContext.Call("fill")
// Restore to remove clipping
canvasObjectContext.Call("restore")
innerRingRadius += ringThickness
outerRingRadius += ringThickness * 1.5
}
// Restore context before drawing the planet's body
canvasObjectContext.Call("restore")
{
// Draw Saturn's body
drawArc(cx, cy, radius, "")
gradient := createRadialGradient(cx, cy, radius*0.3, radius, []colorStop{
{0, "#F5DEB3"}, // Wheat color
{0.5, "#EDD9A3"}, // Lightened Wheat
{1, "#DAA520"}, // Goldenrod color
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
}
// Save the context and rotate for the ring's tilt
canvasObjectContext.Call("save")
canvasObjectContext.Call("translate", cx, cy)
canvasObjectContext.Call("rotate", ringTiltAngle)
canvasObjectContext.Call("translate", -cx, -cy)
// Draw the lower half of the rings
innerRingRadius = radius * 1.2
outerRingRadius = radius * 2.0
for i := 0; i < 3; i++ {
// Clip the upper half of the ellipse to draw only the lower half
canvasObjectContext.Call("save")
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("rect", cx-outerRingRadius, cy, 2*outerRingRadius, outerRingRadius)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip")
// Draw the full ellipse, but only the lower half will be visible due to clipping
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("ellipse", cx, cy, outerRingRadius, innerRingRadius*0.4, 0, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Set("fillStyle", "rgba(210, 180, 140, 0.7)") // Same opacity as the upper half
canvasObjectContext.Call("fill")
// Restore to remove clipping
canvasObjectContext.Call("restore")
innerRingRadius += ringThickness
outerRingRadius += ringThickness * 1.5
}
// Restore the context to remove the rotation
canvasObjectContext.Call("restore")
}
// DrawPlanetUranus is a function that draws Uranus on the document.
func DrawPlanetUranus(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
// Add Uranus's tilted rings
innerRingRadius := radius * 1.4
outerRingRadius := radius * 1.8
ringTiltAngle := math.Pi / 6
// Save the context and rotate for the ring's tilt
canvasObjectContext.Call("save")
canvasObjectContext.Call("translate", cx, cy)
canvasObjectContext.Call("rotate", ringTiltAngle)
canvasObjectContext.Call("translate", -cx, -cy)
{
// Clip the lower half of the ellipse to draw only the upper half
canvasObjectContext.Call("save")
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("rect", cx-outerRingRadius, cy-outerRingRadius, 2*outerRingRadius, outerRingRadius)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip")
// Draw the full ellipse, but only the upper half will be visible due to clipping
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("ellipse", cx, cy, outerRingRadius, innerRingRadius*0.4, 0, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Set("strokeStyle", "rgba(169, 169, 169, 0.8)") // Dark Gray for rings
canvasObjectContext.Set("lineWidth", radius*0.05)
canvasObjectContext.Call("stroke")
// Restore to remove clipping
canvasObjectContext.Call("restore")
}
// Restore context before drawing the planet's body
canvasObjectContext.Call("restore")
{
// Draw Uranus's body
drawArc(cx, cy, radius, "")
// Cyan color for Uranus
gradient := createRadialGradient(cx, cy, radius*0.3, radius, []colorStop{
{0, "#E0FFFF"}, // Light Cyan at the center
{0.5, "#AFEEEE"}, // Pale Turquoise
{1, "#5F9EA0"}, // Cadet Blue at the edges
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
// Clip to the planet's circle to restrict the gas bands within Uranus's shape
canvasObjectContext.Call("save") // Save the current drawing state before clipping
canvasObjectContext.Call("clip")
// Add gas bands
bandColors := []string{
"rgba(176, 224, 230, 0.7)", // #B0E0E6 (Powder Blue) with 70% opacity
"rgba(173, 216, 230, 0.7)", // #ADD8E6 (Light Blue) with 70% opacity
"rgba(135, 206, 235, 0.7)", // #87CEEB (Sky Blue) with 70% opacity
"rgba(135, 206, 250, 0.7)", // #87CEFA (Light Sky Blue) with 70% opacity
}
bandHeight := radius * 2 / float64(len(bandColors)) // Height of each band
for i, color := range bandColors {
y := cy - radius + float64(i)*bandHeight
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("rect", cx-radius, y, radius*2, bandHeight)
canvasObjectContext.Set("fillStyle", color)
canvasObjectContext.Call("fill")
canvasObjectContext.Call("closePath")
}
canvasObjectContext.Call("restore") // Restore the drawing state, removing the clip
}
// Save the context and rotate for the ring's tilt
canvasObjectContext.Call("save")
canvasObjectContext.Call("translate", cx, cy)
canvasObjectContext.Call("rotate", ringTiltAngle)
canvasObjectContext.Call("translate", -cx, -cy)
{
// Clip the upper half of the ellipse to draw only the lower half
canvasObjectContext.Call("save")
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("rect", cx-outerRingRadius, cy, 2*outerRingRadius, outerRingRadius)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip")
// Draw the full ellipse, but only the lower half will be visible due to clipping
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("ellipse", cx, cy, outerRingRadius, innerRingRadius*0.4, 0, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Set("strokeStyle", "rgba(169, 169, 169, 0.8)") // Dark Gray for rings
canvasObjectContext.Set("lineWidth", radius*0.05)
canvasObjectContext.Call("stroke")
// Restore to remove clipping
canvasObjectContext.Call("restore")
}
// Restore the context to remove the rotation
canvasObjectContext.Call("restore")
// Reset the line width to the default value
canvasObjectContext.Set("lineWidth", 1.0)
}
// DrawPlanetVenus is a function that draws Venus on the document.
func DrawPlanetVenus(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
// Draw the main body of Venus with a gradient to simulate the thick atmosphere
drawArc(cx, cy, radius, "")
// Enhanced gradient with multiple stops to create depth
gradient := createRadialGradient(cx, cy, radius*0.2, radius, []colorStop{
{0, "#FFF8DC"}, // CornSilk at the center for a bright, hazy look
{0.5, "#F0E68C"}, // Khaki in the middle for a yellowish hue
{1, "#D2B48C"}, // Tan at the edges for a more defined atmospheric layer
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
// Clip to the planet's circle to restrict the drawing within Venus
canvasObjectContext.Call("save") // Save the current drawing state before clipping
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", cx, cy, radius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("clip")
// Add some cloud patterns or swirls
clouds := [][4]float64{
{cx - radius*0.4, cy - radius*0.4, radius * 0.6, radius * 0.2},
{cx + radius*0.3, cy + radius*0.2, radius * 0.5, radius * 0.25},
{cx - radius*0.2, cy + radius*0.35, radius * 0.4, radius * 0.15},
}
for _, cloud := range clouds {
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("ellipse", cloud[0], cloud[1], cloud[2], cloud[3], 0, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
canvasObjectContext.Set("fillStyle", "rgba(255, 255, 255, 0.2)")
canvasObjectContext.Call("fill")
}
canvasObjectContext.Call("restore") // Restore the drawing state, removing the clip
}
// DrawRect is a function that draws a rectangle on the document.
func DrawRect(coords [2]float64, size [2]float64, color string, cornerRadius float64) {
x, y := coords[0], coords[1]
width, height := size[0], size[1]
if cornerRadius == 0 {
canvasObjectContext.Set("fillStyle", color)
canvasObjectContext.Call("fillRect", x, y, width, height)
return
}
canvasObjectContext.Set("fillStyle", color)
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("moveTo", x+cornerRadius, y)
canvasObjectContext.Call("lineTo", x+width-cornerRadius, y)
canvasObjectContext.Call("quadraticCurveTo", x+width, y, x+width, y+cornerRadius)
canvasObjectContext.Call("lineTo", x+width, y+height-cornerRadius)
canvasObjectContext.Call("quadraticCurveTo", x+width, y+height, x+width-cornerRadius, y+height)
canvasObjectContext.Call("lineTo", x+cornerRadius, y+height)
canvasObjectContext.Call("quadraticCurveTo", x, y+height, x, y+height-cornerRadius)
canvasObjectContext.Call("lineTo", x, y+cornerRadius)
canvasObjectContext.Call("quadraticCurveTo", x, y, x+cornerRadius, y)
canvasObjectContext.Call("fill")
}
// DrawSpaceship is a function that draws a spaceship on the document.
// The spaceship is drawn at the specified position (x, y) with the specified width and height.
// The spaceship is drawn facing the specified direction.
// The spaceship is colored with the specified color.
// The spaceship can have a label displayed above or below it.
// The spaceship can have status bars displayed above or below it.
func DrawSpaceship(coors [2]float64, size [2]float64, faceUp bool, color, label string, statusValues []float64, statusColors []string) {
x, y := coors[0], coors[1]
width, height := size[0], size[1]
canvasObjectContext.Set("fillStyle", color)
canvasObjectContext.Set("strokeStyle", "black")
// Draw the body of the spaceship
canvasObjectContext.Call("fillRect", x+width*0.4, y+height*0.2, width*0.2, height*0.6)
canvasObjectContext.Call("strokeRect", x+width*0.4, y+height*0.2, width*0.2, height*0.6)
// Draw the wings
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("moveTo", x+width*0.4, y+height*0.2) // Left point of left wing
if faceUp {
canvasObjectContext.Call("lineTo", x, y+height*0.75) // Bottom point of left wing
} else {
canvasObjectContext.Call("lineTo", x, y+height*0.25) // Bottom point of left wing
}
canvasObjectContext.Call("lineTo", x+width*0.4, y+height*0.8) // Right point of left wing
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("fill")
canvasObjectContext.Call("stroke")
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("moveTo", x+width*0.6, y+height*0.2) // Right point of right wing
if faceUp {
canvasObjectContext.Call("lineTo", x+width, y+height*0.75) // Bottom point of right wing
} else {
canvasObjectContext.Call("lineTo", x+width, y+height*0.25) // Bottom point of right wing
}
canvasObjectContext.Call("lineTo", x+width*0.6, y+height*0.8) // Left point of right wing
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("fill")
canvasObjectContext.Call("stroke")
// Draw the tip of the spaceship
canvasObjectContext.Call("beginPath")
if faceUp {
canvasObjectContext.Call("moveTo", x+width*0.4, y+height*0.2) // Left point of the tip
canvasObjectContext.Call("lineTo", x+width*0.5, y) // Top point of the tip
canvasObjectContext.Call("lineTo", x+width*0.6, y+height*0.2) // Right point of the tip
} else {
canvasObjectContext.Call("moveTo", x+width*0.4, y+height*0.8) // Left point of the tip
canvasObjectContext.Call("lineTo", x+width*0.5, y+height) // Bottom point of the tip
canvasObjectContext.Call("lineTo", x+width*0.6, y+height*0.8) // Right point of the tip
}
canvasObjectContext.Call("closePath")
canvasObjectContext.Call("fill")
canvasObjectContext.Call("stroke")
// Draw the label above or below the spaceship
if label != "" {
canvasObjectContext.Set("font", "16px Arial") // Set font
// Shorten the label if it is too long
if len(label) > Config.Spaceship.MaximumLabelLength {
label = fmt.Sprintf("%s...", label[:Config.Spaceship.MaximumLabelLength-3])
}
// Measure the width of the label text
textMetrics := canvasObjectContext.Call("measureText", label)
labelWidth := textMetrics.Get("width").Float()
labelX := x + (width-labelWidth)/2 // Center the label horizontally
var labelY float64
if faceUp {
labelY = y + height + 10 // Below the spaceship
} else {
labelY = y - 5 // Above the spaceship
}
// Draw the label text with a black stroke and fill
canvasObjectContext.Set("strokeStyle", "black")
canvasObjectContext.Call("strokeText", label, labelX, labelY)
canvasObjectContext.Set("fillStyle", color) // Set text color
canvasObjectContext.Call("fillText", label, labelX, labelY)
}
// Draw the status bars
for i := 0; i < len(statusColors) && i < len(statusValues); i++ {
canvasObjectContext.Call("beginPath")
arcRadius := (width+height)/4 + 5 + float64(7*i) // Radius of the status arc
canvasObjectContext.Set("lineWidth", 5) // Set line width for the status arc
var startAngle, endAngle float64
if faceUp {
startAngle = math.Pi * 1.25 // Start angle (top-left)
endAngle = math.Pi * 1.75 // End angle (top-right)
canvasObjectContext.Call("arc", x+width/2, y+height*0.2, arcRadius, startAngle, endAngle, false)
} else {
startAngle = math.Pi * 0.25 // Start angle (bottom-left)
endAngle = math.Pi * 0.75 // End angle (bottom-right)
canvasObjectContext.Call("arc", x+width/2, y+height*0.8, arcRadius, startAngle, endAngle, false)
}
// Draw the background arc (gray)
canvasObjectContext.Set("strokeStyle", "rgba(128, 128, 128, 0.3)")
canvasObjectContext.Call("stroke")
value := statusValues[i]
if value > 1 {
value = 1
}
actualAngle := startAngle + (endAngle-startAngle)*value
canvasObjectContext.Call("beginPath")
if faceUp {
canvasObjectContext.Call("arc", x+width/2, y+height*0.2, arcRadius, startAngle, actualAngle, false)
} else {
canvasObjectContext.Call("arc", x+width/2, y+height*0.8, arcRadius, startAngle, actualAngle, false)
}
canvasObjectContext.Set("strokeStyle", statusColors[i])
canvasObjectContext.Call("stroke")
canvasObjectContext.Set("lineWidth", 1)
}
}
// DrawStar draws a star on the invisible canvas to be used as a background on the visible one.
// The star is drawn at the specified position (cx, cy) with the specified number of spikes.
// The outer radius and inner radius of the star are specified.
// The star is filled with the specified color.
func DrawStar(coords [2]float64, spikes int, radius, innerRadius float64, color string, brightness float64) {
cx, cy := coords[0], coords[1] // Center position
// Calculate the positions of the star
var positions [][2]float64
for i, r := 0, 0.0; i < 2*spikes; i++ {
if i%2 == 0 {
r = radius
} else {
r = innerRadius
}
angle := float64(i) * math.Pi / float64(spikes)
x := cx + math.Cos(angle)*r
y := cy + math.Sin(angle)*r
positions = append(positions, [2]float64{x, y})
}
// Draw the star
// Darken the color based on the brightness
for _, c := range []string{color, fmt.Sprintf("rgba(0, 0, 0, %.2f)", 1-brightness)} {
invisibleCtx.Call("beginPath")
invisibleCtx.Set("fillStyle", c)
invisibleCtx.Call("moveTo", positions[0][0], positions[0][1])
for i := 1; i < len(positions); i++ {
invisibleCtx.Call("lineTo", positions[i][0], positions[i][1])
}
invisibleCtx.Call("lineTo", positions[0][0], positions[0][1]) // Close the star
invisibleCtx.Call("closePath")
invisibleCtx.Call("fill")
}
}
// DrawSun is a function that draws the Sun on the document.
func DrawSun(coords [2]float64, radius float64) {
cx, cy := coords[0], coords[1]
scale := 1.0 + rand.Float64()/10
// Create a circular path for the Sun
canvasObjectContext.Call("beginPath")
canvasObjectContext.Call("arc", cx, cy, scale*radius, 0, 2*math.Pi, false)
canvasObjectContext.Call("closePath")
// Use a radial gradient to represent the Sun's glowing appearance
gradient := createRadialGradient(cx, cy, scale*radius*0.3, scale*radius, []colorStop{
{0, "rgba(255, 255, 0, 1)"},
{0.5, "rgba(255, 165, 0, 0.9)"},
{0.9, "rgba(255, 165, 0, 0.5)"},
{1, "rgba(255, 165, 0, 0)"},
})
canvasObjectContext.Set("fillStyle", gradient)
canvasObjectContext.Call("fill")
// Draw sun flares
numFlares := rand.Intn(9)
maxFlareLength := radius * 1.5
minFlareLength := radius * 1.1
flareThickness := 2.0
for i := 0; i < numFlares; i++ {
// Random angle for each flare
angle := 2 * math.Pi * rand.Float64()
// Random length for each flare
flareLength := minFlareLength + rand.Float64()*(maxFlareLength-minFlareLength)
// Calculate the end point of the flare
x := cx + flareLength*math.Cos(angle)
y := cy + flareLength*math.Sin(angle)
// Set the style for the flare
canvasObjectContext.Set("lineWidth", flareThickness)
gradient := canvasObjectContext.Call("createLinearGradient", cx, cy, x, y)
gradient.Call("addColorStop", 0, "rgba(255, 255, 0, 1)") // Bright yellow at the start