-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample-two-notes.html
169 lines (150 loc) · 4.46 KB
/
example-two-notes.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
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<style>
* {
user-select: none;
}
body {
background: #333;
color: snow;
width: 100vw;
height: 100vh;
margin: 0;
font-family: avenir, arial, monospace;
text-align: center;
}
#main {
background: #5e4f40;
align-content: center;
border-radius: 1em;
}
#sound-icon {
color: #335;
font-size: 10em;
position: fixed;
left: calc(50% - 0.5em);
top: calc(50% - 0.4em);
}
footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
padding-bottom: 1em;
}
a {
color: snow;
}
a:hover {
color: lime;
}
#other-note {
position: absolute;
top: 1em;
left: 1em;
z-index: 1;
padding: 10px;
border-radius: 10px;
}
</style>
</head>
<!-- USE _2DNote! -->
<body
onclick="playOrStop(event)"
onmousemove="instructToClickToStop();_2DNote.update(event,colourSoundIcon);"
onmouseleave="exitedView();"
>
<button id="other-note">Other note</button>
<div id="main" role="main">
<h1 id="audio-reminder" aria-hidden="true">Make sure audio is on</h1>
<p id="instruction" aria-live="polite"></p>
</div>
<i id="sound-icon" class="fas fa-music"></i>
<!-- INCLUDE _2DNote! -->
<script
src="https://cdn.jsdelivr.net/gh/hchiam/_2DNote@1.12.3/_2DNote.min.js"
integrity="sha384-e0d2dNwg3F9WTJ3jZBF5iUeuVyAtx+zwMnCAvKMiCHtwO2l2dzo3cIMO4+Xqwn5p"
crossorigin="anonymous"
></script>
<!-- <script src="_2DNote.min.js"></script> -->
<!-- USE _2DNote! -->
<script>
setTimeout(function () {
document.getElementById("instruction").innerHTML =
"Click anywhere to start";
}, 1000);
let on = false;
const other_2DNote = _2DNote.copy();
setInterval(function () {
if (!on) return;
const element = document.getElementById("other-note");
const h = document.documentElement.clientHeight;
const w = document.documentElement.clientWidth;
element.style.left = Math.random() * (w - element.offsetWidth);
element.style.top = Math.random() * (h / 2 - element.offsetHeight);
other_2DNote.update(element);
}, 1000);
function playOrStop(e) {
if (on) {
on = false;
_2DNote.stop();
other_2DNote.stop();
instructToClickToStart();
} else {
on = true;
hideAudioReminder();
instructToMoveMouse();
_2DNote.play(e);
other_2DNote.play(document.getElementById("other-note"));
}
toggleSoundIcon();
}
function exitedView() {
on = false;
_2DNote.stop();
other_2DNote.stop();
instructToClickToStart();
}
function instructToMoveMouse() {
document.getElementById("instruction").innerHTML =
'Move mouse to play with <span style="color: lime;">volume ↕</span> and <span style="color: lime;">pitch ↔️</span>';
}
function hideAudioReminder() {
document.getElementById("audio-reminder").style.display = "none";
}
function instructToClickToStart() {
document.getElementById("instruction").innerHTML =
"Click anywhere to start";
}
function instructToClickToStop() {
if (on) {
document.getElementById("instruction").innerHTML =
'Move mouse to play with <span style="color: lime;">volume ↕</span> and <span style="color: lime;">pitch ↔️</span> <br/><span style="color: lime;">Click anywhere to stop</span>';
}
}
function toggleSoundIcon() {
if (on) {
document.getElementById("sound-icon").style.visibility = "visible";
} else {
document.getElementById("sound-icon").style.visibility = "hidden";
}
}
function colourSoundIcon(volume, frequency) {
const comfyFrequencyRange = [150, 400];
const hueRange = [0, 359];
let hue = _2DNote.normalize(frequency, comfyFrequencyRange, hueRange);
hue = Math.round(hue);
const comfyVolumeRange = [0, 0.5];
const saturationRange = [0, 100];
let saturation = _2DNote.normalize(
volume,
comfyVolumeRange,
saturationRange
);
saturation = Math.round(saturation);
const hsl = "hsl(" + String(hue) + "," + String(saturation) + "%, 50%)";
document.getElementById("sound-icon").style.color = hsl;
}
</script>
</body>