-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
1034 lines (814 loc) · 35.2 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
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
/*
________________________________________________
( Text Animation ()
\-----------------------------------------------\
| |
| Copyright 2024 ag-sanjjeev |
| |
|-----------------------------------------------|
| The source code is licensed under |
| MIT-style License. |
| |
|-----------------------------------------------|
| |
| The usage, permission and condition |
| are applicable to this source code |
| as per license. |
| |
|-----------------------------------------------|
| |
| That can be found in LICENSE file |
| or at https://opensource.org/licenses/MIT. |
(_______________________________________________(
*/
/* global constants */
const inputText = document.getElementById('inputText');
const canvasWidth = document.getElementById('canvasWidth');
const canvasHeight = document.getElementById('canvasHeight');
const animationScale = document.getElementById('animationScale');
const fps = document.getElementById('fps');
const transition = document.getElementById('transition');
const inTransitionDuration = document.getElementById('inTransitionDuration');
const outTransitionDuration = document.getElementById('outTransitionDuration');
const animationDuration = document.getElementById('animationDuration');
const paddingX = document.getElementById('paddingX');
const paddingY = document.getElementById('paddingY');
const lineHeight = document.getElementById('lineHeight');
const fontSize = document.getElementById('fontSize');
const textFontFamily = document.getElementById('textFontFamily');
const fontStyle = document.getElementById('fontStyle');
const textAlignment = document.getElementById('textAlignment');
const textColor = document.getElementById('textColor');
const backgroundColor = document.getElementById('backgroundColor');
const setFontButton = document.getElementById('setFontButton');
const setDeviceWidthButton = document.getElementById('setDeviceWidthButton');
const setDeviceHeightButton = document.getElementById('setDeviceHeightButton');
const playButton = document.getElementById('playButton');
const recordButton = document.getElementById('recordButton');
const fullScreen = document.getElementById('fullScreen');
const canvas = document.getElementById('canvas1');
const canvasCTX = canvas.getContext('2d');
let x = 0;
let y = 0;
// custom animation end event
const animationEndEvent = new CustomEvent('animationEnded', {
detail: {
message: 'Animation Frame Ended'
}
});
/* Class PreferenceHandler */
class PreferenceHandler {
constructor() {
this._fontFamily = null;
this._animationObject = null;
this._animationFrameReference = null;
this._stream = null;
this._recorder = null;
this._videoChunks = [];
}
// set and get method for audio property
set animationObject(obj) {
this._animationObject = obj;
}
get animationObject() {
return this._animationObject;
}
// set and get method for animationFrameReference property
set animationFrameReference(ref) {
this._animationFrameReference = ref;
}
get animationFrameReference() {
return this._animationFrameReference;
}
// set and get method for stream property
set setStream(stream) {
this._stream = stream;
}
get getStream() {
return this._stream;
}
// set and get method for recorder property
set setRecorder(recorder) {
this._recorder = recorder;
}
get getRecorder() {
return this._recorder;
}
// set and get method for videoChunks property
set setVideoChunks(videoChunks) {
this._videoChunks = videoChunks;
}
get getVideoChunks() {
return this._videoChunks;
}
// set and get method for fontFamily property
set setFontFamily(fontFamily) {
this._fontFamily = fontFamily;
}
get getFontFamily() {
return this._fontFamily;
}
}
/* Initiating PreferenceHandler */
const prefObj = new PreferenceHandler();
/* Setting Preferences function*/
function setPreference() {
inputText.value = (getLocalStorage('text-animation.inputText') == null) ? inputText.value : getLocalStorage('text-animation.inputText');
canvasWidth.value = (getLocalStorage('text-animation.canvasWidth') == null) ? canvasWidth.value : getLocalStorage('text-animation.canvasWidth');
canvasHeight.value = (getLocalStorage('text-animation.canvasHeight') == null) ? canvasHeight.value : getLocalStorage('text-animation.canvasHeight');
transition.value = (getLocalStorage('text-animation.transition') == null) ? transition.options[0].value : getLocalStorage('text-animation.transition');
inTransitionDuration.value = (getLocalStorage('text-animation.inTransitionDuration') == null) ? inTransitionDuration.value : getLocalStorage('text-animation.inTransitionDuration');
outTransitionDuration.value = (getLocalStorage('text-animation.outTransitionDuration') == null) ? outTransitionDuration.value : getLocalStorage('text-animation.outTransitionDuration');
animationDuration.value = (getLocalStorage('text-animation.animationDuration') == null) ? animationDuration.value : getLocalStorage('text-animation.animationDuration');
fps.value = (getLocalStorage('text-animation.fps') == null) ? fps.value : getLocalStorage('text-animation.fps');
paddingX.value = (getLocalStorage('text-animation.paddingX') == null) ? paddingX.value : getLocalStorage('text-animation.paddingX');
paddingY.value = (getLocalStorage('text-animation.paddingY') == null) ? paddingY.value : getLocalStorage('text-animation.paddingY');
lineHeight.value = (getLocalStorage('text-animation.lineHeight') == null) ? lineHeight.value : getLocalStorage('text-animation.lineHeight');
fontSize.value = (getLocalStorage('text-animation.fontSize') == null) ? fontSize.value : getLocalStorage('text-animation.fontSize');
textFontFamily.value = (getLocalStorage('text-animation.textFontFamily') == null) ? textFontFamily.value : getLocalStorage('text-animation.textFontFamily');
fontStyle.value = (getLocalStorage('text-animation.fontStyle') == null) ? fontStyle.options[0].value : getLocalStorage('text-animation.fontStyle');
textAlignment.value = (getLocalStorage('text-animation.textAlignment') == null) ? textAlignment.options[0].value : getLocalStorage('text-animation.textAlignment');
textColor.value = (getLocalStorage('text-animation.textColor') == null) ? textColor.value : getLocalStorage('text-animation.textColor');
backgroundColor.value = (getLocalStorage('text-animation.backgroundColor') == null) ? backgroundColor.value : getLocalStorage('text-animation.backgroundColor');
}
/* class TextAnimation */
class TextAnimation {
// private property
#totalTextHeight = 0;
#wrappedText;
// constructor method
constructor (inputText) {
this.inputText = inputText.split('\n');
this.fontSize = fontSize.value;
this.backgroundColor = backgroundColor.value;
this.textColor = textColor.value;
this.paddingX = paddingX.value;
this.paddingY = paddingY.value;
this.lineHeight = lineHeight.value;
this.fontFamily = textFontFamily.value;
this.fontStyle = fontStyle.value;
this.textAlignment = textAlignment.value;
this.interval = 1000/fps.value;
this.timer = 0;
this.recorderState = 'initialized';
this.recorder = null;
this.animationFrameReference = null;
canvas.width = canvasWidth.value;
canvas.height = canvasHeight.value;
canvas.style.backgroundColor = this.backgroundColor;
this.redColor = 0;
this.greenColor = 0;
this.blueColor = 0;
this.alphaTransparent = 0;
this.x = this.paddingX / 2;
this.y = 0;
this.underlineBarHeight = 2;
this.underlineHeightSpace = 15;
this.lineIndex = 0;
this.inTransitionDuration = 0;
this.outTransitionDuration = 0;
this.animationDuration = 0;
this.totalCurrentAnimationDuration = 0;
this.animationStartTime = 0;
this.transition = null;
this.scale = 1;
this.timer = 0;
this.lastTimeStamp = 0;
}
// animateText method
animate(timeStamp) {
// setting default font if it is an empty
if (this.fontFamily == null || this.fontFamily == undefined) { this.fontFamily = 'Ysabeau Infant'; }
// clear everything in canvas before start next frame and set last scale if it is zoom in
canvasCTX.clearRect(0, 0, canvas.width, canvas.height);
canvasCTX.scale(this.scale, this.scale);
setCanvasBackground();
this.transition = transition.value;
this.textColor = textColor.value;
this.totalCurrentAnimationDuration = Number(this.inTransitionDuration) + Number(this.animationDuration) + Number(this.outTransitionDuration);
if (this.lastTimeStamp == 0) { this.lastTimeStamp = timeStamp; }
if (this.lineIndex >= this.inputText.length) {
this.stopAnimation();
} else if (timeStamp - this.lastTimeStamp > this.totalCurrentAnimationDuration) {
this.lineIndex++;
this.alphaTransparent = 0;
this.lastTimeStamp = timeStamp;
this.animationFrameReference = window.requestAnimationFrame(this.animate.bind(this));
} else {
this.switchTransition(timeStamp);
this.animationFrameReference = window.requestAnimationFrame(this.animate.bind(this));
}
}
switchTransition(timeStamp) {
if (this.transition == 'showInOut') {
this.showInOut(timeStamp);
} else if (this.transition == 'fadeInOut') {
this.fadeInOut(timeStamp);
} else if (this.transition == 'zoomInOut') {
this.zoomInOut(timeStamp);
} else if (this.transition == 'zoomIn') {
this.zoomIn(timeStamp);
} else {
throw Error('Unknown transition');
}
}
// showInOut method
showInOut(timeStamp) {
let lines;
let lineWidth;
let tempLineHeight;
let newTempLine;
let underlineHeight = 0;
lines = this.inputText[this.lineIndex];
this.wrapText(lines);
lines = this.#wrappedText;
tempLineHeight = (Number(this.fontSize) + Number(this.lineHeight));
this.y = Number(canvas.height/2);// - ((Number(this.fontSize) + Number(this.lineHeight)) * lines.length * 0.5);
this.y -= lines.length * 0.5 * tempLineHeight;
// there is no text justified alignment
if (this.textAlignment != '') {
canvasCTX.textAlign = (this.textAlignment != 'justified') ? this.textAlignment : 'left';
}
for (let i = 0; i < lines.length; i++) {
let textFontStyle = this.fontStyle.replaceAll('underline', '').trim();
// setting font style
canvasCTX.font = `${textFontStyle} ${this.fontSize}px ${this.fontFamily}, san-serif`;
lineWidth = canvasCTX.measureText(lines[i]).width;
newTempLine = lines[i];
// assigning x co-ordinates for right, center and justify text alignment
if (this.textAlignment == 'right') {
this.x = (Number(this.x) + Number(canvas.width) - this.paddingX) / 2;
} else if (this.textAlignment == 'center') {
this.x = (Number(canvas.width)) / 2;
} else if (this.textAlignment == 'justified') {
// checking if it is an empty line
if (lines[i].trim() != '') {
// getting free space width metrics
let noOfFreeSpaces = (canvas.width - this.paddingX) - lineWidth;
// proceeds only if any whitespace
if (lines[i].indexOf(' ') !== -1) {
// getting number of whitespace in the line
let noOfWhiteSpaces = lines[i].match(/([\s]+)/g).length;
// if it has atleast one whitespace to proceed
if (noOfFreeSpaces > 0) {
// measuring single whitespace width metric for text style
let whiteSpaceWidth = canvasCTX.measureText(' ').width;
// calculating new white space counts
let newWhiteSpaces = Math.ceil((noOfFreeSpaces / noOfWhiteSpaces) / whiteSpaceWidth);
// appending and sharing equal whitespaces to all whitespaces in the line
let whiteSpaces = '';
for (let _i = 0; _i < newWhiteSpaces; _i++) {
whiteSpaces += ' ';
}
newTempLine = lines[i].trim();
newTempLine = newTempLine.replace(/\s/g, whiteSpaces);
}
}
}
}
/* Filling Text */
canvasCTX.fillStyle = this.textColor;
canvasCTX.fillText(newTempLine, this.x,this.y);
// applying underline font style
if (this.fontStyle.indexOf('underline') !== -1) {
// default co-ordinates will be followed for left text alignment
let underlineX = this.x;
// measuring width metrics for new processed line text
lineWidth = canvasCTX.measureText(newTempLine).width;
// setting underline color as same as text color
canvasCTX.fillStyle = this.textColor;
// underline x co-ordinate will be differs for right and center alignment
if (this.textAlignment == 'right') {
underlineX = (canvas.width - this.paddingX) - lineWidth;
} else if (this.textAlignment == 'center') {
underlineX = Number(this.x) - Number(lineWidth / 2);
}
// filling rectangular line as an underline below to text
canvasCTX.fillRect(underlineX, Number(this.y) + this.underlineHeightSpace, lineWidth, this.underlineBarHeight);
}
this.y += Number(tempLineHeight) + Number(this.underlineHeightSpace) + Number(this.underlineBarHeight);
}
}
// fadeInOut method
fadeInOut(timeStamp) {
let lines;
let lineWidth;
let tempLineHeight;
let newTempLine;
let underlineHeight = 0;
let rgbColor = null;
lines = this.inputText[this.lineIndex];
this.wrapText(lines);
lines = this.#wrappedText;
tempLineHeight = (Number(this.fontSize) + Number(this.lineHeight));
this.y = Number(canvas.height/2);// - ((Number(this.fontSize) + Number(this.lineHeight)) * lines.length * 0.5);
this.y -= lines.length * 0.5 * tempLineHeight;
// there is no text justified alignment
if (this.textAlignment != '') {
canvasCTX.textAlign = (this.textAlignment != 'justified') ? this.textAlignment : 'left';
}
for (let i = 0; i < lines.length; i++) {
let textFontStyle = this.fontStyle.replaceAll('underline', '').trim();
// setting font style
canvasCTX.font = `${textFontStyle} ${this.fontSize}px ${this.fontFamily}, san-serif`;
lineWidth = canvasCTX.measureText(lines[i]).width;
newTempLine = lines[i];
// assigning x co-ordinates for right, center and justify text alignment
if (this.textAlignment == 'right') {
this.x = (Number(this.x) + Number(canvas.width) - this.paddingX) / 2;
} else if (this.textAlignment == 'center') {
this.x = (Number(canvas.width)) / 2;
} else if (this.textAlignment == 'justified') {
// checking if it is an empty line
if (lines[i].trim() != '') {
// getting free space width metrics
let noOfFreeSpaces = (canvas.width - this.paddingX) - lineWidth;
// proceeds only if any whitespace
if (lines[i].indexOf(' ') !== -1) {
// getting number of whitespace in the line
let noOfWhiteSpaces = lines[i].match(/([\s]+)/g).length;
// if it has atleast one whitespace to proceed
if (noOfFreeSpaces > 0) {
// measuring single whitespace width metric for text style
let whiteSpaceWidth = canvasCTX.measureText(' ').width;
// calculating new white space counts
let newWhiteSpaces = Math.ceil((noOfFreeSpaces / noOfWhiteSpaces) / whiteSpaceWidth);
// appending and sharing equal whitespaces to all whitespaces in the line
let whiteSpaces = '';
for (let _i = 0; _i < newWhiteSpaces; _i++) {
whiteSpaces += ' ';
}
newTempLine = lines[i].trim();
newTempLine = newTempLine.replace(/\s/g, whiteSpaces);
}
}
}
}
/* FadeIn Effect */
if (timeStamp - this.lastTimeStamp < this.inTransitionDuration) {
this.alphaTransparent = (Number(timeStamp - this.lastTimeStamp) / Number(this.inTransitionDuration));
} else if (timeStamp - this.lastTimeStamp < Number(this.inTransitionDuration) + Number(this.animationDuration)) { /* Static Color */
this.alphaTransparent = 1;
} else if (timeStamp - this.lastTimeStamp < this.totalCurrentAnimationDuration) { /* FadeOut Effect */
this.alphaTransparent = 1 - (Number(timeStamp - this.lastTimeStamp - this.inTransitionDuration - this.animationDuration) / Number(this.outTransitionDuration));
}
// convert hex color code into rgb color value
rgbColor = this.hexToRGB(this.textColor);
/* Filling Text */
canvasCTX.fillStyle = `rgba(${rgbColor.red},${rgbColor.green},${rgbColor.blue}, ${this.alphaTransparent})`; //this.textColor;
// canvasCTX.fillStyle = this.textColor;
canvasCTX.fillText(newTempLine, this.x,this.y);
// applying underline font style
if (this.fontStyle.indexOf('underline') !== -1) {
// default co-ordinates will be followed for left text alignment
let underlineX = this.x;
// measuring width metrics for new processed line text
lineWidth = canvasCTX.measureText(newTempLine).width;
// setting underline color as same as text color
canvasCTX.fillStyle = this.textColor;
// underline x co-ordinate will be differs for right and center alignment
if (this.textAlignment == 'right') {
underlineX = (canvas.width - this.paddingX) - lineWidth;
} else if (this.textAlignment == 'center') {
underlineX = Number(this.x) - Number(lineWidth / 2);
}
// filling rectangular line as an underline below to text
canvasCTX.fillRect(underlineX, Number(this.y) + this.underlineHeightSpace, lineWidth, this.underlineBarHeight);
}
this.y += Number(tempLineHeight) + Number(this.underlineHeightSpace) + Number(this.underlineBarHeight);
}
}
// zoomInOut method
zoomInOut(timeStamp) {
let lines;
let lineWidth;
let tempLineHeight;
let newTempLine;
let underlineHeight = 0;
let tempFontSize = 0;
lines = this.inputText[this.lineIndex];
this.wrapText(lines);
lines = this.#wrappedText;
tempLineHeight = (Number(this.fontSize) + Number(this.lineHeight));
this.y = Number(canvas.height/2);
this.y -= lines.length * 0.5 * tempLineHeight;
// there is no text justified alignment
if (this.textAlignment != '') {
canvasCTX.textAlign = (this.textAlignment != 'justified') ? this.textAlignment : 'left';
}
for (let i = 0; i < lines.length; i++) {
let textFontStyle = this.fontStyle.replaceAll('underline', '').trim();
// setting font style
canvasCTX.font = `${textFontStyle} ${this.fontSize}px ${this.fontFamily}, san-serif`;
lineWidth = canvasCTX.measureText(lines[i]).width;
newTempLine = lines[i];
// assigning x co-ordinates for right, center and justify text alignment
if (this.textAlignment == 'right') {
this.x = (Number(this.x) + Number(canvas.width) - this.paddingX) / 2;
} else if (this.textAlignment == 'center') {
this.x = (Number(canvas.width)) / 2;
} else if (this.textAlignment == 'justified') {
// checking if it is an empty line
if (lines[i].trim() != '') {
// getting free space width metrics
let noOfFreeSpaces = (canvas.width - this.paddingX) - lineWidth;
// proceeds only if any whitespace
if (lines[i].indexOf(' ') !== -1) {
// getting number of whitespace in the line
let noOfWhiteSpaces = lines[i].match(/([\s]+)/g).length;
// if it has atleast one whitespace to proceed
if (noOfFreeSpaces > 0) {
// measuring single whitespace width metric for text style
let whiteSpaceWidth = canvasCTX.measureText(' ').width;
// calculating new white space counts
let newWhiteSpaces = Math.ceil((noOfFreeSpaces / noOfWhiteSpaces) / whiteSpaceWidth);
// appending and sharing equal whitespaces to all whitespaces in the line
let whiteSpaces = '';
for (let _i = 0; _i < newWhiteSpaces; _i++) {
whiteSpaces += ' ';
}
newTempLine = lines[i].trim();
newTempLine = newTempLine.replace(/\s/g, whiteSpaces);
}
}
}
}
/* ZoomIn Effect */
if (timeStamp - this.lastTimeStamp < this.inTransitionDuration) {
tempFontSize = this.fontSize * (Number(timeStamp - this.lastTimeStamp) / Number(this.inTransitionDuration));
} else if (timeStamp - this.lastTimeStamp < Number(this.inTransitionDuration) + Number(this.animationDuration)) { /* Static Size */
tempFontSize = this.fontSize;
} else if (timeStamp - this.lastTimeStamp < this.totalCurrentAnimationDuration) { /* ZoomOut Effect */
tempFontSize = this.fontSize - (this.fontSize * (Number(timeStamp - this.lastTimeStamp - this.inTransitionDuration - this.animationDuration) / Number(this.outTransitionDuration)));
}
// setting font style
canvasCTX.font = `${textFontStyle} ${tempFontSize}px ${this.fontFamily}, san-serif`;
/* Filling Text */
canvasCTX.fillStyle = this.textColor;
canvasCTX.fillText(newTempLine, this.x,this.y);
// applying underline font style
if (this.fontStyle.indexOf('underline') !== -1) {
// default co-ordinates will be followed for left text alignment
let underlineX = this.x;
// measuring width metrics for new processed line text
lineWidth = canvasCTX.measureText(newTempLine).width;
// setting underline color as same as text color
canvasCTX.fillStyle = this.textColor;
// underline x co-ordinate will be differs for right and center alignment
if (this.textAlignment == 'right') {
underlineX = (canvas.width - this.paddingX) - lineWidth;
} else if (this.textAlignment == 'center') {
underlineX = Number(this.x) - Number(lineWidth / 2);
}
// filling rectangular line as an underline below to text
canvasCTX.fillRect(underlineX, Number(this.y) + this.underlineHeightSpace, lineWidth, this.underlineBarHeight);
}
this.y += Number(tempLineHeight) + Number(this.underlineHeightSpace) + Number(this.underlineBarHeight);
}
}
// zoomIn method
zoomIn(timeStamp) {
let lines;
let lineWidth;
let tempLineHeight;
let newTempLine;
let underlineHeight = 0;
let tempFontSize = 0;
let scaleSize = 0;
lines = this.inputText[this.lineIndex];
this.wrapText(lines);
lines = this.#wrappedText;
tempLineHeight = (Number(this.fontSize) + Number(this.lineHeight));
this.x = -(canvas.width / 2) + (this.paddingX / 2);
this.y = 0;//Number(canvas.height/2);
this.y += lines.length * 0.5 * tempLineHeight;
// there is no text justified alignment
if (this.textAlignment != '') {
canvasCTX.textAlign = (this.textAlignment != 'justified') ? this.textAlignment : 'left';
}
for (let i = 0; i < lines.length; i++) {
let textFontStyle = this.fontStyle.replaceAll('underline', '').trim();
// setting font style
canvasCTX.font = `${textFontStyle} ${this.fontSize}px ${this.fontFamily}, san-serif`;
lineWidth = canvasCTX.measureText(lines[i]).width;
newTempLine = lines[i];
// assigning x co-ordinates for right, center and justify text alignment
if (this.textAlignment == 'right') {
this.x = (Number(this.x) + Number(canvas.width) - this.paddingX) / 2;
} else if (this.textAlignment == 'center') {
this.x = 0;//(Number(canvas.width)) / 2;
} else if (this.textAlignment == 'justified') {
// checking if it is an empty line
if (lines[i].trim() != '') {
// getting free space width metrics
let noOfFreeSpaces = (canvas.width - this.paddingX) - lineWidth;
// proceeds only if any whitespace
if (lines[i].indexOf(' ') !== -1) {
// getting number of whitespace in the line
let noOfWhiteSpaces = lines[i].match(/([\s]+)/g).length;
// if it has atleast one whitespace to proceed
if (noOfFreeSpaces > 0) {
// measuring single whitespace width metric for text style
let whiteSpaceWidth = canvasCTX.measureText(' ').width;
// calculating new white space counts
let newWhiteSpaces = Math.ceil((noOfFreeSpaces / noOfWhiteSpaces) / whiteSpaceWidth);
// appending and sharing equal whitespaces to all whitespaces in the line
let whiteSpaces = '';
for (let _i = 0; _i < newWhiteSpaces; _i++) {
whiteSpaces += ' ';
}
newTempLine = lines[i].trim();
newTempLine = newTempLine.replace(/\s/g, whiteSpaces);
}
}
}
}
/* ZoomIn Effect */
this.scaleSize = (Number(timeStamp - this.lastTimeStamp) / Number(this.totalCurrentAnimationDuration));
this.scaleSize += Math.exp(this.scaleSize * (this.scaleSize * 5)) - 1;
canvasCTX.save();
canvasCTX.translate(canvas.width / 2, canvas.height / 2);
// setting font style
canvasCTX.font = `${textFontStyle} ${this.fontSize}px ${this.fontFamily}, san-serif`;
/* Filling Text */
canvasCTX.fillStyle = this.textColor;
canvasCTX.scale(this.scaleSize, this.scaleSize);
canvasCTX.fillText(newTempLine, this.x,this.y);
// applying underline font style
if (this.fontStyle.indexOf('underline') !== -1) {
// default co-ordinates will be followed for left text alignment
let underlineX = this.x;
// measuring width metrics for new processed line text
lineWidth = canvasCTX.measureText(newTempLine).width;
// setting underline color as same as text color
canvasCTX.fillStyle = this.textColor;
// underline x co-ordinate will be differs for right and center alignment
if (this.textAlignment == 'right') {
underlineX = (canvas.width - this.paddingX) - lineWidth;
} else if (this.textAlignment == 'center') {
underlineX = Number(this.x) - Number(lineWidth / 2);
}
// filling rectangular line as an underline below to text
canvasCTX.fillRect(underlineX, Number(this.y) + this.underlineHeightSpace, lineWidth, this.underlineBarHeight);
}
canvasCTX.restore();
this.y += Number(tempLineHeight) + Number(this.underlineHeightSpace) + Number(this.underlineBarHeight);
}
}
// stopAnimation method
stopAnimation () {
// canceling or stopping animation
window.cancelAnimationFrame(this.animationFrameReference);
window.dispatchEvent(animationEndEvent);
this.timer = 0;
this.lastTimeStamp = 0;
this.animationStartTime = 0;
this.animationFrameReference = null;
}
// calcTotalTextHeight method
calcTotalTextHeight() {
// calculating total height that going to occupy by processed text
this.totalTextHeight = Number(this.paddingY);
for (const line of this.#wrappedText) {
this.totalTextHeight += Number(this.fontSize) + Number(this.lineHeight);
}
}
hexToRGB(hex) {
hex = hex.replace('#', '');
// handle for short hex code
if (hex.length === 3) {
hex = hex.split('').map(x => x + x).join('');
}
// validate hex code
if (!/^[0-9A-Fa-f]{6}$/.test(hex)) {
throw new Error('Invalid hex color code');
}
// convert into base 16 integer
const r = parseInt(hex.slice(0,2), 16);
const g = parseInt(hex.slice(2,4), 16);
const b = parseInt(hex.slice(4,6), 16);
return {red: r, green: g, blue: b};
}
// wrapText method
wrapText(line) {
let textFontStyle = this.fontStyle.replaceAll('underline', '').trim();
// assigning font style to calculate total width and height taken in the canvas
canvasCTX.font = `${this.fontSize}px ${this.fontFamily}, san-serif`;
// canvasCTX.font = `${textFontStyle} ${this.fontSize}px ${this.fontFamily}, san-serif`;
// created empty lines array to store new lines
let tempLines = [];
// split by space that consider as words
const words = line.split(' ');
// initialize empty current line array for each iteration
let currentLine = [];
// iterate through each word in the line
for (const word of words) {
// measuring text metrics inside canvas
const metrics = canvasCTX.measureText(currentLine.join(' ') + word + ' ');
// calculating line width taken inclusive of padding in horizontal
const lineWidth = Number(metrics.width) + Number(this.paddingX);
// if lines width is more than the allotted width then it will be a new line
if (lineWidth >= (canvas.width - this.paddingX)) {
tempLines.push(currentLine.join(' '));
currentLine = [];
}
// pushing last line
currentLine.push(word);
}
// pushing all new processed lines into temp lines
tempLines.push(currentLine.join(' '));
// setting into class properties for animation
this.#wrappedText = tempLines;
}
}
// set and get function for localStorage
function setLocalStorage(name, value) {
localStorage.setItem(name, value);
return;
}
function getLocalStorage(name) {
return localStorage.getItem(name);
}
// set function for CanvasSize
function setCanvasSize(width='', height='') {
canvas.width = (width != '') ? width : canvasWidth.value;
canvas.height = (height != '') ? height: canvasHeight.value;
}
// set function for CanvasBackground
function setCanvasBackground(color='') {
if (backgroundColor.value == '#000000' || color == '#000000') {
canvas.style.backgroundColor = (color != '') ? color : backgroundColor.value;
} else {
canvasCTX.fillStyle = (color != '') ? color : backgroundColor.value;
canvasCTX.fillRect(0, 0, canvas.width, canvas.height);
}
}
/* Event Listeners */
// inputText input event listener
inputText.addEventListener('input', function(e) {
setLocalStorage('text-animation.inputText', inputText.value);
});
// canvasWidth input event listener
canvasWidth.addEventListener('input', function(e) {
setLocalStorage('text-animation.canvasWidth', canvasWidth.value);
});
// setDeviceWidthButton click event listener
setDeviceWidthButton.addEventListener('click', function(e) {
canvasWidth.value = window.outerWidth;
});
// canvasHeight input event listener
canvasHeight.addEventListener('input', function(e) {
setLocalStorage('text-animation.canvasHeight', canvasHeight.value);
});
// setDeviceHeightButton click event listener
setDeviceHeightButton.addEventListener('click', function(e) {
canvasHeight.value = window.outerHeight;
});
// transition change event listener
transition.addEventListener('change', function(e) {
setLocalStorage('text-animation.transition', transition.value);
});
// inTransitionDuration input event listener
inTransitionDuration.addEventListener('input', function(e) {
setLocalStorage('text-animation.inTransitionDuration', inTransitionDuration.value);
});
// outTransitionDuration input event listener
outTransitionDuration.addEventListener('input', function(e) {
setLocalStorage('text-animation.outTransitionDuration', outTransitionDuration.value);
});
// animationDuration input event listener
animationDuration.addEventListener('input', function(e) {
setLocalStorage('text-animation.animationDuration', animationDuration.value);
});
// fps input event listener
fps.addEventListener('input', function(e) {
setLocalStorage('text-animation.fps', fps.value);
});
// paddingX input event listener
paddingX.addEventListener('input', function(e) {
setLocalStorage('text-animation.paddingX', paddingX.value);
});
// paddingY input event listener
paddingY.addEventListener('input', function(e) {
setLocalStorage('text-animation.paddingY', paddingY.value);
});
// lineHeight input event listener
lineHeight.addEventListener('input', function(e) {
setLocalStorage('text-animation.lineHeight', lineHeight.value);
});
// fontSize input event listener
fontSize.addEventListener('input', function(e) {
setLocalStorage('text-animation.fontSize', fontSize.value);
});
// textFontFamily input event listener
textFontFamily.addEventListener('input', function(e) {
setLocalStorage('text-animation.textFontFamily', textFontFamily.value);
});
// fontStyle change event listener
fontStyle.addEventListener('change', function(e) {
setLocalStorage('text-animation.fontStyle', fontStyle.value);
});
// textAlignment change event listener
textAlignment.addEventListener('change', function(e) {
setLocalStorage('text-animation.textAlignment', textAlignment.value);
});
// textColor input event listener
textColor.addEventListener('input', function(e) {
setLocalStorage('text-animation.textColor', textColor.value);
});
// backgroundColor input event listener
backgroundColor.addEventListener('input', function(e) {
setLocalStorage('text-animation.backgroundColor', backgroundColor.value);
});
// setFontButton click event listener
setFontButton.addEventListener('click', function() {
let fontFamily = textFontFamily.value.trim();
// Basic validation (optional)
if (!fontFamily) {
return; // Handle empty input
}
// Construct the Google Fonts URL based on user input
const fontUrl = `https://fonts.googleapis.com/css2?family=${fontFamily.replaceAll(' ', '+')}:wght@300;400;700&display=swap`;
const canvasFontLink = document.getElementById('canvasFontLink');
if (canvasFontLink == null || canvasFontLink == undefined) {
// Create a new link element dynamically
const link = document.createElement('link');
link.href = fontUrl;
link.id = 'canvasFontLink';
link.rel = 'stylesheet';
link.media = 'all';
// Inject the link into the head of the document
document.head.appendChild(link);
} else {
canvasFontLink.href = fontUrl;
}
prefObj.fontFamily = fontFamily;
});
// playButton click event listener
playButton.addEventListener('click', function(e) {
let state = playButton.innerText.toLowerCase();
if (state == 'play') {
var animObj = new TextAnimation(inputText.value);
animObj.inTransitionDuration = inTransitionDuration.value;
animObj.outTransitionDuration = outTransitionDuration.value;
animObj.animationDuration = animationDuration.value;
animObj.animate(0);
prefObj.animationObject = animObj;
playButton.innerText = 'Pause';
} else if (state == 'pause') {
prefObj.animationObject.stopAnimation();
playButton.innerText = 'Play';
}
});
// recordButton click event listener
recordButton.addEventListener('click', function(e) {
// getting current state from button text
let state = recordButton.innerText.toLowerCase();
// if it is stopped stage then start capturing and change the button text as stop record
if (state == 'start record') {
// creating and setting stream captureStream with given fps value
prefObj.stream = canvas.captureStream(fps.value);
// creating and setting MediaRecorder with created stream input
prefObj.recorder = new MediaRecorder(prefObj.stream);
// initialize with empty value for recording
prefObj.videoChunks = [];
prefObj.recorderTimes = [];
// setting events to store video chunks
prefObj.recorder.ondataavailable = (e) => prefObj.videoChunks.push(e.data);
// setting events to stop
prefObj.recorder.onstop = () => {
// creating and setting new Blob from chunks with supported video file type
const videoBlob = new Blob(prefObj.videoChunks, { type: 'video/webm' });
// creating and setting objectURL for download
const videoUrl = URL.createObjectURL(videoBlob);
// Create a downloadable link
const link = document.createElement('a');
link.href = videoUrl;
link.download = `canvas_video-T${Date.now()}.webm`;
link.click();