-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvgLoader.src.js
133 lines (117 loc) · 5 KB
/
svgLoader.src.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
/** @preserve
Copyright 2013 Adam van den Hoven
http://littlefyr.com/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function (window) {
'use strict';
var document = window.document,
Math = window.Math;
// Create a path for an annular sector
// https://gist.github.com/4190516
function deg2rad(deg) {
return deg * Math.PI / 180;
}
function annularSector(centerX, centerY, startAngle, endAngle, innerRadius, outerRadius) {
startAngle = deg2rad(startAngle + 180);
endAngle = deg2rad(endAngle + 180);
var p = [
[centerX + innerRadius * Math.cos(startAngle), centerY + innerRadius * Math.sin(startAngle)],
[centerX + outerRadius * Math.cos(startAngle), centerY + outerRadius * Math.sin(startAngle)],
[centerX + outerRadius * Math.cos(endAngle), centerY + outerRadius * Math.sin(endAngle)],
[centerX + innerRadius * Math.cos(endAngle), centerY + innerRadius * Math.sin(endAngle)]
];
var angleDiff = endAngle - startAngle,
largeArc = (angleDiff % (Math.PI * 2)) > Math.PI ? 1 : 0;
var commands = [];
commands.push('M' + p[0].join());
commands.push('L' + p[1].join());
commands.push('A' + [outerRadius, outerRadius].join() + ' 0 ' + largeArc + ' 1 ' + p[2].join());
commands.push('L' + p[3].join());
commands.push('A' + [innerRadius, innerRadius].join() + ' 0 ' + largeArc + ' 0 ' + p[0].join());
commands.push('z');
return commands.join(' ');
}
// Some DOM helpers
function create(type, attr, parent) {
var element = document.createElementNS('http://www.w3.org/2000/svg', type);
if (attr){
for (var name in attr) {
element.setAttribute(name, attr[name]);
}
}
if (parent) {
parent.appendChild(element);
}
return element;
}
function svgLoader(container, height, width, sections, arcSize, innerRadius, outerRadius, color, opacity, cwTime, ccwTime){
height = parseInt(height, 10);
width = parseInt(width, 10);
var centerY = height/2,
centerX = width/2,
svg = create('svg', {
width: width+'px',
height: height+'px',
x: '50%',
y: '50%',
viewBox: '0 0 height width',
preserveAspectRatio: 'xMidYMid meet'
}, create('g', {transform: 'translate(-' + centerX.toString(10) +',-' + centerY.toString(10) +')'},create('svg', {height:'100%', width:'100%', version:'1.1'}, container))),
cw = create('g',{}, svg),
ccw = create('g', {}, svg),
section = arcSize/sections,
circle0 = [0, centerX, centerY].join(' '),
circle360 = [360, centerX, centerY].join(' '),
i;
for(i=0; i<sections; i++){
create('path', {
fill: color,
'fill-opacity': opacity*i/sections,
d: annularSector(centerX, centerY, section*i, section*(i+1), innerRadius, outerRadius)
}, cw);
create('path', {
fill: color,
'fill-opacity': opacity*i/sections,
d: annularSector(centerX, centerY, -section*i, -section*(i+1), innerRadius, outerRadius)
}, ccw);
}
create('animateTransform', {
attributeType: 'xml',
attributeName: 'transform',
type: 'rotate',
from: circle0,
to: circle360,
dur: cwTime,
repeatCount: 'indefinite'
}, cw);
create('animateTransform', {
attributeType: 'xml',
attributeName: 'transform',
type: 'rotate',
to: circle0,
from: circle360,
dur: ccwTime,
repeatCount: 'indefinite'
}, ccw);
}
window.svgLoader = svgLoader;
if ( typeof define === "function" ) {
define( "svgLoader", [], function () { return svgLoader; } );
}
}(window));