forked from caleb531/jcanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjcanvas.js
executable file
·2069 lines (1816 loc) · 49.3 KB
/
jcanvas.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
/** @license jCanvas v6.1b
Copyright 2012, Caleb Evans
Licensed under the MIT license
*/
// Import frequently used globals
(function($, document, Image, Math, parseFloat, TRUE, FALSE, NULL, UNDEFINED) {
// Define local aliases to frequently used properties
var defaults,
prefs,
merge = $.extend,
round = Math.round,
PI = Math.PI,
sin = Math.sin,
cos = Math.cos,
originalEventFix = $.event.fix,
cache = {},
cssProps,
cssPropsObj;
// Preferences constructor (which inherits from the defaults object)
function Prefs() {}
// jCanvas function
function jCanvas(args) {
if (args) {
// Merge arguments with preferences
merge(prefs, args);
} else {
// Reset preferences to defaults if nothing is passed
jCanvas.prefs = prefs = Prefs.prototype = merge({}, defaults);
}
return this;
}
// Allow jCanvas function to be "chained" to other methods
$.fn.jCanvas = jCanvas;
jCanvas.version = '6.1b';
jCanvas.events = {};
// Set jCanvas default property values
defaults = {
align: 'center',
autosave: TRUE,
baseline: 'middle',
bringToFront: FALSE,
ccw: FALSE,
closed: FALSE,
compositing: 'source-over',
cornerRadius: 0,
cropFromCenter: TRUE,
draggable: FALSE,
disableDrag: FALSE,
each: NULL,
end: 360,
fillStyle: 'transparent',
font: '12pt sans-serif',
fromCenter: TRUE,
fromPoint: NULL,
height: NULL,
inDegrees: TRUE,
lineHeight: 1,
load: NULL,
mask: FALSE,
maxWidth: NULL,
method: NULL,
miterLimit: 10,
opacity: 1,
projection: 0,
r1: NULL,
r2: NULL,
radius: 0,
repeat: 'repeat',
rotate: 0,
rounded: FALSE,
scale: 1,
scaleX: 1,
scaleY: 1,
shadowBlur: 0,
shadowColor: 'transparent',
shadowX: 0,
shadowY: 0,
sHeight: NULL,
sides: 3,
source: '',
start: 0,
strokeCap: 'butt',
strokeJoin: 'miter',
strokeStyle: 'transparent',
strokeWidth: 1,
sWidth: NULL,
sx: NULL,
sy: NULL,
text: '',
translate: 0,
translateX: 0,
translateY: 0,
visible: TRUE,
width: NULL,
x: 0,
y: 0
};
// Copy defaults to preferences object
jCanvas();
// Set global properties
function setGlobalProps(ctx, params) {
// Fill/stroke styles
ctx.fillStyle = params.fillStyle;
ctx.strokeStyle = params.strokeStyle;
ctx.lineWidth = params.strokeWidth;
// Rounded corners for paths if chosen
if (params.rounded) {
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
} else {
ctx.lineCap = params.strokeCap;
ctx.lineJoin = params.strokeJoin;
ctx.miterLimit = params.miterLimit;
}
// Drop shadow styles
ctx.shadowOffsetX = params.shadowX;
ctx.shadowOffsetY = params.shadowY;
ctx.shadowBlur = params.shadowBlur;
ctx.shadowColor = params.shadowColor;
// Opacity and composite operation
ctx.globalAlpha = params.opacity;
ctx.globalCompositeOperation = params.compositing;
}
/* Internal helper methods */
// Get canvas context
function getContext(elem) {
return (elem && elem.getContext ? elem.getContext('2d') : NULL);
}
// Close path
function closePath(ctx, params) {
// Close path if chosen
if (params.closed) {
ctx.closePath();
}
ctx.fill();
// Prevent extra shadow created by stroke (but only when fill is present)
if (params.fillStyle !== 'transparent') {
ctx.shadowColor = 'transparent';
}
ctx.stroke();
// Close path if chosen
if (!params.closed) {
ctx.closePath();
}
// Restore transformation if transformShape() was called
if (params._toRad) {
ctx.restore();
}
// Mask shape if chosen
if (params.mask) {
if (params.autosave) {ctx.save();}
ctx.clip();
}
}
// Translate canvas (internal)
function translateCanvas(ctx, params) {
// Translate both the x- and y-axis using the 'translate' property
if (params.translate) {
params.translateX = params.translateY = params.translate;
}
// Translate shape
ctx.translate(params.translateX, params.translateY);
}
// Scale canvas (internal)
function scaleCanvas(ctx, params) {
// Scale both the x- and y- axis using the 'scale' property
if (params.scale !== 1) {
params.scaleX = params.scaleY = params.scale;
}
// Scale shape
ctx.translate(params.x, params.y);
ctx.scale(params.scaleX, params.scaleY);
ctx.translate(-params.x, -params.y);
}
// Rotate canvas (internal)
function rotateCanvas(ctx, params) {
params._toRad = (params.inDegrees ? PI/180 : 1);
ctx.translate(params.x, params.y);
ctx.rotate(params.rotate*params._toRad);
ctx.translate(-params.x, -params.y);
}
// Rotate/scale individual shape and/or position it correctly
function transformShape(e, ctx, params, width, height) {
// Measure angles in chosen units
params._toRad = (params.inDegrees ? PI/180 : 1);
ctx.save();
// Draw from center unless otherwise specified
// Valid points are tl, tc, tr, cl, cc, cr, bl, bc, br
height = height || width;
if (!e) {
if (!params.fromPoint) params.fromPoint = params.fromCenter ? 'cc' : 'tl'; // backwards compatible
if (params.fromPoint[0] == 't') {
params.y += height/2;
}
else if (params.fromPoint[0] == 'b') {
params.y -= height/2;
}
if (params.fromPoint[1] == 'l') {
params.x += width/2;
}
else if (params.fromPoint[1] == 'r') {
params.x -= width/2;
}
}
// Rotate shape if chosen
if (params.rotate || params.angle) {
rotateCanvas(ctx, params);
}
// Scale shape if chosen
if (params.scale !== 1 || params.scaleX !== 1 || params.scaleY !== 1) {
scaleCanvas(ctx, params);
}
if (params.translate || params.translateX || params.translateY) {
translateCanvas(ctx, params);
}
}
// Add support for draggable paths
function makePathDraggable(params) {
if (params.draggable) {
params.translateX += params.x;
params.translateY += params.y;
}
}
/* Plugin API */
// Extend jCanvas with custom methods
jCanvas.extend = function(plugin) {
// Merge properties with defaults
jCanvas.defaults = defaults = merge(defaults, plugin.props);
jCanvas();
// Create plugin
if (plugin.name) {
$.fn[plugin.name] = function self(args) {
var $elems = this, elem, e, ctx,
params = merge(new Prefs(), args);
for (e=0; e<$elems.length; e+=1) {
elem = $elems[e];
ctx = getContext(elem);
if (ctx) {
addLayer(elem, args, self);
setGlobalProps(ctx, params);
plugin.fn.call(elem, ctx, params);
}
}
return $elems;
};
}
return $.fn[plugin.name];
};
/* Layer API */
// Keep track of the last two mouse coordinates for each canvas
function getCanvasData(elem) {
var data;
if (cache.elem === elem) {
// Retrieve canvas data from cache
data = cache.data;
} else {
// Get canvas data
data = $.data(elem, 'jCanvas');
// Create canvas data object if it does not already exist
if (!data) {
data = $.data(elem, 'jCanvas', {
layers: [],
intersects: [],
drag: {},
event: {}
});
}
// Cache canvas data
cache.elem = elem;
cache.data = data;
}
return data;
}
// Get jCanvas layers
$.fn.getLayers = function() {
var elem = this[0], layers;
if (!elem || !elem.getContext) {
layers = [];
} else {
layers = getCanvasData(elem).layers;
}
return layers;
};
// Get a single jCanvas layer
$.fn.getLayer = function(id) {
var layers = this.getLayers(),
idType = $.type(id),
layer, l;
if (id && id.layer) {
// Return the layer itself if given
layer = id;
} else if (idType === 'number') {
// Get layer based on given index
layer = layers[id];
} else {
// Get layer based on given layer name
for (l=0; l<layers.length; l+=1) {
// Ensure layer's index property is accurate
layers[l].index = l;
// Check if layer matches name
if (layers[l].name === id || (idType === 'regexp' && layers[l].name.match(id))) {
layer = layers[l];
break;
}
}
}
return layer;
};
// Set properties of a layer
$.fn.setLayer = function(id, props) {
var $elems = this, e,
layer;
for (e=0; e<$elems.length; e+=1) {
layer = $($elems[e]).getLayer(id);
// Merge properties with layer
merge(layer, props);
}
return $elems;
};
// Remove a jCanvas layer
$.fn.removeLayer = function(id) {
var $elems = this, $elem, e,
layers, layer;
for (e=0; e<$elems.length; e+=1) {
$elem = $($elems[e]);
// Retrieve layers array and desired layer
layers = $elem.getLayers();
layer = $elem.getLayer(id);
// Remove layer if found
if (layer) {
layers.splice(layer.index, 1);
}
}
return $elems;
};
// Remove all jCanvas layers
$.fn.removeLayers = function() {
var $elems = this, e,
layers;
for (e=0; e<$elems.length; e+=1) {
layers = $($elems[e]).getLayers();
layers.length = 0;
}
return $elems;
};
// Get all layers in the given group
$.fn.getLayerGroup = function(id) {
var layers = this.getLayers(),
idType = $.type(id),
group = [], l;
if (idType === 'array') {
// Return layer group if given
return id;
} else {
// Otherwise, find layers in group based on group name
for (l=0; l<layers.length; l+=1) {
// Ensure layer's index property is accurate
layers[l].index = l;
// Include layer is associated with group
if (layers[l].group === id || (idType === 'regexp' && layers[l].group.match(id))) {
group.push(layers[l]);
}
}
}
return group;
};
// Set properties of all layers in the given group
$.fn.setLayerGroup = function(id, props) {
var $elems = this, $elem, e,
group, l;
for (e=0; e<$elems.length; e+=1) {
// Get layer group
$elem = $($elems[e]);
group = $elem.getLayerGroup(id);
for (l=0; l<group.length; l+=1) {
// Merge given properties with layer
merge(group[l], props);
}
}
return $elems;
};
// Remove all layers within a specific group
$.fn.removeLayerGroup = function(id) {
var $elems = this, $elem, e,
idType = $.type(id),
layers, group, l;
if (id !== UNDEFINED) {
for (e=0; e<$elems.length; e+=1) {
$elem = $($elems[e]);
layers = $elem.getLayers();
// Get layers array for each element
group = $elem.getLayerGroup(id);
// Remove all layers in the group from the layers array
for (l=0; l<group.length; l+=1) {
layers.splice(group[l].index, 1);
}
}
}
return $elems;
};
// Draw individual layer (internal)
function drawLayer($elem, ctx, layer) {
if (layer && layer.visible) {
if (layer.method === $.fn.draw) {
// If layer is a function, call it
layer.fn.call($elem[0], ctx);
} else if (layer.method) {
// If layer is an object, call its respective method
layer.method.call($elem, layer);
}
}
}
// Draw an individual layer
$.fn.drawLayer = function(name) {
var $elems = this, e, ctx,
$elem, layer;
for (e=0; e<$elems.length; e+=1) {
$elem = $($elems[e]);
ctx = getContext($elems[e]);
// Retrieve the specified layer
layer = $elem.getLayer(name);
drawLayer($elem, ctx, layer);
}
return $elems;
};
// Draw all layers (or only the given layers)
$.fn.drawLayers = function(resetFire) {
var $elems = this, $elem, e, ctx,
layers, layer, l,
data, eventCache, eventType,
drag, callback;
for (e=0; e<$elems.length; e+=1) {
$elem = $($elems[e]);
ctx = getContext($elems[e]);
if (ctx) {
// Clear canvas first
ctx.clearRect(0, 0, $elems[e].width, $elems[e].height);
// Retrieve canvas data from cache
if (cache.elem === $elems[e]) {
data = cache.data;
} else {
// Get canvas data and cache it
data = getCanvasData($elems[e]);
cache.elem = $elems[e];
cache.data = data;
}
layers = data.layers;
// Draw layers from first to last (bottom to top)
for (l=0; l<layers.length; l+=1) {
layer = layers[l];
// Ensure layer index is up-to-date
layer.index = l;
// Prevent any one event from firing excessively
if (resetFire) {
layer._fired = FALSE;
}
drawLayer($elem, ctx, layer);
// Trigger mouseout event if necessary
if (layer._mousedout) {
layer._mousedout = FALSE;
layer._fired = TRUE;
layer._hovered = FALSE;
if (layer.mouseout) {
layer.mouseout.call($elems[e], layer);
}
// Revert cursor when mousing off layer
if (layer.cursor && layer._cursor) {
$elem.css({
cursor: layer._cursor
});
}
}
}
layer = data.intersects[data.intersects.length-1] || {};
eventCache = data.event;
eventType = eventCache.type;
callback = layer[eventType];
drag = data.drag;
// Check events for intersecting layer
if (layer._event) {
// Detect mouseover events
if (layer.mouseover || layer.mouseout || layer.cursor) {
if (!layer._hovered && !layer._fired) {
layer._fired = TRUE;
layer._hovered = TRUE;
if (layer.mouseover) {
layer.mouseover.call($elems[e], layer);
}
// Set cursor when mousing over layer
if (layer.cursor) {
layer._cursor = $elem.css('cursor');
$elem.css({
cursor: layer.cursor
});
}
}
}
// Detect any other mouse event
if (callback && !layer._fired) {
layer._fired = TRUE;
callback.call($elems[e], layer);
}
// Use the mousedown event to start drag
if (layer.draggable && !layer.disableDrag && eventType === 'mousedown') {
// Being layer to front when drag starts (if chosen)
if (layer.bringToFront) {
layers.splice(layer.index, 1);
// The push() method returns the new length of the array
layer.index = layers.push(layer);
}
// Keep track of drag state
drag.layer = layer;
drag.dragging = TRUE;
drag.startX = layer.x;
drag.startY = layer.y;
drag.endX = layer.mouseX;
drag.endY = layer.mouseY;
// Trigger dragstart event if defined
if (layer.dragstart) {
layer.dragstart.call($elems[e], layer);
}
}
}
// Dragging a layer works independently from other events
if (drag.layer) {
// Use the mouseup event to stop the drag
if (drag.dragging && eventType === 'mouseup') {
// Trigger dragstop event if defined
if (drag.layer.dragstop) {
drag.layer.dragstop.call($elems[e], drag.layer);
}
data.drag = {};
}
// Regardless of whether the cursor is on the layer, drag the layer until drag stops
if (drag.dragging && eventType === 'mousemove') {
drag.layer.x = drag.layer.mouseX - (drag.endX - drag.startX);
drag.layer.y = drag.layer.mouseY - (drag.endY - drag.startY);
// Trigger drag event if defined
if (drag.layer.drag) {
drag.layer.drag.call($elems[e], drag.layer);
}
}
}
}
}
data.intersects = [];
return $elems;
};
// Add a jCanvas layer (internal)
function addLayer(elem, layer, method) {
var $elem, layers, event, layerFn,
isFn = (typeof layer === 'function'),
data, dragHelperEvents, i;
layer = layer || {};
// Only add layer if it hasn't been added before
if (layer.layer && !layer._layer) {
$elem = $(elem);
layers = $elem.getLayers();
// If layer is a function, wrap it in an object
if (isFn) {
layerFn = layer;
// Wrap function within object
layer = {
method: $.fn.draw,
fn: layerFn
};
}
// Ensure layers are unique across canvases by cloning them
layer = merge(new Prefs(), layer);
// Detect events for non-function layers
if (!isFn) {
// Associate a jCanvas method with layer
layer.method = $.fn[layer.method] || method;
// Retrieve canvas data
data = getCanvasData(elem);
// Check for any associated jCanvas events and enable them
for (event in jCanvas.events) {
if (jCanvas.events.hasOwnProperty(event) && layer[event]) {
jCanvas.events[event]($elem, data);
layer._event = TRUE;
}
}
// Enable drag-and-drop support and cursor support
if (layer.draggable || layer.cursor) {
layer._event = TRUE;
dragHelperEvents = ['mousedown', 'mousemove', 'mouseup'];
for (i=0; i<dragHelperEvents.length; i+=1) {
event = dragHelperEvents[i];
jCanvas.events[event]($elem, data);
}
// If cursor mouses out of canvas while dragging, cancel drag
if (!data.mouseout) {
$elem.bind('mouseout.jCanvas', function() {
data.drag = {};
$elem.drawLayers();
});
data.mouseout = TRUE;
}
}
}
// Set layer properties and add to stack
layer.layer = TRUE;
layer._layer = TRUE;
// Add layer to end of array if no index is specified
if (layer.index === UNDEFINED) {
layer.index = layers.length;
}
// Add layer to layers array at specified index
layers.splice(layer.index, 0, layer);
}
}
// Add a jCanvas layer
$.fn.addLayer = function(args) {
var $elems = this, e, ctx;
args = args || {};
for (e=0; e<$elems.length; e+=1) {
ctx = getContext($elems[e]);
if (ctx) {
args.layer = TRUE;
addLayer($elems[e], args);
}
}
return $elems;
};
/* Animation API */
// Define properties used in both CSS and jCanvas
cssProps = [
'width',
'height',
'opacity',
'lineHeight'
];
cssPropsObj = {};
// Hide/show jCanvas/CSS properties so they can be animated using jQuery
function showProps(obj) {
var i;
for (i=0; i<cssProps.length; i+=1) {
obj[cssProps[i]] = obj['_' + cssProps[i]];
}
}
function hideProps(obj, reset) {
var i;
for (i=0; i<cssProps.length; i+=1) {
obj['_' + cssProps[i]] = obj[cssProps[i]];
cssPropsObj[cssProps[i]] = 1;
if (reset) {
delete obj[cssProps[i]];
}
}
}
// Convert a color value to RGB
function toRgb(color) {
var originalColor, elem,
rgb = [],
multiple = 1;
// Deal with hexadecimal colors and color names
if (color.match(/^#?\w+$/i)) {
// Deal with complete transparency
if (color === 'transparent') {
color = 'rgba(0,0,0,0)';
}
elem = document.head;
originalColor = elem.style.color;
elem.style.color = color;
color = $.css(elem, 'color');
elem.style.color = originalColor;
}
// Parse RGB string
if (color.match(/^rgb/i)) {
rgb = color.match(/\d+/gi);
// Deal with RGB percentages
if (color.match(/%/gi)) {
multiple = 2.55;
}
rgb[0] *= multiple;
rgb[1] *= multiple;
rgb[2] *= multiple;
// Ad alpha channel if given
if (rgb[3] !== UNDEFINED) {
rgb[3] = parseFloat(rgb[3]);
} else {
rgb[3] = 1;
}
}
return rgb;
}
// Animate a hex or RGB color
function animateColor(fx) {
var n = 3,
i;
// Only parse start and end colors once
if (typeof fx.start !== 'object') {
fx.start = toRgb(fx.start);
fx.end = toRgb(fx.end);
}
fx.now = [];
// If colors are RGBA, animate transparency
if (fx.start[3] !== 1 || fx.end[3] !== 1) {
n = 4;
}
// Calculate current frame for red, green, blue, and alpha
for (i=0; i<n; i+=1) {
fx.now[i] = fx.start[i] + (fx.end[i] - fx.start[i]) * fx.pos;
// Only the red, green, and blue values must be integers
if (i < 3) {
fx.now[i] = round(fx.now[i]);
}
}
if (fx.start[3] !== 1 || fx.end[3] !== 1) {
// Only use RGBA if RGBA colors are given
fx.now = 'rgba(' + fx.now.join(',') + ')';
} else {
// Otherwise, animate as solid colors
fx.now.slice(0, 3);
fx.now = 'rgb(' + fx.now.join(',') + ')';
}
// Animate colors for both canvas layers and DOM elements
if (fx.elem.nodeName) {
fx.elem.style[fx.prop] = fx.now;
} else {
fx.elem[fx.prop] = fx.now;
}
}
// Animate jCanvas layer
$.fn.animateLayer = function() {
var $elems = this, $elem, e, ctx,
args = ([]).slice.call(arguments, 0),
layer;
// Deal with all cases of argument placement
/*
0. layer name/index
1. properties
2. duration/options
3. easing
4. complete function
5. step function
*/
if (typeof args[0] === 'object' && !args[0].layer) {
// Animate first layer by default
args.unshift(0);
}
if (typeof args[2] === 'object') {
// Accept an options object for animation
args.splice(2, 0, args[2].duration || NULL);
args.splice(3, 0, args[3].easing || NULL);
args.splice(4, 0, args[4].complete || NULL);
args.splice(5, 0, args[5].step || NULL);
} else {
if (args[2] === UNDEFINED) {
// If object is the last argument
args.splice(2, 0, NULL);
args.splice(3, 0, NULL);
args.splice(4, 0, NULL);
} else if (typeof args[2] === 'function') {
// If callback comes after object
args.splice(2, 0, NULL);
args.splice(3, 0, NULL);
}
if (args[3] === UNDEFINED) {
// If duration is the last argument
args[3] = NULL;
args.splice(4, 0, NULL);
} else if (typeof args[3] === 'function') {
// If callback comes after duration
args.splice(3, 0, NULL);
}
}
// Run callback function when animation completes
function complete($elem, layer) {
return function() {
showProps(layer);
$elem.drawLayers();
if (args[4]) {
args[4].call($elem[0], layer);
}
};
}
// Redraw layers on every frame of the animation
function step($elem, layer) {
return function(now, fx) {
showProps(layer);
$elem.drawLayers();
// Run callback function for every frame (if specified)
if (args[5]) {
args[5].call($elem[0], now, fx, layer);
}
};
}
// Do not modify original object
args[1] = merge({}, args[1]);
for (e=0; e<$elems.length; e+=1) {
$elem = $($elems[e]);
ctx = getContext($elems[e]);
if (ctx) {
// If a layer object was passed, use it the layer to be animated
layer = $elem.getLayer(args[0]);
// Ignore layers that are functions
if (layer && layer.method !== $.fn.draw) {
// Bypass jQuery CSS Hooks for CSS properties (width, opacity, etc.)
hideProps(layer);
hideProps(args[1], TRUE);
// Fix for jQuery's vendor prefixing support, which affects how width/height/opacity are animated
layer.style = cssPropsObj;
// Animate layer
$(layer).animate(args[1], {
duration: args[2],
easing: ($.easing[args[3]] ? args[3] : NULL),
// When animation completes
complete: complete($elem, layer),
// Redraw canvas for every animation frame
step: step($elem, layer)
});
}
}
}
return $elems;
};
// Animate all layers in a layer group
$.fn.animateLayerGroup = function(id) {
var $elems = this, $elem, e,
args = ([]).slice.call(arguments, 0),
group, l;
for (e=0; e<$elems.length; e+=1) {
$elem = $($elems[e]);
group = $elem.getLayerGroup(id);
// Animate all layers in the group
for (l=0; l<group.length; l+=1) {
$elem.animateLayer.apply($elem, [group[l]].concat(args.slice(1)));
}
}
};
// Delay layer animation by a given number of milliseconds
$.fn.delayLayer = function(id, duration) {
var $elems = this, e, layer;
duration = duration || 0;
for (e=0; e<$elems.length; e+=1) {
layer = $($elems[e]).getLayer(id);
$(layer).delay(duration);
}
return $elems;
};
// Delay animation all layers in a layer group
$.fn.delayLayerGroup = function(id, duration) {
var $elems = this, $elem, e,
group, l;
duration = duration || 0;
for (e=0; e<$elems.length; e+=1) {
$elem = $($elems[e]);
group = $elem.getLayerGroup(id);
// Delay all layers in the group
for (l=0; l<group.length; l+=1) {
$elem.delayLayer.call($elem, group[l], duration);
}
}
};
// Stop layer animation
$.fn.stopLayer = function(id, clearQueue) {
var $elems = this, e, layer;
for (e=0; e<$elems.length; e+=1) {
layer = $($elems[e]).getLayer(id);
$(layer).stop(clearQueue);
}
return $elems;
};
// Stop animation of all layers in a layer group
$.fn.stopLayerGroup = function(id, clearQueue) {
var $elems = this, $elem, e,
group, l;
for (e=0; e<$elems.length; e+=1) {
$elem = $($elems[e]);
group = $elem.getLayerGroup(id);
// Stop all layers in the group
for (l=0; l<group.length; l+=1) {
$elem.stopLayer.call($elem, group[l], clearQueue);
}
}
};
// Enable animation for color properties
function supportColorProps(props) {
var p;
for (p=0; p<props.length; p+=1) {
$.fx.step[props[p]] = animateColor;
}
}
// Enable animation for color properties
supportColorProps([