forked from hyperaudio/hyperaudio-lite
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhyperaudio-controls.js
103 lines (94 loc) · 3.04 KB
/
hyperaudio-controls.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
var hyperaudiocontrols = (function () {
var hac = {},
transcriptId,
transcript,
playerId,
player,
resetplay,
resetpause,
playanimId,
playanim,
pauseanimId,
pauseanim;
// https://gist.github.com/mrdoob/838785
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = (function() {
return window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
window.setTimeout( callback, 1000 / 60 );
};
})();
};
function togglePlayer(e) {
var ui = null;
if (e.target.getAttribute('data-m') === null && e.target.id !== playerId && e.target.tagName.toLowerCase() !== 'a') {
if (player.paused) {
ui = playanim;
player.play();
} else {
ui = pauseanim;
player.pause();
};
} else if (e.target.id !== playerId && e.target.tagName.toLowerCase() !== 'a') {
ui = playanim;
};
if (ui !== null) {
var pagex = e.pageX;
var pagey = e.pageY;
if (pagex === undefined) {
var rect = canvas.getBoundingClientRect();
var pagex = e.clientX - rect.left;
var pagey = e.clientY - rect.top;
};
window.requestAnimationFrame(function() {
ui.className = 'icondiv';
ui.style.left = pagex + 'px';
ui.style.top = pagey + 'px';
ui.style.display = '';
window.requestAnimationFrame(function() {
ui.className = 'icondiv icongrow';
if (ui.id === playanimId) {
if (resetplay) {
clearTimeout(resetplay);
};
resetplay = setTimeout(function() {
ui.className = 'icondiv';
ui.style.display = 'none';
}, 510);
};
if (ui.id === pauseanimId) {
if (resetpause) {
clearTimeout(resetpause);
};
resetpause = setTimeout(function() {
ui.className = 'icondiv';
ui.style.display = 'none';
}, 510);
};
});
});
};
};
function playCursor() {
document.documentElement.className = "play";
};
function pauseCursor() {
document.documentElement.className = "pause";
};
hac.init = function(transcriptId, mediaElementId, playId, pauseId) {
transcriptId = transcriptId;
transcript = document.getElementById(transcriptId);
playerId = mediaElementId;
player = document.getElementById(playerId);
playanimId = playId;
playanim = document.getElementById(playanimId);
pauseanimId = pauseId;
pauseanim = document.getElementById(pauseanimId);
document.documentElement.addEventListener("click", togglePlayer, false);
player.addEventListener("canplay", playCursor, false);
player.addEventListener("pause", playCursor, false);
player.addEventListener("playing", pauseCursor, false);
};
return hac;
})();