-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
108 lines (91 loc) · 3.2 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>MIDI Color Mixer</title>
<style>
#colorPreview {
width: 250px;
height: 250px;
border: 1px #000 solid;
margin: 2em auto;
background: rgb(0, 0, 0);
}
body {
font-family: system-ui, sans-serif;
}
</style>
</head>
<body>
<h1>MIDI Color Mixer</h1>
<p>
This demo requires:
</p>
<ul>
<li>A <a href="https://caniuse.com/#feat=midi">WebMIDI capable browser</a></li>
<li>An <a href="https://www.adafruit.com/product/3333">Adafruit Circuit Playground</a></li>
<li>The <a href="https://github.com/georgemandis/circuit-playground-midi-multi-tool">Circuit Playground MIDI Multi-Tool</a> sketch
uploaded</li>
</ul>
<div>
<label for="red">Red</label>
<input type="range" name="red" id="red" min="0" max="255" value="0">
<span>0</span>
</div>
<div>
<label for="green">Green</label>
<input type="range" name="green" id="green" min="0" max="255" value="0">
<span>0</span>
</div>
<div>
<label for="blue">Blue</label>
<input type="range" name="blue" id="blue" min="0" max="255" value="0">
<span>0</span>
</div>
<div id="colorPreview"></div>
<script>
const colorChannelMapping = {
"red": 144,
"green": 145,
"blue": 146
}
const currentColor = {
"red": 0,
"green": 0,
"blue": 0
}
const initializeColorRangeInputs = (midiOutput) => {
document.querySelectorAll('input[type=range]').forEach((colorInput) => {
colorInput.addEventListener("input", (e) => {
e.target.nextElementSibling.innerText = e.target.value;
let data1 = parseInt(e.target.value);
let data2 = 0;
if (data1 > 127) {
data2 = -1 * (127 - data1) - 1;
data1 = 127;
}
let midiMessage = [colorChannelMapping[e.target.name], data1, data2];
midiOutput.send(midiMessage);
currentColor[e.target.name] = data1 + data2;
console.log(midiMessage);
console.log(currentColor)
document.querySelector('#colorPreview').style.background =
`rgb(${currentColor.red},${currentColor.green},${currentColor.blue})`
});
})
}
navigator.requestMIDIAccess()
.then((access) => {
// Find the MIDI ouput one we want for this demo
access.outputs.forEach((midiOutput) => {
if (midiOutput.manufacturer === 'Adafruit') {
initializeColorRangeInputs(midiOutput);
console.log("Output found");
}
})
});
</script>
</body>
</html>