-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpanvas.js
1595 lines (1391 loc) · 42.1 KB
/
panvas.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
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
// isMobile function determines if the web page is opened on a mobile device
function isMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
}
// Variables used for handling user interaction
let keyCode, mouseX, mouseY, cMouseX, cMouseY, canvasX, canvasY;
let touches = [];
let touchX, touchY, prevTouchX, prevTouchY;
let mousePressed = false;
// Mathematical variables
const PI = Math.PI;
const TWO_PI = 2 * PI;
const HALF_PI = PI / 2;
const E = Math.E;
const SQRT2 = Math.SQRT2;
const SQRT1_2 = Math.SQRT1_2;
const LN2 = Math.LN2;
const LN10 = Math.LN10;
const LOG2E = Math.LOG2E;
const LOG10E = Math.LOG10E;
// Directional variables
let UP, DOWN, LEFT, RIGHT, CENTER, START, END, CORNER;
let mobile;
// Canvas variables
let width, height;
let innerWidth, innerHeight;
let can;
let frameRate = 60;
let interval = undefined;
let frameCount = 0;
let hasACanvas = true;
// Array used for auto updating objects
const updateable = [];
// Library that handles key codes
const KEY = {
LEFT_ARROW: 37,
UP_ARROW: 38,
RIGHT_ARROW: 39,
DOWN_ARROW: 40,
A: 65,
B: 66,
C: 67,
D: 68,
E: 69,
F: 70,
G: 71,
H: 72,
I: 73,
J: 74,
K: 75,
L: 76,
M: 77,
N: 78,
O: 79,
P: 80,
Q: 81,
R: 82,
S: 83,
T: 84,
U: 85,
V: 86,
W: 87,
X: 88,
Y: 89,
Z: 90,
0: 48,
1: 49,
2: 50,
3: 51,
4: 52,
5: 53,
6: 54,
7: 55,
8: 56,
9: 57,
NUM0: 96,
NUM1: 97,
NUM2: 98,
NUM3: 99,
NUM4: 100,
NUM5: 101,
NUM6: 102,
NUM7: 103,
NUM8: 104,
NUM9: 105,
ESC: 27,
TAB: 9,
CAPS_LOCK: 20,
LEFT_SHIFT: 16,
CONTROL: 17,
SPACE: 32,
CONTEXT_MENU: 93,
RIGHT_SHIFT: 16,
ENTER: 13,
BACKSPACE: 8,
ALT: 18,
F1: 112,
F2: 113,
F3: 114,
F4: 115,
F5: 116,
F6: 117,
F7: 118,
F8: 119,
F9: 120,
F10: 121,
F11: 122,
F12: 123,
PAUSE: 19,
DELETE: 46,
HOME: 36,
PAGE_UP: 33,
PAGE_DOWN: 34,
};
// Main Canvas class
class Canvas {
constructor(width_, height_, canvas_) {
// Handle canvas size
this.width = width_ || 100;
this.height = height_ || this.width;
// Determine if an existig canvas is being used or if a new one should be created
if (canvas_ === undefined) {
this.canvas = document.createElement("CANVAS");
document.body.appendChild(this.canvas);
} else this.canvas = canvas_;
// Update global variables
width = this.width;
height = this.height;
// Get canvas context used for manipulating the canvas
this.ctx = this.canvas.getContext("2d");
// Allow accessing the canvas element through a global variable
can = this;
// Handle canvas internal size variables
this.canvas.width = this.width;
this.canvas.height = this.height;
// Calling the loop function in an interval determined by the global framerate variable
interval = setInterval(loop, 1000 / frameRate);
// Set default maximum size of the canvas
this.maxWidth = 99000;
this.maxHeight = 99000;
// Set the background color
this.backgroundColor = "rgb(0, 0, 0)";
this.shouldClear = true;
this.isPaused = false;
// Handle shape defaults
this.lnWdth = 1;
this.rectDrawMode = "corner";
}
// clear method is used for "clearing" the canvas by drawing a square over it
clear() {
this.ctx.fillStyle = this.backgroundColor;
this.ctx.fillRect(
-this.maxWidth / 3,
-this.maxHeight / 3,
(this.maxWidth / 3) * 2,
(this.maxHeight / 3) * 2
);
}
// noClear method allows the user to disable clearing
noClear() {
this.shouldClear = false;
}
// setMaxSize method allows the user to change the canvas max size
setMaxSize(width_, height_) {
this.maxWidth = width_;
this.maxHeight = height_;
}
// setSize method allows the user to change the canvas size on the go
setSize(width_, height_) {
if (width_ === undefined && height_ === undefined) {
// Handle bad arguments
error("setSize function requires at least one argument");
} else {
// Update all canvas size variables
this.canvas.width = width_;
this.canvas.height = height_ || width_;
width = width_;
height = height_;
}
}
// fullScreen method allows the user to set the canvas to take the full window size
fullScreen() {
this.setSize(innerWidth, innerHeight);
}
// background method allows the user to change the canvas background
background(r, g, b, a) {
if (r === undefined) {
// Handle bad arguments
error("Invalid arguments for Canvas background method");
} else {
let red, green, blue, alpha;
// Check if the provided argument is a Color class
if (r instanceof Color) {
// Update canvas color based on the object arguments
red = r.red;
green = r.green;
blue = r.blue;
alpha = r.alpha;
this.backgroundColor = r.color();
} else {
// Update canvas color based on the provided arguments
red = r;
green = g === undefined ? r : g;
blue = b === undefined ? r : b;
alpha = a === undefined ? 255 : g;
this.backgroundColor = color(red, green, blue, alpha);
this.canvas.style.backgroundColor = color(red, green, blue, alpha);
}
}
}
// fill method allows the user to change the fill color of the canvas
fill(r, g, b, a) {
if (r === undefined) {
// Handle bad arguments
error("Invalid arguments for Canvas fill method");
} else {
let red, green, blue, alpha;
// Check if the provided argument is a Color class
if (r instanceof Color) {
// Update canvas fill color based on the object arguments
this.ctx.fillStyle = r.color();
} else {
// Update canvas fill color based on the provided arguments
red = r;
green = g === undefined ? r : g;
blue = b === undefined ? r : b;
if (g !== undefined && b === undefined) {
green = r;
alpha = g;
} else if (a !== undefined) {
alpha = a;
} else {
alpha = 255;
}
this.ctx.fillStyle = color(red, green, blue, alpha);
}
}
}
// noFill method allows the user to draw shapes with no fill color
noFill() {
this.ctx.fillStyle = "rgba(0, 0, 0, 0)";
}
// stroke method allows the user to set the stroke color of the drawing
stroke(r, g, b, a) {
if (r === undefined) {
// Handle bad arguments
error("Invalid arguments for Canvas stroke method");
} else {
let red, green, blue, alpha;
// Check if the provided argument is a Color class
if (r instanceof Color) {
// Update canvas stroke color based on the object arguments
this.ctx.fillStyle = r.color();
} else {
// Update canvas stroke color based on the provided arguments
red = r;
green = g === undefined ? r : g;
blue = b === undefined ? r : b;
if (g !== undefined && b === undefined) {
green = r;
alpha = g;
} else if (a !== undefined) {
alpha = a;
} else {
alpha = 255;
}
this.ctx.strokeStyle = color(red, green, blue, alpha);
}
}
}
// noStroke method allows the user to disable the stroke
noStroke() {
this.ctx.strokeStyle = "rgba(0, 0, 0, 0)";
}
// lineWidth method allows the user to change the stroke size
lineWidth(width_) {
this.ctx.lineWidth = width_;
this.lnWdth = width_;
}
// line method allows the user to draw a line on the canvas
line(x1, y1, x2, y2) {
// Handle bad arguments
if (
x1 === undefined ||
x2 === undefined ||
y1 === undefined ||
y2 === undefined
)
error("Invalid arguments for Canvas line method");
else {
// Draw a line based on the provided arguments
this.ctx.beginPath();
this.ctx.moveTo(x1, y1);
this.ctx.lineTo(x2, y2);
this.ctx.stroke();
}
}
// lineFromVector method allows the user to draw a line from a Vector class
lineFromVector(vector) {
// Handle bad arguments
if (vector === undefined || !(vector instanceof Vector))
error("Invalid argument for Canvas lineFromVector method");
else {
// Calculate line start and end point based on the vector information
let x1 = vector.x;
let y1 = vector.y;
let theta = vector.angle(true);
let length = vector.magnitude();
let x2 = length * cos(theta) + x1;
let y2 = length * sin(theta) + y1;
// Draw the line based on the calculated arguments
this.ctx.beginPath();
this.ctx.moveTo(x1, y1);
this.ctx.lineTo(x2, y2);
this.ctx.stroke();
}
}
// lineFromAngle method allows the user to draw a line based on an angle
lineFromAngle(x, y, angle, length) {
// Handle bad arguments
if (x === undefined)
error("Invalid arguments for Canvas lineFromAngle method");
else {
// Calculate lin end point based on the vector information
let x1 = x;
let y1 = y;
let theta = angle;
let len = length;
let x2 = len * cos(theta) + x1;
let y2 = len * sin(theta) + y1;
// Draw the line based on the calculated arguments
this.ctx.beginPath();
this.ctx.moveTo(x1, y1);
this.ctx.lineTo(x2, y2);
this.ctx.stroke();
}
}
// rect method allows the user to draw a rectangle on the canvas
rect(x, y, width_, height_) {
if (
x === undefined &&
y === undefined &&
width_ === undefined &&
height_ === undefined
) {
// Handle bad arguments
error("Invalid arguments for Canvas rect method");
} else {
// Prepare necessary variables
let x1 = x;
let y1 = y;
let wid, heig;
// Handle size if the provided arguments are incomplete
if (width_ === undefined && height_ === undefined) {
y1 = x1;
wid = y;
heig = width_;
} else if (width_ !== undefined && height_ === undefined) {
wid = width_;
heig = wid;
} else {
wid = width_;
heig = height_;
}
// Draw the canvas based on the set rectangle drawing origin
if (this.rectDrawMode === "corner") {
this.ctx.fillRect(x1, y1, wid, heig);
this.ctx.strokeRect(x1, y1, wid, heig);
} else if (this.rectDrawMode === "center") {
this.ctx.fillRect(x1 - wid / 2, y1 - heig / 2, wid, heig);
this.ctx.strokeRect(x1 - wid / 2, y1 - heig / 2, wid, heig);
}
}
}
// rectMode method allows the user to change the rectangle drawing origin of the canvas
rectMode(mode) {
// Handle bad arguments
if (mode !== "center" && mode !== "corner")
error("Invalid argument for Canvas rectMode method");
else this.rectDrawMode = mode;
}
// point method allows the user to draw a point on the canvas
point(x, y) {
// Handle bad arguments
if (x === undefined) error("Invalid arguments for Canvas point method");
else {
// Draw the point on the given coordinates with the size of the lineWidth
let x1 = x;
let y1 = y || x;
this.ctx.beginPath();
this.ctx.fillStyle = this.ctx.strokeStyle;
this.ctx.arc(x1, y1, this.lnWdth / 2, 0, PI * 2);
this.ctx.fill();
this.ctx.stroke();
this.ctx.closePath();
}
}
// circle method allows the user to draw a circle on the canvas
circle(x, y, r) {
if (x === undefined || y === undefined || r === undefined) {
// Handle bad arguments
error("Invalid arguments for Canvas circle method");
} else {
// Draw the circle based on the provided arguments
let x1 = x;
let y1 = y;
let radius = r;
this.ctx.beginPath();
this.ctx.arc(x1, y1, radius, 0, PI * 2);
this.ctx.fill();
this.ctx.stroke();
this.ctx.closePath();
}
}
// arc method allows the user to draw an arc shape on the canvas
arc(x, y, r, startAngle, endAngle) {
if (
x === undefined ||
y === undefined ||
r === undefined ||
startAngle === undefined ||
endAngle === undefined
) {
// Handle bad arguments
error("Invalid arguments for Canvas arc method");
} else {
// Draw an arc based on the provided arguments
let x1 = x;
let y1 = y;
let radius = r;
let sa = toRadians(startAngle - 90);
let ea = toRadians(endAngle - 90);
this.ctx.beginPath();
this.ctx.moveTo(x1, y1);
this.ctx.arc(x1, y1, radius, sa, ea);
this.ctx.closePath();
this.ctx.fill();
this.ctx.stroke();
this.ctx.closePath();
}
}
// ellipse method allows the user to draw an ellipse on the canvas
ellipse(x, y, width_, height_, rotation) {
// Handle bad arguments
if (x === undefined || height === undefined)
error("Invalid arguments for Canvas ellipse method");
else {
// Draw an ellipse based on the provided arguments
let x1 = x;
let y1 = y;
let wid = abs(width_);
let heig = abs(height_);
let angle = 0;
if (rotation !== undefined) angle = toRadians(rotation);
this.ctx.beginPath();
this.ctx.ellipse(x1, y1, wid, heig, angle, 0, TWO_PI);
this.ctx.fill();
this.ctx.stroke();
}
}
// beginShape method allows the user to start a shape at the given coordinates; in conjunction with the vertext and closeShape methods
beginShape(x, y) {
if (x === undefined)
error("Invalid arguments for Canvas beginShape method");
else {
this.ctx.beginPath();
this.ctx.moveTo(x, y);
}
}
// vertex method allows the user to place a new vertex in the started shape at the given coordinates; in conjunction with the beginShape and closeShape methods
vertex(x, y) {
if (x === undefined) error("Invalid arguments for Canvas vertex method");
else {
this.ctx.lineTo(x, y);
}
}
// closeShape method allows the user to close the shape at the started shape at the given coordinates; in conjunction with the vertext and beginShape methods
closeShape() {
this.ctx.closePath();
this.ctx.stroke();
this.ctx.fill();
}
// text method allows the user to write text on the canvas
text(text, x, y, fontSize, fontName) {
// Handle bad arguments
if (
text === undefined ||
x === undefined ||
y === undefined ||
fontSize === undefined ||
fontName === undefined
)
error("Invalid arguments for Canvas text method");
else {
// Write the text based on the given arguments
this.ctx.font = fontSize.toString() + "px " + fontName;
this.ctx.fillText(text, x, y);
this.ctx.strokeText(text, x, y);
}
}
// textAlign method allows the user to set the text alignment
textAlign(align) {
// Handle bad arguments
if (align === undefined) error("You need to specify the text alignment");
else {
// Update the text alignment
this.ctx.textAlign = align;
}
}
// translate method allows the user to move the canvas origin point to the provided coordinates
translate(x, y) {
// Handle bad arguments
if (x === undefined) error("Invalid arguments for Canvas translate method");
else {
// Translate the canvas origin point
if (y === undefined) {
y = x;
}
this.ctx.translate(x, y);
}
}
// rotate method allows the player to rotate the canvas by the angle
rotate(angle) {
if (angle === undefined) angle = 0;
this.ctx.rotate((angle * PI) / 180);
}
// save method allows the user to save the canvas state to memory
save() {
this.ctx.save();
}
// restore method allows the user to restore the saved canvas state
restore() {
this.ctx.restore();
}
// scale method allows the user to scale the canvas size
scale(width_, height_) {
let wid = width_ || 1;
let heig = height_ || wid;
this.ctx.scale(wid, heig);
}
// screenshot method allows the user to save a screenshot of the canvas
screenshot(name) {
let d = document.createElement("a");
let n;
if (name) {
n = name;
} else {
n = "panvasjs_screenshot" + randInt(999999);
}
// Download the screenshot to the user's device
let down = n + ".png";
d.setAttribute("download", down);
d.href = this.canvas
.toDataURL("image/png")
.replace(/^data:image\/[^;]/, "data:application/octet-stream");
d.click();
}
// playPause method allows the user to pause or unpause the canvas updates based on the current statee
playPause() {
if (interval) {
clearInterval(interval);
interval = null;
if (typeof onPause === "function") onPause();
this.isPaused = true;
} else {
framerate(frameRate);
this.isPaused = false;
}
}
// pause method allows the user to pause the canvas updates
pause() {
clearInterval(interval);
interval = null;
this.isPaused = true;
}
// play method allows the user to resume the canvas updates
play() {
framerate(frameRate);
this.isPaused = false;
}
// drawImage method allows the user to drawn an image to the canvas
drawImage(image, sx, sy, swidth, sheight, x, y, wid, heig) {
// Handle bad arguments
if (image === undefined)
error("Invalid arguments for Canvas drawImage method");
else {
if (swidth === undefined) this.ctx.drawImage(image.image, sx, sy);
else if (swidth !== undefined && sheight !== undefined && x === undefined)
this.ctx.drawImage(image.image, sx, sy, swidth, sheight);
else if (heig !== undefined)
this.ctx.drawImage(
image.image,
sx,
sy,
swidth,
sheight,
x,
y,
wid,
heig
);
}
}
}
// Main Vector class
class Vector {
constructor(x, y) {
// Prepare basic vector variables
this.x = 0;
this.y = 0;
this.previousX = 0;
this.previousY = 0;
if (x !== undefined) this.x = x;
if (y !== undefined) this.y = y;
}
// set method allows the user to update the vector coordinates
set(x, y) {
if (x === undefined || y === undefined) {
// Handle bad arguments
error("No value has been passed to the Vector set function");
} else {
// Update the vector coordinates based on the provided arguments
this.previousX = x;
this.previousX = y;
this.x = x;
this.y = y;
}
}
// add method allows the user to perform addition with the provided vector
add(vec2) {
if (vec2 === undefined) {
// Handle bad arguments
error("No vector has been passed to the Vector add function");
} else {
// Add the provided vector
this.previousX = this.x;
this.previousY = this.y;
this.x += vec2.x;
this.y += vec2.y;
}
}
// subtract method allows the user to perform subtraction with the provided vector
subtract(vec2) {
if (vec2 === undefined) {
// Handle bad arguments
error("No vector has been passed to the Vector subtract function");
} else {
// Subtract the provided vector
this.previousX = this.x;
this.previousY = this.y;
this.x -= vec2.x;
this.y -= vec2.y;
}
}
// multiply method allows the user to perform multiplication with the provided vector
multiply(num) {
if (num === undefined) num = 1;
this.previousX = this.x;
this.previousY = this.y;
this.x *= num;
this.y *= num;
}
// divide method allows the user to perform division with the provided vector
divide(num) {
if (num === undefined) num = 1;
num = 1 / num;
this.multiply(num);
}
// angle method allows the user to get the angle of the vector
angle(degrees) {
if (degrees != undefined) return (atan(this.y / this.x) * 180) / PI;
return atan(this.y / this.x);
}
// rotate method allows the user to rotate the vector by the given angle
rotate(angle) {
this.previousX = this.x;
this.previousY = this.y;
this.x = cos(angle) * this.previousX - sin(angle) * this.previousY;
this.y = sin(angle) * this.previousX + cos(angle) * this.previousY;
}
// magnitude method allows the user to get the magnitude of the vector
magnitude() {
return sqrt(sqr(this.x) + sqr(this.y));
}
// magnitudeSqr method allows the user to get the squared magnitude of the vector
magnitudeSqr() {
return sqr(this.x) + sqr(this.y);
}
// setMagnitude method allows the user to update the vector magnitude
setMagnitude(newMag) {
let mag = sqrt(sqr(this.x) + sqr(this.y));
let ratio = newMag / mag;
this.previousX = this.x;
this.previousY = this.y;
this.x *= ratio;
this.y *= ratio;
}
// limit method allows the user to set the maximum magnitude for the vector
limit(minMag, maxMag) {
let maxM = minMag;
let minM = null;
if (maxMag) {
maxM = maxMag;
minM = minMag;
}
if (minM && this.magnitude() < minM) this.setMagnitude(minM);
if (this.magnitude() > maxM) this.setMagnitude(maxM);
}
// copy method allows the user to get a copy of the vector
copy() {
return new Vector(this.x, this.y);
}
// normalize method allows the user to normalize the vector/set its magnitude to 1
normalize() {
let tmp = new Vector(this.x, this.y);
let mag = sqrt(sqr(tmp.x) + sqr(tmp.y));
tmp.x = tmp.x / mag;
tmp.y = tmp.y / mag;
this.previousX = this.x;
this.previousY = this.y;
this.x = tmp.x;
this.y = tmp.y;
}
// distance method allows the user to get the distance to another vector
distance(vec2) {
// Handle bad arguments
if (vec2 === undefined)
error("You need to pass another vector to the Vector distance method");
else return sqrt(sqr(this.x - vec2.x) + sqr(this.y - vec2.y));
}
// isOffScreen method allows the user to check if the vector is off the edges of the canvas
isOffScreen() {
if (this.x >= width || this.x < 0 || this.y >= height || this.y < 0)
return true;
else return false;
}
// lerp method allows the user to lerp the vector towards another vector over time
lerp(vec2, step) {
// Handle bad arguments
if (vec2 === undefined || step === undefined)
error("Invalid arguments for Vector lerp method");
else {
this.x = lerp(this.x, vec2.x, step);
this.y = lerp(this.y, vec2.y, step);
}
}
// constrain method allows the user to constrain the method between the set coordinates
constrain(minX, maxX, minY, maxY) {
if (this.x >= maxX) this.x = maxX;
else if (this.x <= minX) this.x = minX;
if (this.y >= maxY) this.y = maxY;
else if (this.y <= minY) this.y = minY;
}
}
// Vector.fromAngle method allows the user to create a vector from the given angles
Vector.__proto__.fromAngle = (angle) => {
return new Vector(cos(angle), sin(angle));
};
// Main Point class
class Point {
constructor(x, y) {
// Define all necessary variables
this.x = x;
this.y = y;
}
// distance method allows the user to get the distance to another point
distance(pt2) {
if (pt2 === undefined)
error("You need to pass another point to the Point distance method");
else return sqrt(sqr(this.x - pt2.x) + sqr(this.y - pt2.y));
}
// isOffScreen method allows the user to check if the point is off the edges of the canvas
isOffScreen() {
if (
this.x >= canvas.width ||
this.x < 0 ||
this.y >= canvas.height ||
this.y < 0
)
return true;
else return false;
}
}
// Main Color class
class Color {
constructor(red, green, blue, alpha) {
// Define all necessary variables
this.red = red || 0;
this.alpha = alpha || 255;
if (green !== undefined && blue !== undefined) {
this.green = green;
this.blue = blue;
this.alpha = 255;
} else if (green !== undefined && blue === undefined) {
this.green = red;
this.blue = red;
this.alpha = green;
} else {
this.green = red;
this.blue = red;
}
}
// randomize method allows the user to get a random variation of the color
randomize(randomizeAlpha) {
let r, g, b, a;
let offset = randInt(-100, 100);
r = this.red + offset;
g = this.green + offset;
b = this.blue + offset;
a = randomizeAlpha ? this.alpha + offset : this.alpha;
return new Color(r, g, b, a);
}
// color method allows the user to get the object as a usable color code
color() {
return color(this.red, this.green, this.blue, this.alpha);
}
}
// Main Image class
class Image {
constructor(path) {
// Define all necessary variables
this.path = path;
this.filename = this.path.split("/").pop();
this.image = document.createElement("IMG");
this.image.src = this.path;
}
}
// Main Sound class
class Sound {
constructor(path) {
// Define all necessary variables
this.path = path;
this.filename = this.path.split("/").pop();
this.audio = document.createElement("AUDIO");
this.audio.src = this.path;
}
// play method allows the user to play the audio
play() {
this.audio.play();
}
// pause method allows the user to pause the audio
pause() {
this.audio.pause();
}
// playPause method allows the user to play or pause the audio based on its current state
playPause() {
if (this.audio.paused) this.play();
else this.pause();
}
}
// Main Store class
class Store {
// save method allows the user to save data to the local storage
save(name, data) {
let d;
if (typeof data === "object") d = JSON.stringify(data);
else d = data;
window.localStorage.setItem(name, d);
}
// load method allows the user to load data from the local storage
load(name) {
let d = window.localStorage.getItem(name);
let data = JSON.parse(d);
return data;
}
// removeItem method allows the user to remvoe the provided item from the local storage
removeItem(name) {
window.localStorage.removeItem(name);
}