-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkeybed.js
194 lines (175 loc) · 7.22 KB
/
keybed.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
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
194
class PianoKeybed extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
.keybed-container {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
}
.label-container {
display: flex;
justify-content: space-between;
width: 100%;
font-size: 2vh;
color: white;
margin-bottom: 0.5vh;
}
.instrument-label {
text-align: left;
font-size: 2.3vh;
}
.variant-label {
position: absolute;
text-align: left;
font-size: 1.5vh;
padding-top: 2.2vh;
}
.keybed {
display: flex;
position: relative;
width: 100%;
height: 2em;
padding: 1vh 0;
}
.white-key {
flex: 1;
height: 80%;
border: 0.2vh solid black;
background: white;
position: relative;
}
.black-key {
width: 40%;
height: 67%;
border: 0.2vh solid black;
background: black;
position: absolute;
top: 0;
left: 50%;
transform: translateX(+50%);
z-index: 2;
}
.highlight {
border-color: #000 !important;
background: #F5F !important;
border: 0.2vh solid black;
}
.black-key.highlight {
border-color: #000 !important;
background: #808 !important;
border: 0.2vh solid black;
}
.octave-c {
background: linear-gradient(0deg, rgb(255, 0, 255) 0%, rgba(255, 255, 255, 255) 50%);
}
.octave-c.highlight {
background: linear-gradient(0deg, rgba(255,0,255,1) 0%, rgba(255, 255, 255, 255) 100%) !important;
}
.label {
width: 100%;
text-align: center;
font-size: 1.5vh;
margin-top: -1vh;
color: white;
}
.octave-label {
font-size: 1em;
position: absolute;
color: white;
margin-top: 1.5vh;
text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
z-index: 3;
}
.key-container {
position: relative;
display: flex;
flex-grow: 1;
}
</style>
<div class="keybed-container">
<div class="label-container">
<div class="instrument-label"></div>
<div class="variant-label"></div>
</div>
<div class="keybed"></div>
</div>
`;
}
connectedCallback() {
if (this.hasAttribute('label')) {
this.shadowRoot.querySelector('.instrument-label').innerText = this.getAttribute('label');
} else {
// If there's no label, we still want the variant label to be positioned correctly, so
// we need to move it up.
this.shadowRoot.querySelector('.variant-label').style.marginTop = '-0.7vh';
// We should also move the entire keybed container up to make them more compact.
this.shadowRoot.querySelector('.keybed-container').style.marginBottom = '-1vh';
this.shadowRoot.querySelector('.keybed-container').style.marginTop = '-.5vh';
// And move the variant label up
this.shadowRoot.querySelector('.variant-label').style.paddingTop = '0.1vh';
}
if (this.hasAttribute('variant')) {
this.shadowRoot.querySelector('.variant-label').innerText = this.getAttribute('variant');
}
// Default values
let start = 'C4';
let end = 'C5';
// Set the range of notes to highlight
if (this.hasAttribute('highlight')) {
[start, end] = this.getAttribute('highlight').split('-');
}
// Convert start note name + number to index. Ex: C4 -> 40 or C#4 -> 41
// Get the octave number
let startOctave = start.slice(-1);
let startNote = start.slice(0, -1);
let startNoteIndex = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'].indexOf(startNote);
start = ((parseInt(startOctave)-1) * 12) + startNoteIndex;
// Do the same for the end note
let endOctave = end.slice(-1);
let endNote = end.slice(0, -1);
let endNoteIndex = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'].indexOf(endNote);
end = ((parseInt(endOctave)-1) * 12) + endNoteIndex;
this.renderKeybed(start, end);
}
renderKeybed(start, end ) {
const keybed = this.shadowRoot.querySelector('.keybed');
keybed.innerHTML = '';
const pattern = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
const blackKeyOffsets = { 'C#': true, 'D#': true, 'F#': true, 'G#': true, 'A#': true };
let whiteKeyIndex = 0; // Used for correct positioning of black keys
for (let i = 0; i < 88; i++) {
let note = pattern[i % 12];
let keyContainer = document.createElement('div');
keyContainer.classList.add('key-container');
if (blackKeyOffsets[note]) {
let blackKey = document.createElement('div');
blackKey.classList.add('black-key');
if (i >= start && i <= end) {
blackKey.classList.add('highlight');
}
// Append black key **relative to previous white key**
keybed.children[whiteKeyIndex - 1]?.appendChild(blackKey);
} else {
let whiteKey = document.createElement('div');
whiteKey.classList.add('white-key');
if (note === 'C') {
whiteKey.classList.add('octave-c');
let label = document.createElement('div');
label.classList.add('octave-label');
label.innerText = `${Math.floor(i / 12) + 1}`;
whiteKey.appendChild(label);
}
if (i >= start && i <= end) {
whiteKey.classList.add('highlight');
}
keybed.appendChild(whiteKey);
whiteKeyIndex++; // Increment only when adding white keys
}
}
}
}
customElements.define('piano-keybed', PianoKeybed);