-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
159 lines (145 loc) · 6.3 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Raphaël · Polar Clock</title>
<link rel="stylesheet" href="demo.css" media="screen">
<link rel="stylesheet" href="demo-print.css" media="print">
<script src="raphael.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
var startDate;
var currentState = 0; //0: stop, 1: start, 2: pause
var timeElapsed = 0;
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
$(document).ready(function () {
var r = Raphael("holder", 600, 600);
var R = 200;
var totalDuration = 0;
var timeElapsed = 0;
var timePreviouslyElapsed = 0;
// Custom Attribute
r.customAttributes.arc = function (value, total, R) {
var alpha;
if (value == 0 || total == 0) {
alpha = 0;
} else {
alpha = 360 / total * value;
}
var a = (90 - alpha) * Math.PI / 180,
x = 300 + R * Math.cos(a),
y = 300 - R * Math.sin(a),
path;
path = [["M", 300, 300 - R], ["A", R, R, 0, +(alpha > 180), 1, x, y]];
return {path: path};
};
r.customAttributes.dot = function (value, total, R) {
var alpha;
if (value == 0 || total == 0) {
alpha = 0;
} else {
alpha = 360 / total * value;
}
var a = (90 - alpha) * Math.PI / 180,
x = 300 + R * Math.cos(a),
y = 300 - R * Math.sin(a),
color = "rgb(255, 0, 0)";
return {cx: x, cy: y, r:15, stroke: color, fill: color};
};
r.customAttributes.duration = function (seconds) {
var hours = Math.floor(seconds / 60);
var seconds = Math.ceil(seconds - hours * 60);
return {text: pad(hours, 2) + ":" + pad(seconds, 2), "font-size": 100};
};
function updateVal(timeElapsed, totalDuration, R, hand, circle, duration, state) {
hand.attr({arc: [timeElapsed, totalDuration, R]});
circle.attr({dot: [timeElapsed, totalDuration, R]});
duration.attr({duration: totalDuration-timeElapsed});
if (currentState == 0) {
state.attr({text: 'Tap to start'});
} else if (currentState == 1) {
state.attr({text: 'Tap to pause'});
} else if (currentState == 2) {
state.attr({text: 'Tap to resume'});
}
}
var backarc = r.path().attr({fill: "#fff", stroke: "#0f0", "stroke-width": 10}).attr({arc: [59.999, 60, R]});
var sec = r.path().attr({fill: "#fff", stroke: "#f00", "stroke-width": 10}).attr({arc: [0, 59.999, R]});
var circle = r.circle().attr({dot: [0, 59.999, R]});
var duration = r.text(300, 300).attr({duration: [0]});
var state = r.text(300, 400, "Tap to start").attr({"font-size": 30});
// var c = r.circle(250, 250, 40);
(function () {
if (currentState == 1) {
timeElapsed = (Date.now()-startDate.getTime()) / 1000;
var totalTimeElapsed = timePreviouslyElapsed+timeElapsed;
if (totalTimeElapsed < totalDuration) {
updateVal(totalTimeElapsed, totalDuration, 200, sec, circle, duration, state);
} else {
currentState = 0;
updateVal(totalDuration, totalDuration, 200, sec, circle, duration, state);
}
}
setTimeout(arguments.callee, 1000*1/24);
})();
$(".btn-time-update").click(function() {
var val = parseInt($(this).attr('value'));
if (val > 0) {
totalDuration += val;
duration.attr({duration: totalDuration});
}
});
$("#btn-reset").click(function() {
var val = 0;
totalDuration += 0;
});
$("#btn-stop").click(function() {
currentState = 0;
updateVal(0, 0, 200, sec, circle, duration, state);
});
function updateState() {
if (totalDuration > 0) {
var val = 0;
if (currentState == 0) { //stop
console.log("currentState"+currentState);
startDate = new Date();
currentState = 1;
timePreviouslyElapsed = 0;
timeElapsed = 0;
} else if (currentState == 1) { // pause
timePreviouslyElapsed += timeElapsed;
state.attr({text: 'Tap to pause'});
currentState = 2;
state.attr({text: 'Tap to resume'});
} else if (currentState == 2) { //resume
startDate = new Date();
currentState = 1;
}
}
}
$("#holder").click(function() {
updateState();
});
});
</script>
<style media="screen">
#holder {
height: 600px;
/*margin: -300px 0 0 -300px;*/
width: 600px;
}
tspan {
pointer-events: none;
}
</style>
</head>
<body>
<div id="holder"></div>
<button id="btn-stop">stop</button>
<button class="btn-time-update" value="-15">-15</button><button class="btn-time-update" value="-10">-10</button><button class="btn-time-update" value="-5">-5</button><button id="btn-reset">reset</button><button class="btn-time-update" value="5">+5</button><button class="btn-time-update" value="10">+10</button><button class="btn-time-update" value="15">+15</button>
</body>
</html>