-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcountdown.js
135 lines (113 loc) · 2.8 KB
/
countdown.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
const urlParams = new URLSearchParams(window.location.search);
var timeLimit = 1
if (urlParams.has('timer')) {
document.getElementById("timer").classList.remove('hide-timer')
timeLimit = urlParams.get('timer')
}
var width = 350,
height = 350,
timePassed = 0;
var fields = [{
value: timeLimit,
size: timeLimit,
update: function() {
return timePassed = timePassed + 1;
}
}];
var nilArc = d3.svg.arc()
.innerRadius(width / 3 - 133)
.outerRadius(width / 3 - 133)
.startAngle(0)
.endAngle(2 * Math.PI);
var arc = d3.svg.arc()
.innerRadius(width / 3 - 55)
.outerRadius(width / 3 - 25)
.startAngle(0)
.endAngle(function(d) {
return ((d.value / d.size) * 2 * Math.PI);
});
var svg = d3.select(".timer").append("svg")
.attr("width", width)
.attr("height", height);
var field = svg.selectAll(".field")
.data(fields)
.enter().append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.attr("class", "field");
var back = field.append("path")
.attr("class", "path path--background")
.attr("d", arc);
var path = field.append("path")
.attr("class", "path path--foreground");
var label = field.append("text")
.attr("class", "label")
.attr("dy", ".35em");
function update() {
field
.each(function(d) {
d.previous = d.value, d.value = d.update(timePassed);
});
path.transition()
.ease("elastic")
.duration(500)
.attrTween("d", arcTween);
if ((timeLimit - timePassed) <= 10)
pulseText();
else
label
.text(function(d) {
return d.size - d.value;
});
if (timePassed <= timeLimit)
// setTimeout(update, 1000 - (timePassed % 1000));
// The above is the original but it seems very wrong
setTimeout(update, 1000);
else
destroyTimer();
};
function pulseText() {
back.classed("pulse", true);
label.classed("pulse", true);
if ((timeLimit - timePassed) >= 0) {
label.style("font-size", "90px")
.attr("transform", "translate(0," + +4 + ")")
.text(function(d) {
return d.size - d.value;
});
}
label.transition()
.ease("elastic")
.duration(900)
.style("font-size", "80px")
.attr("transform", "translate(0," + -10 + ")");
}
function destroyTimer() {
label.transition()
.ease("back")
.duration(700)
.style("opacity", "0")
.style("font-size", "5")
.attr("transform", "translate(0," + -40 + ")")
.each("end", function() {
field.selectAll("text").remove()
});
path.transition()
.ease("back")
.duration(700)
.attr("d", nilArc);
back.transition()
.ease("back")
.duration(700)
.attr("d", nilArc)
.each("end", function() {
field.selectAll("path").remove()
});
}
function arcTween(b) {
var i = d3.interpolate({
value: b.previous
}, b);
return function(t) {
return arc(i(t));
};
}