-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
79 lines (65 loc) · 2.03 KB
/
timer.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
var $ = require('jquery');
var d3 = require('d3');
var SEC = 1000;
var TWOPI = 2 * Math.PI;
function Timer (options) {
if (!options.interval) {
throw new Error('Please set an interval');
}
this.$el = $('<div/>', { class: 'component-timer' });
this.interval = options.interval;
this.timeLeft = options.interval;
this.progressWidth = options.progressWidth || 5;
this.outerRadius = options.outerRadius || this.$el.height() / 2;
this.innerRadius = this.outerRadius - this.progressWidth;
this.d3Container = d3.select(this.$el[0]);
this.svg = this.d3Container.append('svg').style('width', this.outerRadius * 2);
this._completeCallback = options.onComplete || function () {};
this._initialDraw();
}
Timer.prototype._initialDraw = function () {
this.group = this.svg.append('g').attr(
'transform',
'translate(' + this.outerRadius + ',' + this.outerRadius +')'
);
this.group.append('path').attr('fill', '#eee');
this._updatePath(1);
var arc = d3.svg.arc().innerRadius(this.innerRadius).outerRadius(this.outerRadius);
this.group.append('text')
.text(this.timeLeft)
.attr('fill', '#eee')
.attr('text-anchor', 'middle')
.attr('alignment-baseline', 'middle');
};
Timer.prototype.start = function() {
this.timeout = setTimeout(function () {
this._decrementTime();
this._updatePath(this.timeLeft / this.interval);
}.bind(this), SEC);
};
Timer.prototype.stop = function () {
if (this.timeout) clearTimeout(this.timeout);
};
Timer.prototype._decrementTime = function() {
this.timeLeft--;
$(this).trigger('step', this.timeLeft);
var text = this.group.select('text');
text.text(this.timeLeft);
if (this.timeLeft === 0) {
this._completeCallback();
} else {
this.start();
}
};
Timer.prototype._updatePath = function (ratioLeft) {
var path = this.group.select('path');
path.attr(
'd',
d3.svg.arc()
.startAngle(TWOPI)
.endAngle(TWOPI * (1 - ratioLeft))
.innerRadius(this.innerRadius)
.outerRadius(this.outerRadius)
);
};
module.exports = Timer;