-
Notifications
You must be signed in to change notification settings - Fork 164
/
app.js
201 lines (188 loc) · 7.75 KB
/
app.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
195
196
197
198
199
200
201
(function() {
const root = document.documentElement;
const fretboard = document.querySelector('.fretboard');
const instrumentSelector = document.querySelector('#instrument-selector');
const accidentalSelector = document.querySelector('.accidental-selector');
const numberOfFretsSelector = document.querySelector('#number-of-frets');
const showAllNotesSelector = document.querySelector('#show-all-notes');
const showMultipleNotesSelector = document.querySelector('#show-multiple-notes');
const noteNameSection = document.querySelector('.note-name-section');
const singleFretMarkPositions = [3, 5, 7, 9, 15, 17, 19, 21];
const doubleFretMarkPositions = [12, 24];
const notesFlat = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
const notesSharp = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
const instrumentTuningPresets = {
'Guitar': [4, 11, 7, 2, 9, 4],
'Bass (4 strings)': [7, 2, 9, 4],
'Bass (5 strings)': [7, 2, 9, 4, 11],
'Ukulele': [9, 4, 0, 7]
};
let allNotes;
let showMultipleNotes = false;
let showAllNotes = false;
let numberOfFrets = 20;
let accidentals = 'flats';
let selectedInstrument = 'Guitar';
let numberOfStrings = instrumentTuningPresets[selectedInstrument].length;
const app = {
init() {
this.setupFretboard();
this.setupinstrumentSelector();
this.setupNoteNameSection();
handlers.setupEventListeners();
},
setupFretboard() {
fretboard.innerHTML = '';
root.style.setProperty('--number-of-strings', numberOfStrings);
// Add strings to fretboard
for (let i = 0; i < numberOfStrings; i++) {
let string = tools.createElement('div');
string.classList.add('string');
fretboard.appendChild(string);
// Create frets
for (let fret = 0; fret <= numberOfFrets; fret++) {
let noteFret = tools.createElement('div');
noteFret.classList.add('note-fret');
string.appendChild(noteFret);
let noteName = this.generateNoteNames((fret + instrumentTuningPresets[selectedInstrument][i]), accidentals);
noteFret.setAttribute('data-note', noteName);
// Add single fret marks
if (i === 0 && singleFretMarkPositions.indexOf(fret) !== -1) {
noteFret.classList.add('single-fretmark');
}
if (i === 0 && doubleFretMarkPositions.indexOf(fret) !== -1) {
let doubleFretMark = tools.createElement('div');
doubleFretMark.classList.add('double-fretmark');
noteFret.appendChild(doubleFretMark);
}
}
}
allNotes = document.querySelectorAll('.note-fret');
},
generateNoteNames(noteIndex, accidentals) {
noteIndex = noteIndex % 12;
let noteName;
if (accidentals === 'flats') {
noteName = notesFlat[noteIndex];
} else if (accidentals === 'sharps') {
noteName = notesSharp[noteIndex];
}
return noteName;
},
setupinstrumentSelector() {
for (instrument in instrumentTuningPresets) {
let instrumentOption = tools.createElement('option', instrument);
instrumentSelector.appendChild(instrumentOption);
}
},
setupNoteNameSection() {
noteNameSection.innerHTML = '';
let noteNames;
if (accidentals === 'flats') {
noteNames = notesFlat;
} else {
noteNames = notesSharp;
}
noteNames.forEach((noteName) => {
let noteNameElement = tools.createElement('span', noteName);
noteNameSection.appendChild(noteNameElement);
});
},
toggleMultipleNotes(noteName, opacity) {
for (let i = 0; i < allNotes.length; i++) {
if (allNotes[i].dataset.note === noteName) {
allNotes[i].style.setProperty('--noteDotOpacity', opacity);
}
}
}
}
const handlers = {
showNoteDot(event) {
// Check if show all notes is selected
if (showAllNotes) {
return;
}
if (event.target.classList.contains('note-fret')) {
if (showMultipleNotes) {
app.toggleMultipleNotes(event.target.dataset.note, 1);
} else {
event.target.style.setProperty('--noteDotOpacity', 1);
}
}
},
hideNoteDot(event) {
if (showAllNotes) {
return;
}
if (showMultipleNotes) {
app.toggleMultipleNotes(event.target.dataset.note, 0);
} else {
event.target.style.setProperty('--noteDotOpacity', 0);
}
},
setSelectedInstrument(event) {
selectedInstrument = event.target.value;
numberOfStrings = instrumentTuningPresets[selectedInstrument].length;
app.setupFretboard();
},
setAccidentals(event) {
if (event.target.classList.contains('acc-select')) {
accidentals = event.target.value;
app.setupFretboard();
app.setupNoteNameSection();
} else {
return;
}
},
setNumberOfFrets() {
numberOfFrets = numberOfFretsSelector.value;
app.setupFretboard();
},
setShowAllNotes() {
showAllNotes = showAllNotesSelector.checked;
if (showAllNotes) {
root.style.setProperty('--noteDotOpacity', 1);
app.setupFretboard();
} else {
root.style.setProperty('--noteDotOpacity', 0);
app.setupFretboard();
}
},
setShowMultipleNotes() {
showMultipleNotes = !showMultipleNotes;
},
setNotesToShow(event) {
let noteToShow = event.target.innerText;
app.toggleMultipleNotes(noteToShow, 1);
},
setNotesToHide(event) {
if (!showAllNotes) {
let noteToHide = event.target.innerText;
app.toggleMultipleNotes(noteToHide, 0);
} else {
return;
}
},
setupEventListeners() {
fretboard.addEventListener('mouseover', this.showNoteDot);
fretboard.addEventListener('mouseout', this.hideNoteDot);
instrumentSelector.addEventListener('change', this.setSelectedInstrument);
accidentalSelector.addEventListener('click', this.setAccidentals);
numberOfFretsSelector.addEventListener('change', this.setNumberOfFrets);
showAllNotesSelector.addEventListener('change', this.setShowAllNotes);
showMultipleNotesSelector.addEventListener('change', this.setShowMultipleNotes);
noteNameSection.addEventListener('mouseover', this.setNotesToShow);
noteNameSection.addEventListener('mouseout', this.setNotesToHide);
}
}
const tools = {
createElement(element, content) {
element = document.createElement(element);
if (arguments.length > 1) {
element.innerHTML = content;
}
return element;
}
}
app.init();
})();