-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (50 loc) · 1.34 KB
/
index.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
var canvas = document.getElementById("stopwatch");
var ctx = canvas.getContext("2d");
var startTime;
var running = false;
function drawCanvas() {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "blue";
ctx.fillRect(40, 50, 500, 300);
ctx.fillStyle = "white";
ctx.font = "30px Arial";
var elapsedTime = running ? (Date.now() - startTime) : 0;
var minutes = Math.floor(elapsedTime / 60000);
var seconds = Math.floor((elapsedTime % 60000) / 1000);
var milliseconds = elapsedTime % 1000;
ctx.fillText(
pad(minutes) + ":" + pad(seconds) + ":" + padMilliseconds(milliseconds),
235,
200
);
}
function pad(value) {
return value < 10 ? "0" + value : value;
}
function padMilliseconds(value) {
return ("00" + value).slice(-2);
}
function startStopwatch() {
if (!running) {
startTime = Date.now();
running = true;
updateStopwatch();
}
}
function pauseStopwatch() {
running = false;
}
function restartStopwatch() {
if (!running) {
startTime = Date.now();
drawCanvas();
}
}
function updateStopwatch() {
if (running) {
drawCanvas();
requestAnimationFrame(updateStopwatch);
}
}
drawCanvas();