-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
151 lines (132 loc) · 4.9 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>react-watch-keys</title>
</head>
<body>
<script>
document.write((navigator.onLine ? [
'<script src="https://unpkg.com/react@15.0.1/dist/react.js"></'+'script>',
'<script src="https://unpkg.com/react-dom@15.0.1/dist/react-dom.js"></'+'script>',
'<script src="https://unpkg.com/babel-core@5.8.38/browser.js"></'+'script>'
] : [
'<script src="./node_modules/react/dist/react.js"></'+'script>',
'<script src="./node_modules/react-dom/dist/react-dom.js"></'+'script>',
'<script src="./node_modules/babel-core/browser.js"></'+'script>'
]).join('\n'));
</script>
<script src="./index.js"></script>
<script type="text/babel">
var watchKeys = window.reactWatchKeys;
var RawTone = React.createClass({
getDefaultProps: function() {
return {
frequency: 440
};
},
componentDidMount: function() {
this.audioContext = new AudioContext();
this.oscillator = this.audioContext.createOscillator();
this.oscillator.connect(this.audioContext.destination);
this.oscillator.frequency.value = this.props.frequency;
this.oscillator.start();
},
componentWillUnmount: function() {
this.oscillator.stop();
this.audioContext.close();
},
componentDidUpdate: function() {
this.oscillator.frequency.value = this.props.frequency;
},
render: function() {
return null;
}
});
var Tone = React.createClass({
statics: {
getFrequency: function(step) {
return 440 * Math.pow(2, step / 12);
}
},
getDefaultProps: function() {
return {
frequency: 0,
disabled: false
};
},
render: function() {
return (
<span style={{
opacity: this.props.disabled ? 0.33 : null
}}>
{this.props.children}
{this.props.disabled ? null : <RawTone frequency={this.props.frequency} />}
</span>
);
}
});
var Example = watchKeys({
'16': 'shift',
'17': 'ctrl',
'18': 'alt',
'81': 'q',
'87': 'w',
'69': 'e',
'82': 'r',
'84': 't',
'89': 'y',
'85': 'u',
'73': 'i',
'79': 'o',
'80': 'p',
'219': 'leftBracket',
'221': 'rightBracket',
'220': 'backslash'
}, null, true)(React.createClass({
displayName: 'Example',
render: function() {
var bend = 0;
if (this.props.heldKeys.shift) {
bend += 1;
}
if (this.props.heldKeys.ctrl) {
bend += 2;
}
if (this.props.heldKeys.alt) {
bend *= -1;
}
return (
<div style={{
textAlign: 'center'
}}>
<p>Here’s a dumb little demo that watches keyboard state.</p>
<p>Not responsible for blown speakers.</p>
<p style={{
fontSize: '2em'
}}>
<Tone frequency={Tone.getFrequency(-9 + bend)} disabled={!this.props.heldKeys.q}>Q</Tone>
<Tone frequency={Tone.getFrequency(-8 + bend)} disabled={!this.props.heldKeys.w}>W</Tone>
<Tone frequency={Tone.getFrequency(-7 + bend)} disabled={!this.props.heldKeys.e}>E</Tone>
<Tone frequency={Tone.getFrequency(-6 + bend)} disabled={!this.props.heldKeys.r}>R</Tone>
<Tone frequency={Tone.getFrequency(-5 + bend)} disabled={!this.props.heldKeys.t}>T</Tone>
<Tone frequency={Tone.getFrequency(-4 + bend)} disabled={!this.props.heldKeys.y}>Y</Tone>
<Tone frequency={Tone.getFrequency(-3 + bend)} disabled={!this.props.heldKeys.u}>U</Tone>
<Tone frequency={Tone.getFrequency(-2 + bend)} disabled={!this.props.heldKeys.i}>I</Tone>
<Tone frequency={Tone.getFrequency(-1 + bend)} disabled={!this.props.heldKeys.o}>O</Tone>
<Tone frequency={Tone.getFrequency(0 + bend)} disabled={!this.props.heldKeys.p}>P</Tone>
<Tone frequency={Tone.getFrequency(1 + bend)} disabled={!this.props.heldKeys.leftBracket}>[</Tone>
<Tone frequency={Tone.getFrequency(2 + bend)} disabled={!this.props.heldKeys.rightBracket}>]</Tone>
<Tone frequency={Tone.getFrequency(3 + bend)} disabled={!this.props.heldKeys.backslash}>\</Tone>
</p>
<p>Bend (with shift, control, alt): {bend}</p>
</div>
);
}
}));
var container = document.createElement('div');
document.body.appendChild(container);
ReactDOM.render(<Example />, container);
</script>
</body>
</html>