-
Notifications
You must be signed in to change notification settings - Fork 13
/
intence.js
3660 lines (3084 loc) · 103 KB
/
intence.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
/**
* @fileoverview intence - scrolling indicator
* @version 1.1.4
*
* @license MIT, see http://github.com/asvd/intence
* @copyright 2015 asvd <[email protected]>
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports);
} else {
factory((root.intence = {}));
}
}(this, function (exports) {
var cfg = {
textureMaxSqueeze : 1000,
indicatorMaxArea : .12,
gainSlownessMin : 300,
gainSlownessMax : 5000,
animationTime : 160,
animationDelay : 20
};
cfg.blocksNumber =
1+Math.ceil(Math.log(cfg.textureMaxSqueeze)/Math.log(4));
var UQ = 'intence-unique-' + (new Date().getTime());
var elemSample = {
div : document.createElement('div'),
img : document.createElement('img'),
canvas : document.createElement('canvas'),
object : document.createElement('object'),
style : document.createElement('style')
};
/**
* Checks if the CSS property may have as a value the CSS function
* with the given parameters
*
* @param {String} name of the property
* @param {String} value (or a function name)
* @param {String} params of a function as example
*
* @returns {Boolean}
*/
var checkCSSProperty = function(name, value, params) {
var idx, camel = name;
while ((idx = camel.indexOf('-')) != -1) {
camel = camel.slice(0, idx) +
camel.slice(idx+1, idx+2).toUpperCase() +
camel.slice(idx+2);
}
var elem = elemSample.div.cloneNode(false);
var csstext = name + ': ' + value;
if (params) {
csstext += '('+params+')';
}
elem.style.cssText = csstext;
return camel in document.documentElement.style &&
elem.style[camel].indexOf(value) != '-1';
}
var UA = navigator.userAgent;
var IS_FF = typeof InstallTrigger !== 'undefined';
var IS_IE = /*@cc_on!@*/false || !!document.documentMode;
var IS_SAFARI =
Object.prototype.toString.call(window.HTMLElement).
indexOf('Constructor') > 0;
var IS_CHROME = !!window.chrome;
// what is supported by the browser
var features = {
classList : !!elemSample.div.classList,
event : !!window.addEventListener,
canvas : !!elemSample.canvas.getContext,
isolation : checkCSSProperty('isolation', 'isolate'),
opacity : checkCSSProperty('opacity', '.5'),
transform : checkCSSProperty(
'transform', 'translate3d', '0, 0, 0'
),
webkitTransform : checkCSSProperty(
'-webkit-transform', 'rotate', '-180deg'
),
backgroundCanvas : {
webkit :
!!document.getCSSCanvasContext &&
checkCSSProperty(
'background', '-webkit-canvas', 'a'
),
mozElement : checkCSSProperty(
'background', '-moz-element', '#a'
)
},
gradientMask : {
alphaFilter : checkCSSProperty(
'filter',
'progid:DXImageTransform.Microsoft.Alpha',
'opacity=100,finishOpacity=0,style=1,'+
'startX=0,finishX=1,startY=0,finishY=1'
),
webkit : checkCSSProperty(
'-webkit-mask-image',
'-webkit-linear-gradient',
'top, rgba(0,0,0,1), rgba(0,0,0,0) 100%'
),
svgReuse : IS_FF
}
};
var IS_IE9 = IS_IE && features.gradientMask.alphaFilter;
var INTENCE_ENABLED = true;
// implementations to use according to the available features
var METHODS = {
// list of element classes
hasClass : features.classList ? 'classList' : 'className',
// asynchronous function invocation
async : features.event ? 'event' : 'setTimeout',
// creating own z-index stacking context for an element
stackingContext : null,
// rendering a set of image blocks
blocks : features.webkitTransform ? 'webkitTransform': 'div',
// applying a transparency gradient mask
mask : null,
// using a canvas element as a background
canvas : null,
// setting float:left for the element
floatLeft : null
};
if (features.canvas) {
if (features.gradientMask.webkit) {
METHODS.mask = 'webkit';
METHODS.canvas = features.backgroundCanvas.webkit ?
'webkit' : 'dataURL';
} else if (features.gradientMask.svgReuse) {
if (features.backgroundCanvas.mozElement) {
METHODS.canvas = 'mozElement';
METHODS.mask = 'svgReuse';
} else {
METHODS.blocks = 'svg';
}
} else if (features.gradientMask.alphaFilter) {
METHODS.canvas = 'dataURL';
METHODS.mask = 'alphaFilter';
METHODS.blocks = 'div';
} else {
METHODS.blocks = 'svg';
}
if (features.isolation) {
METHODS.stackingContext = 'isolation';
} else if (!IS_IE9) {
if (features.transform) {
METHODS.stackingContext = 'transform';
} else if (features.opacity) {
METHODS.stackingContext = 'opacity';
}
} // IE9 messes up indicator filters
} else {
// cannot do anything without canvas
INTENCE_ENABLED = false;
}
// FF < 36 requires a stylesheet to set float:left
if (IS_FF && +UA.match(/Firefox\/(\d+)/)[1] < 36) {
METHODS.floatLeft = 'stylesheet';
} else {
METHODS.floatLeft = 'direct';
}
if (METHODS.blocks == 'svg' && !IS_IE) {
// svg variant only works reasonably fast in IE
INTENCE_ENABLED = false;
}
// disabling particular browsers
var match;
if (
// supported Opera 15+ does not contain its name in UA
UA.indexOf('Opera') != -1 ||
// FF < 8 not supported
(IS_FF &&
(match = UA.match(/Firefox\/(\d+)/)) &&
+match[1] < 8) ||
// Safari < 7 not supported
(IS_SAFARI &&
(match = UA.match(/Version\/(\d+)/)) &&
+match[1] < 7) ||
// Chrome on iPad < 15 not supported
(IS_SAFARI &&
(match = UA.match(/CriOS\/(\d+)/)) &&
+match[1] < 15) ||
// Chrome < 15 not supported
(IS_CHROME &&
(match=UA.match(/Chrome\/(\d+)/)) &&
+match[1] < 15)
) {
INTENCE_ENABLED = false;
}
var impl = {}; // browser-dependent implementations
/**
* Applies gradient mask to the given component
*
* Uses -webkit-mask-image CSS property
*
* @param {Element} elem DOM element to apply mask to
* @param {String} dir direction of the mask
*/
var gradientMask_webkit = function(elem, dir) {
var where = {
north : 'top',
east : 'right',
south : 'bottom',
west : 'left'
};
elem.style.WebkitMaskImage =
'-webkit-linear-gradient('
+ where[dir] + ','
+ 'rgba(0,0,0,1) 0%,'
+ 'rgba(0,0,0,.81) 10%,'
+ 'rgba(0,0,0,.64) 20%,'
+ 'rgba(0,0,0,.49) 30%,'
+ 'rgba(0,0,0,.36) 40%,'
+ 'rgba(0,0,0,.25) 50%,'
+ 'rgba(0,0,0,.16) 60%,'
+ 'rgba(0,0,0,.09) 70%,'
+ 'rgba(0,0,0,.04) 80%,'
+ 'rgba(0,0,0,.01) 90%,'
+ 'rgba(0,0,0,0) 100%'
+ ')';
}
/**
* Applies gradient mask to the given component
*
* Uses DXImageTransform.Microsoft.Alpha filter
*
* @param {Element} elem DOM element to apply mask to
* @param {String} dir direction of the mask
*/
var gradientMask_alphaFilter = function(elem, dir) {
var pos = {
x1 : 0,
x2 : 0,
y1 : 0,
y2 : 0
};
var full = {
north : 'y2',
east : 'x1',
south : 'y1',
west : 'x2'
};
pos[full[dir]] = '100';
var filter =
'progid:DXImageTransform.Microsoft.Alpha('+
'opacity=100,'+
'finishOpacity=0,'+
'style=1,'+ // linear
'startX=' +pos.x1+','+
'finishX='+pos.x2+','+
'startY=' +pos.y1+','+
'finishY='+pos.y2+''+
')';
elem.style.filter = filter;
}
var svgMaskIds = {
north : null,
east : null,
south : null,
west : null
};
/**
* Applies gradient mask to the given component
*
* Uses generated SVG element
*
* @param {Element} elem DOM element to apply mask to
* @param {String} dir direction of the mask
*/
var gradientMask_svgReuse = function(elem, dir) {
if (!svgMaskIds[dir]) {
svgMaskIds[dir] = genMaskSVG(dir);
}
elem.style.mask = 'url(#'+svgMaskIds[dir]+')';
}
/**
* Generates an SVG image containing a gradient mask
*
* @param {String} dir direction of the mask gradient
*
* @return {String} mask id to reuse
*/
var genMaskSVG = function(dir) {
var id = 'svg-mask-'+dir+'-'+UQ;
var maskId = 'mask-'+id;
var gradientId = 'gradient-'+id;
var svg = util.genSVGElement('svg');
var defs = util.genSVGElement('defs', svg);
util.genSVGLinearGradient(defs, dir, gradientId);
var mask = util.genSVGElement('mask', defs, {
id : maskId,
maskUnits : 'objectBoundingBox',
maskContentUnits : 'objectBoundingBox'
});
util.genSVGElement('rect', mask, {
y : '0',
width : '1',
height : '1',
fill : 'url(#'+gradientId+')'
});
util.setStyle(svg, {
position : 'absolute',
width : '0px',
height : '0px'
});
document.body.appendChild(svg);
return maskId;
}
/**
* Applies gradient mask to the given component
*
* @param {Element} elem DOM element to apply mask to
* @param {String} dir direction of the mask
*/
switch (METHODS.mask) {
case 'svgReuse':
impl.gradientMask = gradientMask_svgReuse;
break;
case 'webkit':
impl.gradientMask = gradientMask_webkit;
break;
case 'alphaFilter':
impl.gradientMask = gradientMask_alphaFilter;
break;
}
// creating own z-index stacking context for an element
/**
* Creates own stacking context for the given element
*
* Uses isolation css property
*
* @param {Element} elem to create stacking context for
*/
var stackingContext_isolation = function(elem) {
elem.style.isolation = 'isolate';
}
/**
* Creates own stacking context for the given element
*
* Applies identity transformation to an element (which forces
* initializinga stacking context for the element, since the
* transformation is processed by GPU)
*
* @param {Element} elem to create stacking context for
*/
var stackingContext_transform = function(elem) {
elem.style.transform = 'translate3d(0,0,0)';
}
/**
* Creates own stacking context for the given element
*
* Sets the opacity of the element to a value a bit smaller than 1
* (which forces initializinga stacking context for the element,
* since the transformation is processed by GPU)
*
* @param {Element} elem to create stacking context for
*/
var stackingContext_opacity = function(elem) {
elem.style.opacity = .9999999;
}
/**
* Creates own stacking context for the given element
*
* @param {Element} elem to create stacking context for
*/
switch (METHODS.stackingContext) {
case 'isolation':
impl.stackingContext = stackingContext_isolation;
break;
case 'transform':
impl.stackingContext = stackingContext_transform;
break;
case 'opacity':
impl.stackingContext = stackingContext_opacity;
break;
default:
impl.stackingContext = function(){};
break;
}
// Using canvas as a background in different browsers
/**
* Sets the content of the given canvas as a background for the
* given element
*
* Obtains the raw data using toDataURL() method and places it as
* a background
*
* @param {Element} elem to set background for
* @param {Element} canvas element to use as a background
*/
var backgroundCanvas_dataURL = function(elem, canvas) {
elem.style.backgroundImage =
'url('+util.getCanvasDataURL(canvas)[0]+')';
}
/**
* Sets the content of the given canvas as a background for the
* given element
*
* Uses the global css canvas context along with -webkit-canvas
* CSS function
*
* @param {Element} elem to set background for
* @param {Element} canvas element to use as a background
*/
var canvasCSSCounter = 0;
var backgroundCanvas_webkit = function(elem, canvas) {
if (typeof canvas.CSSContextId == 'undefined') {
var id = 'canvasCSSContext-'+(canvasCSSCounter++)+'-'+UQ;
var ctx = document.getCSSCanvasContext(
'2d', id, canvas.width, canvas.height
);
ctx.drawImage(canvas, 0,0);
canvas.CSSContextId = id;
}
elem.style.background =
'-webkit-canvas('+canvas.CSSContextId+')';
}
/**
* Sets the content of the given canvas as a background for the
* given element
*
* Uses the existing canvas along with the -moz-element CSS
* function
*
* @param {Element} elem to set background for
* @param {Element} canvas element to use as a background
*/
var canvasMozElementCounter = 0;
var backgroundCanvas_mozElement = function(elem, canvas) {
if (!canvas.getAttribute('id')) {
var id = 'MozElement-'+(canvasMozElementCounter++)+'-'+UQ;
canvas.setAttribute('id', id);
util.setStyle(canvas, {
position: 'absolute',
width: '0px',
height: '0px'
});
document.body.appendChild(canvas);
}
elem.style.background =
'-moz-element(#'+canvas.getAttribute('id')+')';
}
switch (METHODS.canvas) {
case 'dataURL':
impl.backgroundCanvas = backgroundCanvas_dataURL;
break;
case 'webkit':
impl.backgroundCanvas = backgroundCanvas_webkit;
break;
case 'mozElement':
impl.backgroundCanvas = backgroundCanvas_mozElement;
break;
}
// Asynchronous function invocation
/**
* Performs a method asynchronously
*
* setTimeout() version
*
* @param {Function} func method to invoke
* @param {Object} obj context object
* @param {Array} args
*/
var async_setTimeout = function(func, obj, args) {
setTimeout(function() {
func.apply(obj||null, args||[]);
}, 0);
}
/**
* Performs a method asynchronously
*
* Event emission version
*
* @param {Function} func method to invoke
* @param {Object} obj context object
* @param {Array} args
*/
var async_event = function(func, obj, args) {
asyncs.push([func, obj||window, args]);
window.postMessage(asyncMsg, '*');
}
var asyncs = [];
var asyncMsg = 'async-' + UQ;
var invoke = function(event) {
if (event.source == window &&
event.data == asyncMsg) {
if (asyncs.length > 0) {
var async = asyncs.shift();
async[0].apply(async[1], async[2]);
}
}
};
if (METHODS.async == 'event') {
window.addEventListener('message', invoke, true);
impl.async = async_event;
} else {
impl.async = async_setTimeout;
}
// Obtaining a list of element classes
/**
* Checks if the given element has the given class
*
* reads classList array
*
* @param {Element} elem to check against having the class
* @param {String} cls class name
*
* @returns {Boolean}
*/
var hasClass_classList = function(elem, cls) {
var result = false;
var list= elem.classList;
for (var i = 0; i < list.length; i++){
if (list[i] == cls) {
result = true;
break;
}
}
return result;
}
/**
* Checks if the given element has the given class
*
* evaluates className string
*
* @param {Element} elem to check against having the class
* @param {String} cls class name
*
* @returns {Boolean}
*/
var hasClass_className = function(elem, cls) {
var result = false;
var list = elem.className.split(' ');
for (var i = 0; i < list.length; i++){
if (list[i] == cls) {
result = true;
break;
}
}
return result;
}
if (METHODS.hasClass == 'classList') {
impl.hasClass = hasClass_classList;
} else {
impl.hasClass = hasClass_className;
}
// setting float:left for an element
/**
* Applies float:left for the element.
*
* Direct implementation simply updates the style property
*
* @param {Element} elem to set float:left for
*/
var floatLeft_direct = function(elem) {
elem.style['float'] ='left';
}
/**
* Applies float:left for the element.
*
* For some reasons, direct way does not work in FF < 36, so this
* variant creates a stylesheet and sets the class for the element
*
* @param {Element} elem to set float:left for
*/
var floatLeft_stylesheet = function(elem) {
elem.className = getFloatLeftClassName();
}
/**
* Creates a stylesheet (if not created yet) with the css class
* having float:left
*
* @returns {String} name of the css class to apply
*/
var _floatLeftClassName = null;
var getFloatLeftClassName = function() {
if (!_floatLeftClassName) {
var cls = 'float-left-cls-'+UQ;
var head = document.getElementsByTagName('head')[0];
var style = elemSample.style.cloneNode(false);
style.type = 'text/css';
style.innerHTML = '.' + cls + ' { float:left; }';
head.appendChild(style);
_floatLeftClassName = cls;
}
return _floatLeftClassName;
}
if (METHODS.floatLeft == 'stylesheet') {
impl.floatLeft = floatLeft_stylesheet;
} else {
impl.floatLeft = floatLeft_direct;
}
var util = {};
util.dir = ['north','east','south','west'];
util.isVertical = {
north : true,
east : false,
south : true,
west : false
};
util.ccv = {
north : 'west',
east : 'north',
south : 'east',
west : 'south'
};
/**
* Applies the list of style properties to the element
*
* @param {Element} elem to apply style to
* @param {Object} style
*/
util.setStyle = function(elem, style) {
for (var key in style) {
if (style.hasOwnProperty(key)) {
elem.style[key] = style[key];
}
}
}
/**
* Applies the set of attributes to the element
*
* @param {Element} elem to set attributes for
* @param {Object} attributes
*/
util.setAttributes = function(elem, attributes) {
for (var key in attributes) {
if (attributes.hasOwnProperty(key)) {
elem.setAttribute(key, attributes[key]);
}
}
}
/**
* Casts the value to the string and adds 'px' to the end
*
* @param {Number} value
*
* @returns {String}
*/
util.px = function(value) {
return '' + value + 'px';
}
/**
* Removes all child nodes from the given element, returns those
* as an array
*
* @param {Element} elem to remove the child nodes from
*
* @returns {Array} nodes removed from the element
*/
util.detachChildren = function(elem) {
var detached = [];
while (elem.firstChild) {
detached.push(elem.removeChild(elem.firstChild));
}
return detached;
}
/**
* Attaches the given set of nodes to the given element
*
* @param {Element} elem to attach nodes to
* @param {Array} nodes to attach as children
*/
util.attachChildren = function(elem, nodes) {
for (var i = 0; i < nodes.length; i++) {
elem.appendChild(nodes[i]);
}
}
/**
* Returns a cached copy of dataURL of the canvas, performs
* caching if needed (assuming the image is never changed).
*
* The dataURL itself is always transfered in an array in order to
* prevent copying the whole string upon function invocation
*
* @param {Element} canvas
*
* @returns {Array} dataURL with the canvas image
*/
util.getCanvasDataURL = function(canvas) {
if (typeof canvas.intence_cached_dataURL == 'undefined') {
canvas.intence_cached_dataURL = [canvas.toDataURL()];
}
return canvas.intence_cached_dataURL;
}
/**
* Creates and returns a new canvas element
*
* @param {Number} w width
* @param {Number} h height
*
* @returns {Element} created canvas element
*/
util.genCanvas = function(w,h) {
var canvas = elemSample.canvas.cloneNode(false);
canvas.width = w;
canvas.height = h;
util.setStyle(canvas, {
width : util.px(w),
height : util.px(h),
display : 'none'
});
return canvas;
}
/**
* Produces a canvas element containing the image of the given img
* element
*
* @param {Element} img
*
* @returns {Element} canvas
*/
util.img2canvas = function(img) {
var canvas = util.genCanvas(
img.width || img.naturalWidth,
img.height || img.naturalHeight
);
var ctx = canvas.getContext('2d');
ctx.drawImage(img,0,0);
return canvas;
}
/**
* Creates and returns a new SVG element
*
* @param {String} name of the SVG element to create
* @param {Element} parent element
* @param {Object} attrs attributes for the new element
*
* @returns {Element} newly created SVG element
*/
util._svgNS = 'http://www.w3.org/2000/svg';
util._xlinkNS = 'http://www.w3.org/1999/xlink';
util.genSVGElement = function(name, parent, attrs) {
var elem = document.createElementNS(util._svgNS, name);
if (attrs) {
for (var key in attrs) {
if (attrs.hasOwnProperty(key)) {
if (key.indexOf('xlink') != -1) {
elem.setAttributeNS(
util._xlinkNS, key, attrs[key]
);
} else {
elem.setAttribute(key, attrs[key]);
}
}
}
}
if (parent) {
parent.appendChild(elem);
}
return elem;
}
/**
* Generates (if not generated yet) an SVG element which is
* intended to store common SVG objects to be later reused along
* the application
*
* @returns {Element} common svg element
*/
util._commonSVG = null;
util.getCommonSVG = function() {
if (!util._commonSVGDefs) {
util._commonSVG =
util.genSVGElement('svg', document.body);
util.setStyle(util._commonSVG, {
width : '0px',
height : '0px',
position : 'absolute',
display : 'none'
});
}
return util._commonSVG;
}
/**
* Generates (if not generated yet) an SVG element with a <defs>
* section and returns the <defs> which is intended to store
* common SVG objects to be later reused along the application
*
* @returns {Element} common defs element
*/
util._commonSVGDefs = null;
util.getCommonSVGDefs = function() {
if (!util._commonSVGDefs) {
util._commonSVGDefs = util.genSVGElement(
'defs', util.getCommonSVG()
);
}
return util._commonSVGDefs;
}
/**
* Generates an SVG element containing a linear gradient in the
* given direction
*
* @param {Element} parent parent element (defs)
* @param {String} dir gradient direction (north, east, ...)
* @param {String} id of the gradient to assign
*/
util.genSVGLinearGradient = function(parent, dir, id) {
var gradientAttr = {
x1: '0%',
y1: '0%',
x2: '0%',
y2: '0%'
};
if (dir) {
var full = {
north : 'y2',
east : 'x1',
south : 'y1',
west : 'x2'
};
gradientAttr[full[dir]] = '100%';
}
if (id) {
gradientAttr.id = id;
}
var linearGradient = util.genSVGElement(
'linearGradient', parent, gradientAttr
);
var i, p;
for (i = 0; i <= 10; i++) {
p = ''+((10-i)*(10-i)) + '%';
util.genSVGElement('stop', linearGradient, {
'stop-color': 'rgb('+p+','+p+','+p+')',
offset: ''+(i*10)+'%'
});
}
}
/**
* @returns {Element} the default canvas image ("ashed pages")
* used when an image failed to load for some reason
*/
util._defaultCanvas = null;
util.getDefaultCanvas = function() {
if (!util._defaultCanvas) {
util._defaultCanvas = util._genDefaultCanvas();
}