forked from kimmobrunfeldt/progressbar.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressbar.js
More file actions
314 lines (251 loc) · 9.23 KB
/
progressbar.js
File metadata and controls
314 lines (251 loc) · 9.23 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
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define('progressbar', [], function() {
return factory();
});
} else {
// Browser globals
root.ProgressBar = factory();
}
}(this, function() {
var oldTweenable = this.Tweenable;
// The next line will be replaced with minified version of shifty library
// in a build step. Don't expose Tweenable to global scope
// #include shifty
var Tweenable = this.Tweenable;
this.Tweenable = oldTweenable;
var EASING_ALIASES = {
easeIn: 'easeInCubic',
easeOut: 'easeOutCubic',
easeInOut: 'easeInOutCubic'
};
// Base object for different progress bar shapes
var Progress = function(container, opts) {
// Prevent calling constructor without parameters so inheritance
// works correctly
if (arguments.length === 0) return;
var svgView = this._createSvgView(opts);
var element;
if (isString(container)) {
element = document.querySelector(container);
} else {
element = container;
}
element.appendChild(svgView.svg);
var newOpts = extend({
attachment: this
}, opts);
this._path = new Path(svgView.path, newOpts);
// Expose public attributes
this.path = svgView.path;
this.trail = svgView.trail;
};
Progress.prototype.animate = function animate(progress, opts, cb) {
this._path.animate(progress, opts, cb);
};
Progress.prototype.stop = function stop() {
this._path.stop();
};
Progress.prototype.set = function set(progress) {
this._path.set(progress);
};
Progress.prototype._createSvgView = function _createSvgView(opts) {
opts = extend({
color: "#555",
strokeWidth: 1.0,
trailColor: null,
fill: null
}, opts);
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
this._initializeSvg(svg, opts);
var trailPath = null;
if (opts.trailColor) {
var trailOpts = extend({}, opts);
trailOpts.color = opts.trailColor;
// When trail path is set, fill must be set for it instead of the
// actual path to prevent trail stroke from clipping
opts.fill = null;
trailPath = this._createPath(trailOpts);
svg.appendChild(trailPath);
}
var path = this._createPath(opts);
svg.appendChild(path);
return {
svg: svg,
path: path,
trail: trailPath
};
};
Progress.prototype._initializeSvg = function _initializeSvg(svg, opts) {
svg.setAttribute("viewBox", "0 0 100 100");
};
Progress.prototype._createPath = function _createPath(opts) {
var path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", this._pathString(opts));
path.setAttribute("stroke", opts.color);
path.setAttribute("stroke-width", opts.strokeWidth);
if (opts.fill) {
path.setAttribute("fill", opts.fill);
} else {
path.setAttribute("fill-opacity", "0");
}
return path;
};
Progress.prototype._pathString = function _pathString(opts) {
throw new Error("Override this function for each progress bar");
};
// Progress bar shapes
var Line = function(container, options) {
Progress.apply(this, arguments);
};
Line.prototype = new Progress();
Line.prototype.constructor = Line;
Line.prototype._initializeSvg = function _initializeSvg(svg, opts) {
svg.setAttribute("viewBox", "0 0 100 " + opts.strokeWidth);
svg.setAttribute("preserveAspectRatio", "none");
};
Line.prototype._pathString = function _pathString(opts) {
var pathString = "M 0,{c} L 100,{c}";
var center = opts.strokeWidth / 2;
pathString = pathString.replace(/\{c\}/g, center);
return pathString;
};
var Circle = function(container, options) {
Progress.apply(this, arguments);
};
Circle.prototype = new Progress();
Circle.prototype.constructor = Circle;
Circle.prototype._pathString = function _pathString(opts) {
// Use two arcs to form a circle
// See this answer http://stackoverflow.com/a/10477334/1446092
var pathString = "M 50,50 m 0,-{r} a {r},{r} 0 1 1 0,{r*2} a {r},{r} 0 1 1 0,-{r*2}";
var r = 50 - opts.strokeWidth / 2;
pathString = pathString.replace(/\{r\}/g, r);
pathString = pathString.replace(/\{r\*2\}/g, r * 2);
return pathString;
};
var Square = function(container, options) {
Progress.apply(this, arguments);
};
Square.prototype = new Progress();
Square.prototype.constructor = Square;
Square.prototype._pathString = function _pathString(opts) {
var pathString = "M 0,{s/2} L {w},{s/2} L {w},{w} L {s/2},{w} L {s/2},{s}";
var w = 100 - opts.strokeWidth / 2;
pathString = pathString.replace(/\{w\}/g, w);
pathString = pathString.replace(/\{s\}/g, opts.strokeWidth);
pathString = pathString.replace(/\{s\/2\}/g, opts.strokeWidth / 2);
return pathString;
};
// Lower level API to animate any kind of svg path
var Path = function(path, opts) {
opts = extend({
duration: 800,
easing: "linear",
from: {},
to: {},
step: noop
}, opts);
this._path = path;
this._opts = opts;
this._tweenable = null;
// Set up the starting positions
var length = this._path.getTotalLength();
this._path.style.strokeDasharray = length + ' ' + length;
this._path.style.strokeDashoffset = length;
};
Path.prototype.set = function set(progress) {
this.stop();
var length = this._path.getTotalLength();
this._path.style.strokeDashoffset = length - progress * length;
};
Path.prototype.stop = function stop() {
this._stopTween();
var computedStyle = window.getComputedStyle(this._path, null);
var offset = computedStyle.getPropertyValue('stroke-dashoffset');
this._path.style.strokeDashoffset = offset;
};
// Method introduced here:
// http://jakearchibald.com/2013/animated-line-drawing-svg/
Path.prototype.animate = function animate(progress, opts, cb) {
if (isFunction(opts)) {
cb = opts;
opts = {};
}
// Copy default opts to new object so defaults are not modified
var defaultOpts = extend({}, this._opts);
opts = extend(defaultOpts, opts);
this.stop();
// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
this._path.getBoundingClientRect();
var computedStyle = window.getComputedStyle(this._path, null);
var offset = computedStyle.getPropertyValue('stroke-dashoffset');
// Remove 'px' suffix
offset = parseFloat(offset, 10);
var length = this._path.getTotalLength();
var newOffset = length - progress * length;
var self = this;
this._tweenable = new Tweenable();
this._tweenable.tween({
from: extend({ offset: offset }, opts.from),
to: extend({ offset: newOffset }, opts.to),
duration: opts.duration,
easing: this._easing(opts.easing),
step: function(state) {
self._path.style.strokeDashoffset = state.offset;
opts.step(state, opts.attachment);
},
finish: function(state) {
// step function is not called on the last step of animation
self._path.style.strokeDashoffset = state.offset;
opts.step(state, opts.attachment);
if (isFunction(cb)) {
cb();
}
}
});
};
Path.prototype._stopTween = function _stopTween() {
if (this._tweenable !== null) {
this._tweenable.stop();
this._tweenable.dispose();
this._tweenable = null;
}
};
Path.prototype._easing = function _easing(easing) {
if (EASING_ALIASES.hasOwnProperty(easing)) {
return EASING_ALIASES[easing];
}
return easing;
};
// Utility functions
function noop() {}
// Copy all attributes from source object to destination object.
// destination object is mutated.
function extend(destination, source) {
destination = destination || {};
source = source || {};
for (var attrName in source) {
if (source.hasOwnProperty(attrName)) {
destination[attrName] = source[attrName];
}
}
return destination;
}
function isString(obj) {
return typeof obj === 'string' || obj instanceof String;
}
function isFunction(obj) {
return typeof obj === "function";
}
// Expose modules
var ProgressBar = {
Line: Line,
Circle: Circle,
Square: Square,
Path: Path
};
return ProgressBar;
}));