-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathApp.final.js
84 lines (73 loc) · 1.87 KB
/
App.final.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
import React, { Component } from "react";
import createOscillator from "./lib/createOscillator";
import SineWave from "./lib/SineWave";
class Tone extends Component {
oscillator = createOscillator();
componentDidMount() {
this.doImperativeStuff();
}
componentDidUpdate() {
this.doImperativeStuff();
}
doImperativeStuff() {
const { isPlaying, pitch, volume } = this.props;
if (isPlaying) {
this.oscillator.play();
} else {
this.oscillator.stop();
}
this.oscillator.setPitchBend(pitch);
this.oscillator.setVolume(volume);
}
render() {
return null;
}
}
class App extends Component {
state = {
isPlaying: false,
pitch: 0,
volume: 0
};
play = () => {
this.setState({ isPlaying: true });
};
stop = () => {
this.setState({ isPlaying: false });
};
changeTone = event => {
const { clientX, clientY } = event;
const { top, right, bottom, left } = event.target.getBoundingClientRect();
const pitch = (clientX - left) / (right - left);
const volume = 1 - (clientY - top) / (bottom - top);
this.setState({ pitch, volume });
};
render() {
return (
<div className="App">
<div
className="theremin"
onMouseEnter={this.play}
onMouseLeave={this.stop}
onMouseMove={this.changeTone}
>
<SineWave
width="400px"
height="400px"
amplitude={this.state.volume}
frequency={this.state.pitch}
draw={this.state.isPlaying}
/>
<Tone
isPlaying={this.state.isPlaying}
pitch={this.state.pitch}
volume={this.state.volume}
/>
</div>
<div className="label pitch">◀︎ Pitch ▶︎</div>
<div className="label volume">◀︎ Volume ▶︎</div>
</div>
);
}
}
export default App;