-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsanim.js
More file actions
2063 lines (1993 loc) · 91.6 KB
/
sanim.js
File metadata and controls
2063 lines (1993 loc) · 91.6 KB
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
var Sanim = (function(){
function Animation(world){
//this object has all the animations types as its methods
//the animation object sets the scene to be continually rerendering by creating a javascript animation frame that continually rerenders it
this.speedReference = 0.01;
window.sanimKitSpeedReference = this.speedReference;//setting the speed reference number gloabally;
this.tasks = new Array();
this.world = world;//setting the scene
this.animationOn = false;//property that will be used to put out animation and allow another object to start animation
this.asynchronous = false;//this allows the animations to occur at same time for all the object and not one at a time, then the other
world.animationObjects.push(this)//pushing the animation to the scene so that it can take effect
this.setAsynchronous = function(booleanValue){
//this method sets the asynchronous property to boolean value provided as a scheduled task, hence it will be part of the timeline
var self = this;//a hack to make the object accessible in the function
this.execute(function(){self.asynchronous = booleanValue;});
}
this.sleep = function(time){
//this method sleeps the animation with the animation object for a specified period of time in milliseconds
//this sleep is not windows animation frame based so will not pause while the user is not on tab on the webbrowser
var sleep = new Task({
world:this.world,
status:true,
time:time,
animationStatus: function(){
return this.status;
},
executeOnStartOnly: function(){
var sleeper = this;
window.setTimeout(function(){sleeper.status=false;}, sleeper.time);
}
});
this.schedule(sleep);
return sleep;
}
this.execute = function(func){
//performs an instant animation
var instance = new Task({
world:this.world,
status:true,
execute: function (){
func();
this.status=false;
},
animationStatus: function (){
return status;
}
});
this.schedule(instance)
}
this.setInterval = function(params){
//this method set an interval for a loop
var animInstance = new Task(Object.assign({
isNotSet:true,
delay:100,
status:true,
world:this.world,
loop: function(){
//the method the user needs to define at most are the methods that do the looping which will replace this one
//and the method that checks the status to know when to clear the set interval
},
execute:function(){
var self = this;
if(this.isNotSet){
this.interval = setInterval(() => {
if(self.animationStatus()==false){
clearInterval(self.interval);
}else{
self.loop();
}
}, self.delay);
this.isNotSet = false;
}
},
animationStatus: function(){
return this.status;//this method should not be overwritten, instead a functionality that makes status to be set to false
//should be implemented in the loop method
}
}, params));
this.schedule(animInstance);
return animInstance;
}
this.clearInterval = function(animInstance){
//this method clears the set interval if the user wishe to clear the interval
animInstance.obj.status = false;//and that is all that is needed to clear it, finished
}
this.animateTask = function(instance){
//this method animates a single instance
if(instance.animationStarted==false){
//instance.obj.executeOnStartOnly();
instance.executeOnStartOnly();
}
instance.animationStarted=true;
this.animationOn = true;//toggling the state of the animation object animation check
instance.obj.execute();//executing the function;
if(instance.obj.animationStatus()==false){
//window.cancelAnimationFrame(animationFrame);
this.animationOn=false;
instance.pauseAnimation = true;
instance.animationStarted = false;
var instanceIndex = this.tasks.indexOf(instance);
this.tasks.splice(instanceIndex, 1);//removing the instance from the list to populating the list with unecessary intances
instance.added = false;//putting that the instance is no longer added since it has been removed from the animation object
instance.onEnded();//running something once the instance is finished
}
}
this.animateTasks = function(){
//this method executes a function as long as the animation frame is on
if(this.asynchronous===true || this.tasks.length<=0){
for(var i=0; this.tasks.length>i; i++){
var instance = this.tasks[i];
if(instance.pauseAnimation==false){
this.animateTask(instance);//animating the instance;
if(instance.obj.animationStatus()==false){
break;//breaking the loop since an item in the list of animation instances is being removed, check animateTask method
}
}
}
}else{
var instance = this.tasks[0];
if(instance.pauseAnimation==false){
this.animateTask(instance);
}
}
}
this.schedule = function(task){
if(this.tasks.indexOf(task) < 0){
task.animationObject = this;
task.added = true;
this.tasks.push(task);
}else{
throw('You are attempting to add an already existing animation instance');//making the developer aware of the mistake
}
}
}
function Task(obj){
//this creates an animation instance to allow control of the animation timeline
this.obj = null;//note that this object could as well be an animation instance to
//this.repeatCheck = false;
if(obj != null){
this.obj = obj;
obj.task = this;//setting the animation instance of the object to be this
this.pauseAnimation=false;//setting that animation on that instance is not paused by default
}else{
throw('A JavaScript object must be provided to create an animation instance');
}
this.animationStarted = false;//setting that the animation have not started on that instance by default
this.added = false;
this.onEnded = function(){}//this is what happens when the animation finishes, i.e for this animation instance
this.reset= function(param){
//this resets the animation instance so that the animation could take place again;
this.pauseAnimation = false;
this.animationStarted = false;
if(param){
Object.assign(this.obj, param);
}
}
this.repeat = function(param){//to repeat an animation instance you have to provide the parameters to reset in other to repeat the animation
/*
What we need to do is reset the parameters of the instance and then add it to the list of instances, but the instance and it's obj property is mutable
so we need to make a copy of the instance and it's obj property so that when we reset the new instance it would not affect the one that is already running
*/
var newInstance = new Object();
Object.assign(newInstance, this);
var newObj = new Object();
Object.assign(newObj, this.obj);
newInstance.obj=newObj;
newInstance.reset(param);
this.animationObject.schedule(newInstance);
}
this.executeOnStartOnly = function(){
//this gets done the momement the animation instance starts
this.obj.executeOnStartOnly();
}
if(this.obj.executeOnStartOnly==undefined){
this.obj.executeOnStartOnly = function(){};//empty function
}
if(this.obj.execute==undefined){
this.obj.execute = function(){};//empty function
}
if(this.obj.animationStatus==undefined){
this.obj.animationStatus = function(){return true};
}
}
function SanimObject(){
this.path2DObject = new Path2D();//this is the path2d object created for the path
this.lineWidth = 0.0000000000000001;//setting the lineWidth to a very low value so that the lines does not show by default
this.children = []; //list of children embedded in the object
this.props = new Object();
this.parentObject = null;
this.animationStarted = false;
this.pauseAnimation = false;//this property can be used to pause all animation on an object
this.translationMatrix = [0,0];
this.initialTransformationMatrix = [0, 0];//setting the initial transformation matrix
this.skewMatrix = [0,0];
this.scaleMatrix = [1, 1];
this.transformationMatrix = [
1, 0, 0,
1, 0, 0
];
this.rotationAngle = 0;
this.transformationOrigin = [0, 0];//this x and y rendering origin of the object
this.renderingTransformationOrigin = this.transformationOrigin;
this.transformWithParent = true;//setting that object always transform with it's parent object if any
this.translateWithParent = true; //doing similar thing above for translation
this.skewWithParent = true;//for skewing
this.rotateWithParent = true;//for rotation
this.scaleWithParent = true;//for scaling
this.noTransformationWithParent = false;//with this set to true no parent transformation is ever inherited
this.noAncestorTransformation = false;//with this set to true no parent or ancestor transformation is ever inherited
this.isInPath = function(x, y){
//it uses the path2dobject of the object to know if it was pointed to;
var status = this.world.context.isPointInPath(this.path2DObject, x, y);
return status;
}
this.setWorld = function(world){
//this sets the world of the object
this.world = world;
}
this.addEvent = function (eventType, eventResponseIfOnObject, eventResponseIfNotOnObject=function(){}){
//this function is going to stand for as the event listeener function that is going to be doing the work of the normal event listener that
//DOM elements listens to
var obj = this;// trying to reassign the object to allow the eventlistener function to be able to see it as it is
this.world.context.canvas.addEventListener(eventType, function(e){
e.canvasTargetObject = obj;//setting the object to be the canvas target so that it can be refered to in the event response function
if(obj.isInPath(e.clientX, e.clientY)){
eventResponseIfOnObject(e);//fire the event response if the event occurred on the object
}else{
eventResponseIfNotOnObject(e);//fire the event response if the event did not occur on the object
}
});
}
this.addChild = function(obj){
//this method adds a child object to an object
if(!this.world){
//this will throw an error as there is no world related to the parent object
throw("can not add child object to parent as the parent have not been added to a world(scene)");
}else{
obj.x = obj.initialX + this.x;//setting the origin of the child object to be with respect to the origin of the parent object
obj.y = obj.initialY + this.y;//we are using the initial settings because it is tamper proof;
this.children.push(obj);//adding the child object to the list of children to the parent object
this.world.addObject(obj);//also adding it to the list of objects in the scene
obj.parentObject = this;//setting the parent object of the added object to this object
}
}
this.removeChild = function(obj){
//this method removes a child object from the parent
var obj_index = this.children.indexOf(obj);//the index in the parent object children list
var scene_index = this.world.objects.indexOf(obj);//the index in the in the scene's objects list
if(0 <= obj_index){
obj.flushChildren//flushing the children in the object
this.children.splice(obj_index, 1);//this removes the object from the list of children but does not change other setttings of the object
this.world.removeObject(obj);// removing it from the scene too
obj.parentObject = null;//setting the parent object property of the object being removed to null
}//this does not throw an error if such object does not exist as child to the parent object
}
this.flushChildren = function(){
//this method flushes the children attached to this object
var children = new Array(...this.children); //de-structuring the children
var self = this;//safety measures
children.forEach((child) => {
self.removeChild(child);//this removes the child
});
}
this.renderChildren =function(){
this.children.forEach((obj) => {
obj.render();
})
}
this.implementAfterAddedToScene = function(){
return null;//doing nothing at this point
}
this.alignCenter = function(origin=false){
/*
This method places an object at the center of the parent.
if origin is set to true, then the objects starting x is what is placed at the center and not the object's center.
Note: for an object whose parent has no width, nothing will be done. And for an object with no width, it's center is
the starting origin, so setting origin to false will make no difference
*/
this.setCanvasPropsForObject();//making sure that all the canvas properties for object are set to normal
if(this.measureText){//measuring the width and height of text objects before applying
this.measureText();
}
if(this.parentObject){//checking if the object has a parent object
if(this.parentObject.width){//checking if the parent object has a width
if(this.width && origin == false){//checking if the object has a width and if it is the object's center that is meant to be aligned to the parent's middle
this.initialX = (this.parentObject.width - this.width)/2;//aligning the object's center to the parent's center
}else{
this.initialX = this.parentObject.width/2;//aligning the object origin to parent center
}
}
}else{
if(this.width && origin == false){
this.initialX = (this.world.context.canvas.width - this.width)/2;//aligning the object's center to the world's center
}else{
this.initialX = this.world.context.canvas.width/2;//aligning the object's origin to world's center
}
this.x = this.initialX;//making sure that the x value has same value as the initialX
}
}
this.alignMiddle = function(origin=false){
/*
This method places an object at the middle of the parent.
if origin is set to true, then the objects starting y is what is placed at the middle and not the object's middle.
Note: for an object whose parent has no height, nothing will be done. And for an object with no height, it's middle is
the starting origin, so setting origin to false will make no difference
*/
this.setCanvasPropsForObject();
if(this.measureText){
this.measureText();
}
if(this.parentObject){//checking if the object has a parent object
if(this.parentObject.height){//checking if the parent object has a height
if(this.height && origin == false){//checking if the object has a height and if it is the object's middle that is meant to be aligned to the parent's middle
this.initialY = (this.parentObject.height - this.height)/2;//aligning the object's middle to the parent's middle
}else{
this.initialY = this.parentObject.height/2;//aligning the object origin to the parent's middle
}
}
}else{
if(this.height && origin == false){
this.initialY = (this.world.context.canvas.height - this.height)/2;//aligning the object's middle to the world's middle
}else{
this.initialY = this.world.context.canvas.height/2;//aligning the object origin to world's middle
}
this.y = this.initialY;//making sure that the y value has same value as the initialY
}
}
this.hide = function(children = false){
//this method hides an object in the canvas
//setting the children to true, will make it to also hide the children
this.props.globalAlpha = 0;//setting the global alpha to zero make it invisible
if(children == true){
for(var i = 0; i<this.children.length; i++){
//here now I am trying to hide the children as well
this.children[i].hide(true);
}
}
}
this.show = function(children = false){
//this method shows the object in the canvas
//setting the children to true, will make it to also show the children
this.props.globalAlpha = 1;
if(children == true){
for(var i = 0; i<this.children.length; i++){
//here now I am trying to showing the children as well
this.children[i].show(true);
}
}
}
this.fadeOut = function(anim, time, children = false){
//this method takes an animation object as parameter
//setting the children to true will make it to also fadeOut the children
if(children == true){
var initialAsynchronousState = anim.asynchronous;//holding the inital state of the asynchronous property of anim
anim.execute(function(){anim.asynchronous = true});//setting it to be asynchronous so that the task will happen synchronously
for(var i = 0; i<this.children.length; i++){
//here now I am trying to showing the children as well
this.children[i].fadeOut(anim, time, true);
if(i==this.children.length - 1){//checking if this is the last depth of the iteration
anim.execute(function(){anim.asynchronous = initialAsynchronousState});//setting the asynchronous to initial value
}
}
}
return anim.setInterval({
obj:this,
delay:time/10,
loop: function(){
if(this.obj.props.globalAlpha === undefined){
this.obj.props.globalAlpha = this.obj.world.canvasContextProperties.globalAlpha;
}
if(this.obj.props.globalAlpha <= 0.1){
this.status = false;
this.obj.props.globalAlpha = 0;
}else{
this.obj.props.globalAlpha -= 0.1;
}
}
});
}
this.fadeIn = function(anim, time, children){
//this method takes an animation object as parameter
//setting the children to true will make it to also fadeIn the children
if(children == true){
var initialAsynchronousState = anim.asynchronous;//holding the inital state of the asynchronous property of anim
anim.execute(function(){anim.asynchronous = true});//setting it to be asynchronous so that the task will happen synchronously
for(var i = 0; i<this.children.length; i++){
//here now I am trying to showing the children as well
this.children[i].fadeIn(anim, time, true);
if(i==this.children.length - 1){//checking if this is the last depth of the iteration
anim.execute(function(){anim.asynchronous = initialAsynchronousState});//setting the asynchronous to initial value
}
}
}
return anim.setInterval({
obj:this,
delay:time/10,
loop: function(){
if(this.obj.props.globalAlpha === undefined){//checking if the global alpha property exist
this.obj.props.globalAlpha = this.obj.world.canvasContextProperties.globalAlpha;//assigning the default value;
}
if(this.obj.props.globalAlpha >= 0.9){
this.status = false;
this.obj.props.globalAlpha = 1;
}else{
this.obj.props.globalAlpha += 0.1;
}
}
});
}
this.grow = function(anim, scaleAmount, time){
var scaleFactor = 1.1;
/*
the scale amount is the amount in which the object should be grown to
time is the time to spent while it is attempting to grow
let F = scaling factor each time int the loop
F^x = scaleAmount
hence it means that we will need to call the scale method with the scaling factor x times before it reaches the amount wanted
mathematically x is given by:
ln(scaleAmount)/ln(F) where ln = natural logarithm of numbers
the delay time like as done with fade in and fade out, is giveb by:
time/n
*/
if(scaleAmount < 1){
scaleFactor = 0.90;
}
var n = Math.floor(Math.log(scaleAmount)/Math.log(scaleFactor));
return anim.setInterval({
n:Math.abs(n),
objectInitialScale: [this.scaleMatrix[0], this.scaleMatrix[1]],
scaleAmount:scaleAmount,
delay:time/Math.abs(n),
obj:this,
loop:function(){
if(this.n <= 0){
this.status = false;
this.obj.scaleMatrix = [this.objectInitialScale[0]*this.scaleAmount, this.objectInitialScale[1]*this.scaleAmount];
}else{
this.obj.scale(scaleFactor, scaleFactor);
}
this.n--;//decreasing n, to know when the multiplication power has been reached
}
})
}
this.applyTransformation = function(child={
transformWithParent:true, scaleWithParent:true, translateWithParent:true, rotateWithParent:true,
skewWithParent:true, noTransformationWithParent:false, noAncestorTransformation:false
}){//setting default value as a hack not to make the code longer than neccessary
//this method applies the transformation properties to the canvas
// it is very important to note that transformations transcends to the descendants of of an object by default. that is to it's children objects
var zoom = 1;//default zoom without camera effect
if(this.world.camera){//checking if to apply camera effect
zoom = this.world.camera.zoom;//to be used with translation option
}
this.world.context.save()//saving the state of the canvas
if((this.parentObject!=null || this.parentObject!=undefined) && child.noAncestorTransformation==false){
this.parentObject.applyTransformation(this);//applying parents transformation if parent exist
}else{//We are not saving the canvas state if the object has a parent because we don't want to overwrite the transformation done by parent
}
this.world.context.translate(this.renderingTransformationOrigin[0], this.renderingTransformationOrigin[1]);//changing transformation orgin to the specified transformation origin of the object
if(child.translateWithParent==true && child.noTransformationWithParent==false){
this.world.context.translate(this.translationMatrix[0]*zoom, this.translationMatrix[1]*zoom);
}
if(child.transformWithParent==true && child.noTransformationWithParent==false){
this.world.context.transform(...this.transformationMatrix);//applying the transformation
}
if(child.skewWithParent==true && child.noTransformationWithParent==false){
this.world.context.transform(1, ...this.skewMatrix, 1, 0, 0);//transformation for skewing
}
if(child.rotateWithParent && child.noTransformationWithParent==false){
this.world.context.rotate(this.rotationAngle);//applying the rotation
}
if(child.scaleWithParent && child.noTransformationWithParent==false){
this.world.context.scale(this.scaleMatrix[0], this.scaleMatrix[1]);//applying the scaling transformation
}
this.world.context.translate(-this.renderingTransformationOrigin[0], -this.renderingTransformationOrigin[1]);//translating back to the origin
}
this.removeTransformation = function(){
//this method removes the transformation that has been applied on the canvas so that other objects in the canvas will not be affected
this.world.context.restore();//restoring the state of the canvas
}
this.motionPath = function(func){
//this method moves the object in the canvas through a path using the Function object as a template for the motion path
var mover = {
obj:this,
employ: function (){
//this method employs the data from the Function and uses it to perform some functions
this.obj.x = this.xOrigin + this.x*this.xScale;
this.obj.y = this.yOrigin + this.y*this.yScale;
}
}
Object.assign(mover, func);//assigning the Function Expression
mover.xOrigin = this.x;//changing the origin to the origin of the object
mover.yOrigin = this.y;
return mover;
}
this.scale = function(x, y){
//the function execution here for scaling
//find out the mathematics behind the implementation of this and have it implemented
this.scaleMatrix[0] *= x;
this.scaleMatrix[1] *= y;
}
this.rotate = function(angle){
//the function execution comes here for rotation using the rotation angle property of the object
this.rotationAngle += angle;
}
this.translate = function(x, y){
//the function execution comes here for translation
//find out the mathematics behind the implementation of this and have it implemented
this.translationMatrix[0] += x;
this.translationMatrix[1] += y;
}
this.skew= function(angleX, angleY){
//this skews the object, using the skew matrix property of the object which provides the skew x and y
this.skewMatrix[0] += angleX;
this.skewMatrix[1] += angleY;
}
this.transform = function(transformationMatrix){
//the function exectution comes here for transformation using the transformation property
//find out the mathematics behind the implementation of this
if(this.transformationMatrix.length == transformationMatrix.length){
this.transformationMatrix[0] *= transformationMatrix[0];
this.transformationMatrix[1] += transformationMatrix[1];
this.transformationMatrix[2] += transformationMatrix[2];
this.transformationMatrix[3] *= transformationMatrix[3];
this.transformationMatrix[4] += transformationMatrix[4];
this.transformationMatrix[5] += transformationMatrix[5];
/*
the 2D transformation matrix is of this form: | a c | | x |
| | | |
| b d | | y |
where the transformationParameters that should be provided is of the form [a, b, c, d, translationX, translationY]
*/
}else{
throw('the transformation matrix provided is not in the correct dimmension or length');
}
}
this.drawPath = function(){
//this method draws a path round th object for event listening purposes
//The below lines is trying to create a path for the rect object so as to be able to trace when they is a point in the path and alternative way that it can be done is make a check to know if when the point is within the area of the rect object
//---------------------using the path2d object---------------------
delete this.path2DObject;//deleting the previous one so it does not stack the existing one to it
this.path2DObject = new Path2D();//creating new one
this.path2DObject.moveTo(this.renderingX, this.renderingY);
this.path2DObject.lineTo(this.renderingX, this.renderingY+this.renderingHeight);
this.path2DObject.lineTo(this.renderingX+this.renderingWidth, this.renderingY+this.renderingHeight);
this.path2DObject.lineTo(this.renderingX+this.renderingWidth, this.renderingY);
this.path2DObject.closePath();
this.world.context.stroke(this.path2DObject);
}
this.setCanvasPropsForObject = function(){
//this method sets the canvas props to the objects rendering properties
Object.assign(this.world.context, this.world.canvasContextProperties);//making sure that the setting are reset to what it was originally
Object.assign(this.world.context, this.props);
}
this.alignOriginWithParent = function(){
//this method sets the rendering origin parameter of the object to align with the parent object
if(this.parentObject){
//checking if the object has parent, so we could readjust to fit to the parent object
/*
NOTE: Once the object is added to a parent we can no longer use the x and y properties of the object to
adjust its position. But we can use the initialX and initialY properties. hence it is safer to use the
initialX and initialY properties to make adjustment to object positions if we want them to reflect when added
to a parent object.
*/
this.x = this.initialX + this.parentObject.x;
this.y = this.initialY + this.parentObject.y;
}
}
this.applyTransformationOrigin = function(){
//this method applies the transformation origin ready for rendering
this.renderingTransformationOrigin = [this.transformationOrigin[0], this.transformationOrigin[1]];
if(this.world.camera){
this.renderingTransformationOrigin[0]=(this.renderingTransformationOrigin[0]*this.world.camera.zoom);
this.renderingTransformationOrigin[1]=(this.renderingTransformationOrigin[1]*this.world.camera.zoom);
}
if(this.setTransformationOriginToCenter === true ){
//trying to make the transformation take place at the center of the object
this.renderingTransformationOrigin = [0.5*this.renderingWidth, 0.5*this.renderingHeight];
}
this.renderingTransformationOrigin = [this.renderingX+this.renderingTransformationOrigin[0], this.renderingY+this.renderingTransformationOrigin[1]];
}
this.setRenderingParameters = function(){
//this method sets the rendering paramters for the object
if(this.world.camera){
if(this.props.lineWidth){
this.world.context.lineWidth = this.world.context.lineWidth*this.world.camera.zoom;
}
this.renderingX = (this.x*this.world.camera.zoom)-this.world.camera.x, this.renderingY = (this.y*this.world.camera.zoom)- this.world.camera.y;//this is defines the starting poisiton of the path to be drawn
this.renderingWidth = this.width*this.world.camera.zoom, this.renderingHeight = this.height*this.world.camera.zoom; //this tries to apply the camera effect if the camera is added to the scene, so we are dividing by the world camera's zoom
}else{
this.renderingX = this.x, this.renderingY = this.y;
this.renderingWidth = this.width, this.renderingHeight = this.height;
}
}
}
function Player(obj){
//this constructor function takes care of all the operations that has to do with having a player on the scene;
this.object = obj;//this sets the object that is going to be the controller in the scene instance.
obj.playerObject = obj;//setting a playerObject for the object that is now made player
if(obj.width == undefined || obj.height == undefined){//checking to know if the object has a width and a height
obj.width = 0, obj.height = 0;
//the developer has to set a width and height parameter to make the object a player
}
this.minOffsetX = 0; //the minimum offset of the player from the edge of the viewport on x-axis
this.minOffsetY = 0; //the minimum offset of the player from the edge of the viewport on y-axis
this.maxOffsetX = 0; //the maximum offset of the player from the edge of the viewport on x-axis
this.maxOffsetY = 0; //the maximum offset of the player from the edge of the viewport on y-axis
this.coupleToCamera = true;//this states if the player should be coupled with camera or not, if true the camera will be moving with the player
this.makeCameraAdjustments = function(){
//this method makes all the neccessary adjustments that need to be made to camera with respect to the players position at that time.
if(this.world && this.coupleToCamera === true && this.world.camera != null){
//checking to be sure that the player has been attached to a scene and that the coupleToCamera setting has been set to true before performing neccessary computations
var Xp = this.world.camera.x;//camera position on the x-axis
var Yp = this.world.camera.y;//camera position on the y-axis
var Vw = this.world.context.canvas.width;// the width of the viewport
var Vh = this.world.context.canvas.height;// the height of the viewport
var Px = this.object.x*this.world.camera.zoom;//player origin position on x-axis
var Py = this.object.y*this.world.camera.zoom;//player origin position on y-axis
var Ph = this.object.height*this.world.camera.zoom;//player height
var Pw = this.object.width*this.world.camera.zoom;//player width
//the reason for dividing by the camera's zoom is to make sure that the values that will be using for the calculation is with respect to the camera's view
var ofX = this.minOffsetX;//player min offset from viewport on x-axis
var ofY = this.minOffsetY;//player min offset from viewport on y-axis
if(Xp >= (Px - ofX)){
//checking to know if a part of the player is outside the viewport of the camera by the left hand side.
this.world.camera.x = Px - ofX//adjusting the camera position on the x-axis
}
if(Yp >= (Py - ofY)){
//checking to know if a part of the player is outside the viewport of the camera by the top side.
this.world.camera.y = Py - ofY//adjusting the camera position on the y-axis
}
if((Yp + Vh) <= (Py + ofY + Ph)){
//checking to know if a part of the player is outside the viewport of the camera by the bottom side.
this.world.camera.y = (Py + ofY + Ph)-Vh //adjusting the camera position on the y-axis
}
if((Xp + Vw) <= (Px + ofX + Pw)){
//checking to know if a part of the player is outside the viewport of the camera by the bottom side.
this.world.camera.x = (Px + ofX + Pw)-Vw //adjusting the camera position on the x-axis
}
}else{
//if no scene then the programs below will take place
}
//Note that this camera coupling property does not apply when transformation is done on the object using the canvas methods. Currently
//the transformations availble on objects are based on cavas methods
}
this.implementAfterAddedToScene = function(){
//the developer has to put things he wants to implement when the player gets added to the scene here
}
this.render = function(){
//this renders the player to the scene
this.makeCameraAdjustments();//making camera adjustments
this.world.render(); //re-rendering the scene after adjustments
}
}
function TextObject(text, x, y, fillText= false){
//this is a text object
//note that at this point camera features does not affect the text properties like width and height
SanimObject.call(this);//referencing the sanim object
this.x = x, this.y = y, this.fillText = fillText;
this.fillBox = false;
this.width = 220;
this.height = 10;
this.renderingWidth = this.width;
this.renderingHeight = this.height;
this.text = text;
this.textMeasurement = null;
this.boxProps = {paddingX:0, paddingY:0};
this.initialX = this.x, this.initialY = this.y;//tamper proofing so as to get back to initial value if object is added and removed from another
this.setTransformationOriginToCenter=true;
this.fontSize=10;
this.renderingFontSize = this.fontSize;
this.render = function(){
//first is to set canvas properties for the object
this.setCanvasPropsForObject();
if(this.props.font){
//trying the extract the width property of the text from the font information that was given
this.fontSize = this.props.font.substring(this.props.font.search(/[0-9]+px/), this.props.font.search(/[0-9]px/)+1);// this finds extracts the fontSize of the text from the text
}
this.measureFont();
this.measureText();
this.alignOriginWithParent();
this.setRenderingParameters();
Object.assign(this.world.context, this.world.canvasContextProperties);//reseting the change so that the changes for the text box itself could be applied
Object.assign(this.world.context, this.boxProps);//assigning the textbox properties
if(this.world.camera && this.boxProps.lineWidth){
this.world.context.lineWidth = this.world.context.lineWidth*this.world.camera.zoom;
}
this.applyTransformationOrigin();
this.applyTransformation();//applyiing transformation properties
//The below lines is trying to create a path for the rect object so as to be able to trace when they is a point in the path and alternative way that it can be done is make a check to know if when the point is within the area of the rect object
this.drawPath();//drawing the path for the path2DObject
if(this.fillBox==true){
this.world.context.fill(this.path2DObject);
}
this.setCanvasPropsForObject();//reseting again to apply text properties
this.measureFont();
this.renderText();
this.removeTransformation();//removing setted transformation properties so it does not affect the next object
}
this.measureText = function(){
//-----taking care of measuring the dimension of the text
this.textMeasurement = this.world.context.measureText(this.text);
this.width = this.textMeasurement.width;
this.height = this.renderingFontSize*(36/50);
}
this.measureFont = function(){
//takes care of measuring the font of the text
if(this.world.camera){
this.renderingFontSize = this.fontSize*this.world.camera.zoom;
}else{
this.renderingFontSize = this.fontSize;
}
this.world.context.font = this.world.context.font.replace(String(this.fontSize), String(this.renderingFontSize));
if(this.world.camera && this.props.lineWidth){
this.world.context.lineWidth = this.world.context.lineWidth*this.world.camera.zoom;
}
}
this.write = function(anim, time, sound=false){
//this method write the text word by word
return anim.setInterval({
text:this.text,
position:0,//positon of text splice at each time
obj:this,
sound:sound,
delay:time/this.text.length,//the interval of time
loop:function(){
//this is the execution
this.obj.props.globalAlpha = 1;
this.obj.text = this.text.slice(0, this.position);//assigning the spliced text to the text object
if(this.sound){//checking if they is a sound to play
if(this.sound.media.paused == true){
this.sound.media.play();//playing the sound if the sound is paused
}
}
if(this.position >= this.text.length){
this.status = false;//ending the interval
if(this.sound){
this.sound.media.pause();//pausing the sound if the sound is still playing
}
}
this.position++;
}
});
}
this.superscript = function(text, ratio){
//this method creates a text object that positions itself like a subscript to the current text object.
this.render();
var obj = new TextObject(text, this.width, 2);//putting 2 in the y position to account for canvas offset on text
obj.fontSize = parseInt(this.fontSize*ratio);//setting the fontsize to be a fraction of the ration and converting to integer
Object.assign(obj.props, this.props);//making it's property equal to the parent object
obj.props.font = this.props.font.replace(String(this.fontSize), String(obj.fontSize));//changing the actual font property
obj.fillText = this.fillText;//setting the fillText opition to equal the fillText of the parent;
this.addChild(obj);//adding it to the parent
return obj;
}
this.subscript = function(text, ratio){
//this method creates a text object that positions itself like a subscript to the current text object.
var obj = this.superscript(text, ratio);
obj.render();//rendering object so as to use properties calculated during rendering to make adjustments to position
obj.initialY = this.height-obj.height+2;//making adjustment to the position
obj.y = obj.initialY;
return obj;
}
this.renderText = function(){
//this method renders the actual text to the canvas
var paddingX = 0; var paddingY = 0;
if(this.world.camera && (this.boxProps.paddingX>0 || this.boxProps.paddingY>0)){
paddingX = this.boxProps.paddingX*this.world.camera.zoom;
paddingY = this.boxProps.paddingY*this.world.camera.zoom;
}else if(this.boxProps.paddingX>0 || this.boxProps.paddingY>0){
paddingX = this.boxProps.paddingX;
paddingY = this.boxProps.paddingY;
}
if(this.fillText==true){
this.world.context.fillText(this.text, this.renderingX+paddingX, this.renderingY+this.renderingHeight-paddingY);
}
this.world.context.strokeText(this.text, this.renderingX+paddingX, this.renderingY+this.renderingHeight-paddingY);
}
this.setRenderingParameters = function(){
//setting the rendering parameters for the text object different from the way others where set
if(this.world.camera){
this.renderingX = (this.x*this.world.camera.zoom)-this.world.camera.x, this.renderingY = (this.y*this.world.camera.zoom)-this.world.camera.y;//this is defines the starting poisiton of the path to be drawn
this.renderingWidth = (this.width+this.boxProps.paddingX*2)*this.world.camera.zoom, this.renderingHeight = (this.height+this.boxProps.paddingY*2)*this.world.camera.zoom; //this tries to apply the camera effect if the camera is added to the scene, so we are dividing by the world camera's zoom
}else{
this.renderingX = this.x, this.renderingY = this.y;
this.renderingWidth = this.width + this.boxProps.paddingX*2, this.renderingHeight = this.height+this.boxProps.paddingY*2;
}
}
}
function RectObject(x, y, width, height, fillRect = false){
//this constructor function is for the rectangle object, this takes care of the operations that has to do with the objects in the canvas
SanimObject.call(this);
this.x = x, this.y = y, this.width = width, this.height = height, this.fillRect = fillRect;
this.initialX = this.x, this.initialY = this.y;//tamper proofing so as to get back to initial value if object is added and removed from another
this.renderingX = this.x, this.renderingY = this.y, this.renderingWidth = this.width, this.renderingHeight= this.height;
this.setTransformationOriginToCenter=true;
this.render = function(){
//this renders the object to the canvas
//first is to set canvas properties for the object
this.setCanvasPropsForObject();
//second is to align the objects origin with parent
this.alignOriginWithParent();
//next is to set the rendering parameters
this.setRenderingParameters();
//next is to apply the setted transformation origin for the object
this.applyTransformationOrigin();
//next is to apply transformation on the object before rendering
this.applyTransformation();
//next is to draw a path round the object for event listening purpose
this.drawPath();
if(this.fillRect==true){
this.world.context.fill(this.path2DObject);
}
//finally we remove the transformation on the object
this.removeTransformation();//removing setted transformation properties so it does not affect the next object
}
}
function PathObject(x, y, paths, closePath=true, fillPath=false){
/*
This is the definition for a canvas path object.
This object gives more functionality to the canvas path, it literally make the path an object instead of just a path.
*/
this.x = x; this.y = y; this.fillPath = fillPath; this.closePath = closePath;
this.initialX = this.x; this.initialY = this.y;//tamper proofing so as to get back to initial value if object is added and removed from another
this.newPath = true;//stating that the object js a new path and not the continuation of another.
this.paths = paths;
this.renderingX = this.x, this.renderingY = this.y;
this.renderingPaths = new Array();
for(var i = 0; i<paths.length; i++){//trying to assign the paths to renderingPaths;
var path = {pathMethod:paths[i].pathMethod, params:new Array()};
for(var j=0; j<paths[i].params.length; j++){
path.params.push(paths[i].params[j]);
}
this.renderingPaths.push(path);
}
this.initialPaths = new Array();
for(var i = 0; i<paths.length; i++){//trying to assign the paths to initialPaths
var path = {pathMethod:paths[i].pathMethod, params:new Array()};
for(var j=0; j<paths[i].params.length; j++){
path.params.push(paths[i].params[j]);
}
this.initialPaths.push(path);
}
this.appendPath = function(path){
//this method allows the developer to be able to append path to the already existing path
var newPath = {pathMethod:path.pathMethod, params:new Array()};
for(var j=0; j<path.params.length; j++){
newPath.params.push(path.params[j]);
}
this.renderingPaths.push(newPath);
newPath = {pathMethod:path.pathMethod, params:new Array()};//reseting it to append to the initialPaths
for(var j=0; j<path.params.length; j++){
newPath.params.push(path.params[j]);
}
this.initialPaths.push(newPath);
this.paths.push(path);
}
this.render = function(){
this.setCanvasPropsForObject();
this.alignOriginWithParent();
if(this.world.camera){
this.renderingX = (this.x*this.world.camera.zoom)-this.world.camera.x, this.renderingY = (this.y*this.world.camera.zoom)-this.world.camera.y;//this is defines the starting poisiton of the path to be drawn
}else{
this.renderingX = this.x, this.renderingY = this.y;
}
//remove the rest of the general properties that where not used here
this.applyTransformationOrigin();
this.applyTransformation();
// if(this.newPath == true){
// this.world.context.beginPath();
// }
delete this.path2DObject;//deleting the existing path2d object
this.path2DObject = new Path2D();//creating a new one
for(var i = 0;i<this.paths.length;i++){
/* Trying to add the origin to the neccessary parameters of the path methods */
if(this.paths[i].pathMethod == 'lineTo' || this.paths[i].pathMethod == 'moveTo'){
this.paths[i].params[0] = this.initialPaths[i].params[0] + this.x;
this.paths[i].params[1] = this.initialPaths[i].params[1] + this.y;
}else if(this.paths[i].pathMethod == 'arcTo'){
this.paths[i].params[0] = this.initialPaths[i].params[0] + this.x;
this.paths[i].params[1] = this.initialPaths[i].params[1] + this.y;
this.paths[i].params[2] = this.initialPaths[i].params[2] + this.x;
this.paths[i].params[3] = this.initialPaths[i].params[3] + this.y;
}else if(this.paths[i].pathMethod == 'bezierCurveTo'){
this.paths[i].params[0] = this.initialPaths[i].params[0] + this.x;
this.paths[i].params[1] = this.initialPaths[i].params[1] + this.y;
this.paths[i].params[2] = this.initialPaths[i].params[2] + this.x;
this.paths[i].params[3] = this.initialPaths[i].params[3] + this.y;
this.paths[i].params[4] = this.initialPaths[i].params[4] + this.x;
this.paths[i].params[5] = this.initialPaths[i].params[5] + this.y;
}else if(this.paths[i].pathMethod == 'quadraticCurveTo'){
this.paths[i].params[0] = this.initialPaths[i].params[0] + this.x;
this.paths[i].params[1] = this.initialPaths[i].params[1] + this.y;
this.paths[i].params[2] = this.initialPaths[i].params[2] + this.x;
this.paths[i].params[3] = this.initialPaths[i].params[3] + this.y;
}else if(this.paths[i].pathMethod == 'arc'){
this.paths[i].params[0] = this.initialPaths[i].params[0] + this.x;
this.paths[i].params[1] = this.initialPaths[i].params[1] + this.y;
}
if(this.world.camera){
if(this.paths[i].pathMethod == 'lineTo' || this.paths[i].pathMethod == 'moveTo'){
this.renderingPaths[i].params[0] = (this.paths[i].params[0]*this.world.camera.zoom)-this.world.camera.x;
this.renderingPaths[i].params[1] = (this.paths[i].params[1]*this.world.camera.zoom)-this.world.camera.y;
}else if(this.paths[i].pathMethod == 'arcTo'){
this.renderingPaths[i].params[0] = (this.paths[i].params[0]*this.world.camera.zoom) -this.world.camera.x;
this.renderingPaths[i].params[1] = (this.paths[i].params[1]*this.world.camera.zoom) -this.world.camera.y;
this.renderingPaths[i].params[2] = (this.paths[i].params[2]*this.world.camera.zoom) -this.world.camera.x;
this.renderingPaths[i].params[3] = (this.paths[i].params[3]*this.world.camera.zoom) -this.world.camera.y;
this.renderingPaths[i].params[4] = (this.paths[i].params[4]*this.world.camera.zoom);
}else if(this.paths[i].pathMethod == 'bezierCurveTo'){
this.renderingPaths[i].params[0] = (this.paths[i].params[0]*this.world.camera.zoom) -this.world.camera.x;
this.renderingPaths[i].params[1] = (this.paths[i].params[1]*this.world.camera.zoom) -this.world.camera.y;
this.renderingPaths[i].params[2] = (this.paths[i].params[2]*this.world.camera.zoom) -this.world.camera.x;
this.renderingPaths[i].params[3] = (this.paths[i].params[3]*this.world.camera.zoom) -this.world.camera.y;
this.renderingPaths[i].params[4] = (this.paths[i].params[4]*this.world.camera.zoom) -this.world.camera.x;
this.renderingPaths[i].params[5] = (this.paths[i].params[5]*this.world.camera.zoom) -this.world.camera.y;
}else if(this.paths[i].pathMethod == 'quadraticCurveTo'){
this.renderingPaths[i].params[0] = (this.paths[i].params[0]*this.world.camera.zoom) -this.world.camera.x;
this.renderingPaths[i].params[1] = (this.paths[i].params[1]*this.world.camera.zoom) -this.world.camera.y;
this.renderingPaths[i].params[2] = (this.paths[i].params[2]*this.world.camera.zoom) -this.world.camera.x;
this.renderingPaths[i].params[3] = (this.paths[i].params[3]*this.world.camera.zoom) -this.world.camera.y;
}else if(this.paths[i].pathMethod == 'arc'){
this.renderingPaths[i].params[0] = (this.paths[i].params[0]*this.world.camera.zoom) -this.world.camera.x;
this.renderingPaths[i].params[1] = (this.paths[i].params[1]*this.world.camera.zoom) -this.world.camera.y;
this.renderingPaths[i].params[2] = (this.paths[i].params[2]*this.world.camera.zoom);
}
}else{
this.renderingPaths[i].params = new Array(...this.paths[i].params);
}
if(this.paths[i].pathMethod == 'moveTo'){
this.path2DObject.moveTo(...this.renderingPaths[i].params);
}else if(this.paths[i].pathMethod == 'lineTo'){
this.path2DObject.lineTo(...this.renderingPaths[i].params);
}else if(this.paths[i].pathMethod == 'arcTo'){
this.path2DObject.arcTo(...this.renderingPaths[i].params);
}else if(this.paths[i].pathMethod == 'bezierCurveTo'){
this.path2DObject.bezierCurveTo(...this.renderingPaths[i].params);
}else if(this.paths[i].pathMethod == 'quadraticCurveTo'){
this.path2DObject.quadraticCurveTo(...this.renderingPaths[i].params);
}else if(this.paths[i].pathMethod == 'arc'){
this.path2DObject.arc(...this.renderingPaths[i].params);
}
}
if(this.closePath==true){
this.path2DObject.closePath();
}
this.world.context.stroke(this.path2DObject);
if(this.fillPath==true){
this.world.context.fill(this.path2DObject)
}
//this.world.context.translate(-this.renderingX, -this.renderingY);//returning back to the origin of the canvas
this.removeTransformation();//removing the transformation applied on the object
}
SanimObject.call(this);
}
function CircleObject(x, y, radius, fill=false){
//this object draws a circle on the canvas
this.radius = radius; this.width = radius*2; this.height = this.width;
PathObject.call(this, x, y, [{pathMethod:'arc', params:[0, 0, radius, 0, Math.PI*2, false]}], true, fill);//calls the Path object to draw the circle
}
function StraightLineObject(x, y, xEnd, yEnd){
//this funcion draws a straight line in the canvas
this.xEnd = xEnd; this.yEnd = yEnd;
PathObject.call(this, x, y, [{pathMethod:'moveTo', params:[0, 0]}, {pathMethod:'lineTo', params:[xEnd-x, yEnd-y]}], false, false);//drawing the straight path
this.stampArrowHead = function(length = 20, angle=Math.PI/3){
//this method stamps a cursor to the PathObject
//add this as an object of it's own instead, arrowPointer
//the angle provided is the angle of separation between the arrow wings
if(!this.arrowHead){//checking if there is no existing arrow head so we don't have duplicate arrow head
var theta = Math.atan((this.y - this.yEnd)/(this.xEnd - this.x));//calculating theta
if(this.x>this.xEnd){
theta = theta+Math.PI;
}
this.arrowHead = new ArrowHeadObject(this.xEnd-this.x, this.yEnd-this.y, theta, length, angle);