-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
executable file
·193 lines (163 loc) · 6.72 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!doctype html>
<html lang=en-us>
<head>
<meta charset=utf-8>
<title>Circle player (proof-of-concept)</title>
<style>
*,
*:after,
*:before {
box-sizing:border-box;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-font-smoothing:antialiased;
-moz-font-smoothing:antialiased;
-o-font-smoothing:antialiased;
font-smoothing:antialiased;
text-rendering:optimizeLegibility;
}
#playable .control { opacity: 0; /* transition: opacity .2s linear; */ }
#playable.not-started .play, #playable.paused .play { opacity: 1; }
#playable.playing .pause { opacity: 1; }
#playable.ended .stop {opacity: 1; }
#playable.precache-bar .done {opacity: 0; }
#playable.not-started .progress-bar, #playable.ended .progress-bar { display: none; }
#playable.ended .progress-track { stroke-opacity: 1; }
#playable .progress-bar,
#playable .precache-bar {
stroke-dasharray: 298.1371428256714;
stroke-dashoffset: 298.1371428256714;
}
</style>
</head>
<body>
<p>When you press play, the sample should pre-load and show progress on the graph in dark grey. As the file plays, the circle fills with black.</p>
<p>The size is set to 50% of the window - resize to see.</p>
<audio src="" preload="none" id="listen"></audio>
<svg id="playable" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" width="50%" height="50%" class="not-started">
<g class="shape">
<circle class="progress-track" cx="50" cy="50" r="47.45" stroke="#000000" stroke-opacity="0.25" stroke-linecap="round" fill="none" stroke-width="1" />
<circle class="precache-bar" cx="50" cy="50" r="47.45" stroke="#000000" stroke-opacity="0.25" stroke-linecap="round" fill="none" stroke-width="1" transform="rotate(-90 50 50)" />
<circle class="progress-bar" cx="50" cy="50" r="47.45" stroke="#000000" stroke-opacity="1" stroke-linecap="round" fill="none" stroke-width="1" transform="rotate(-90 50 50)" />
</g>
<circle class="controls" cx="50" cy="50" r="45" stroke="none" fill="#000000" opacity="0.0" pointer-events="all" />
<g class="control pause">
<line x1="40" y1="35" x2="40" y2="65" stroke="#000000" fill="none" stroke-width="1" stroke-linecap="round" />
<line x1="60" y1="35" x2="60" y2="65" stroke="#000000" fill="none" stroke-width="1" stroke-linecap="round" />
</g>
<g class="control play">
<line x1="45" y1="35" x2="45" y2="65" stroke="#000000" fill="none" stroke-width="1" stroke-linecap="round" />
<line x1="45" y1="65" x2="65" y2="50" stroke="#000000" fill="none" stroke-width="1" stroke-linecap="round" />
<line x1="65" y1="50" x2="45" y2="35" stroke="#000000" fill="none" stroke-width="1" stroke-linecap="round" />
</g>
<g class="control stop">
<rect x="35" y="35" width="30" height="30" stroke="#000000" fill="none" stroke-width="1" />
</g>
</svg>
<script>
var playObj = document.getElementById("playable"),
progress = playObj.querySelector(".progress-bar"),
precache = playObj.querySelector(".precache-bar"),
audioObj = document.getElementById("listen"),
controlsObj = playObj.querySelector(".controls"),
pt = playObj.createSVGPoint(),
pc = 298.1371428256714; // 2 pi r
function cursorPoint(evt){
pt.x = evt.clientX; pt.y = evt.clientY;
return pt.matrixTransform(playObj.getScreenCTM().inverse());
}
function angle(ex, ey) {
var dy = ey - 50; // 100;
var dx = ex - 50; // 100;
var theta = Math.atan2(dy, dx); // range (-PI, PI]
theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
theta = theta + 90; // in our case we are animating from the top, so we offset by the rotation value;
if (theta < 0) theta = 360 + theta; // range [0, 360)
return theta;
}
// nice long audio for precache testing
//audioObj.setAttribute("src", "http://media.blubrry.com/stilluntitledwithadamsavage/files.tested.com/podcast/stilluntitled-20150922.mp3?" + Math.random());
audioObj.setAttribute("src", "http://frumbert.org/mp3/half%20life.mp3?" + Math.random());
// https://github.com/toddmotto/lunar/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory;
} else {
root.lunar = factory();
}
})(this, function () {
'use strict';
var lunar = {};
lunar.hasClass = function (elem, name) {
return new RegExp('(\\s|^)' + name + '(\\s|$)').test(elem.getAttribute('class'));
};
lunar.addClass = function (elem, name) {
!lunar.hasClass(elem, name) && elem.setAttribute('class', (!!elem.getAttribute('class') ? elem.getAttribute('class') + ' ' : '') + name);
};
lunar.removeClass = function (elem, name) {
var remove = elem.getAttribute('class').replace(new RegExp('(\\s|^)' + name + '(\\s|$)', 'g'), '$2');
lunar.hasClass(elem, name) && elem.setAttribute('class', remove);
};
lunar.toggleClass = function (elem, name) {
lunar[lunar.hasClass(elem, name) ? 'removeClass' : 'addClass'](elem, name);
};
lunar.className = function (elem, name) {
elem.setAttribute("class", name);
console.log("className", elem);
}
return lunar;
});
function setGraphValue(obj, val) {
var val = pc - parseFloat(((val / audioObj.duration) * pc), 10);
obj.style.strokeDashoffset = val;
if (val === 0) {
lunar.addClass(obj,"done");
if (obj===progress) lunar.className(playObj, "ended");
}
}
audioObj.addEventListener('progress', function() {
var end = audioObj.buffered.end(audioObj.buffered.length - 1);
setGraphValue(precache, end);
});
function reportPosition() {
setGraphValue(progress, audioObj.currentTime);
}
function positionListener(event) {
// console.log("a",Math.sqrt((pc-loc.x)*(pc-loc.x) + (pc-loc.y)* (pc-loc.y)));
var loc = cursorPoint(event),
deg = (angle(loc.x,loc.y) / 360),
pct = pc * deg;
console.log(loc, deg);
// doo doo doo don't mind me, this code does nothing yet ...
}
// idea:
// use polar co ordinate conversion and convert the position as a percentage of 360 degrees... and draw it as an arc rather than a circle
// rather than extending the length of the dash
// http://stackoverflow.com/a/24569190/1238884
controlsObj.addEventListener("click", function(e) {
switch (playObj.getAttribute("class")) {
case "not-started":
audioObj.addEventListener('timeupdate', reportPosition);
precache.addEventListener("mousedown", positionListener, false);
audioObj.play();
playObj.setAttribute("class", "playing");
break;
case "playing":
playObj.setAttribute("class", "paused");
audioObj.pause();
break;
case "paused":
playObj.setAttribute("class", "playing");
audioObj.play();
break;
case "ended":
playObj.setAttribute("class", "not-started");
audioObj.removeEventListener('timeupdate', reportPosition);
break;
}
});
</script>
</body>
</html>