-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.html
132 lines (109 loc) · 3 KB
/
timer.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
<!doctype html>
<html>
<head>
<meta charset="utf8">
<title>sketch</title>
<style>
body, #root {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="root">
<svg></svg>
<button id="toggle">start</button>
<button id="reset">reset</button>
</div>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script>
// Playing around with a timer
function timer (callback, duration, controlled = false) {
let active = false
let elapsed = null
function start () {
if (active) return; // Cannot start more than once
active = true
// If we have an elapsed time, we had previously paused so subtract
// already elapsed time so we only count down the remaining time
const now = performance.now()
const startTime = elapsed ? (now - elapsed) : now
requestAnimationFrame(function render (now) {
if (!active) return;
elapsed = now - startTime
if (elapsed < duration) {
callback(elapsed / duration, elapsed)
requestAnimationFrame(render)
} else {
callback(1, duration)
reset()
}
})
}
function pause () {
active = false
}
function reset () {
active = false
elapsed = null
}
if (!controlled) {
start()
}
return { start, pause, reset }
}
let active = false
const width = 900
const height = 600
const toggle = d3.select('#toggle')
const reset = d3.select('#reset')
const svg = d3.select('svg')
.attr('width', width)
.attr('height', height)
svg.append('path')
.attr('d', `M0,${height / 2}L${width},${height / 2}`)
.style('stroke', '#ccc')
.style('stroke-width', 2)
.style('stroke-dasharray', '8,8')
const g = svg.append('g')
const circle = g.append('circle')
.attr('cx', 20)
.attr('cy', height / 2)
.attr('r', 20)
.style('fill', 'black')
const text = g.append('text')
.attr('x', 20)
.attr('y', height / 2)
.attr('dy', 5)
.style('text-anchor', 'middle')
.text('')
const a = height / 4
const b = 4 * Math.PI
const xRange = width - 40
const tween = timer((t, elapsed) => {
g.attr('transform', `translate(${t * xRange}, ${a * Math.sin(t * b)})`)
circle.style('fill', `hsl(${(t * 360)}, 100%, 75%)`)
text.text(~~elapsed)
if (t === 1) toggle.text('start').style('display', 'none')
}, 5000, true)
toggle.on('click', e => {
const action = active ? 'pause' : 'start'
const text = active ? 'start' : 'pause'
tween[action]()
toggle.text(text)
active = !active
})
reset.on('click', e => {
toggle.text('start').style('display', '')
g.attr('transform', null)
circle.style('fill', 'black')
text.text('')
tween.reset()
active = false
})
</script>
</body>
</html>