-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.js
77 lines (67 loc) · 2.12 KB
/
run.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
"use strict";
let notation = localStorage.getItem( "gammes-notation" ) || "doremi";
let majorMinor = localStorage.getItem( "gammes-majorMinor" ) || "major";
let diezeBemol = localStorage.getItem( "gammes-diezeBemol" ) || "♯";
document.body.prepend( ...GSUgetTemplate( "main", { notation, majorMinor, diezeBemol } ) );
const form = document.querySelector( "#form" );
const scalesGraph = document.querySelector( "#scalesGraph" );
const keys = document.querySelectorAll( ".key" );
const scales = {
major: [ 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 ],
minor: [ 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0 ],
};
const notations = {
"♯": {
CDE: [ "C", "C♯", "D", "D♯", "E", "F", "F♯", "G", "G♯", "A", "A♯", "B" ],
doremi: [ "Do", "Do♯", "Ré", "Ré♯", "Mi", "Fa", "Fa♯", "Sol", "Sol♯", "La", "La♯", "Si" ],
},
"♭": {
CDE: [ "C", "D♭", "D", "E♭", "E", "F", "G♭", "G", "A♭", "A", "B♭", "B" ],
doremi: [ "Do", "Ré♭", "Ré", "Mi♭", "Mi", "Fa", "Sol♭", "Sol", "La♭", "La", "Si♭", "Si" ],
},
};
let gamme = 0;
scalesGraph.onclick = e => {
const k = e.target.parentNode.dataset.key;
if ( k ) {
gamme = +k;
scalesGraph.style.setProperty( "--gamme", gamme );
activeKeys();
}
};
form.onchange = e => {
const val = e.target.value;
switch ( e.target.name ) {
case "majorMinor":
localStorage.setItem( "gammes-majorMinor", val );
majorMinor = val;
scalesGraph.dataset.scaleM = val;
activeKeys();
break;
case "notation":
localStorage.setItem( "gammes-notation", val );
notation = val;
updateKeysName();
break;
case "diezeBemol":
localStorage.setItem( "gammes-diezeBemol", val );
diezeBemol = val;
updateKeysName();
break;
}
};
function activeKeys() {
const arr = scales[ majorMinor ];
keys.forEach( ( el, i ) => {
el.classList.toggle( "key-selected", i === gamme );
el.classList.toggle( "key-active", !!arr[ ( 12 + i - gamme ) % 12 ] );
} );
}
function updateKeysName() {
const arr = notations[ diezeBemol ][ notation ];
keys.forEach( ( el, i ) => {
el.firstChild.textContent = arr[ i ];
} );
}
activeKeys();
updateKeysName();