-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathFX.js
60 lines (56 loc) · 1.21 KB
/
FX.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
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import { bufferResource } from './bufferResource';
const Button = styled.button`
background: ${props => (props.held ? '#fd7272' : 'none')};
flex: 1;
border: 2px solid #fd7272;
color: ${props => (props.held ? 'black' : '#fd7272')};
display: inline-block;
font-size: 24px;
border-radius: 2;
padding: 20px;
font-family: 'Righteous', cursive;
margin: 2px;
&:active {
background: #fd7272;
color: black;
}
`;
export default function FX({ sound, title }) {
let [held, setHeld] = useState(false);
let buffer = bufferResource.read(sound);
buffer.volume.value = -8;
useEffect(() => {
buffer.start();
buffer.stop();
}, [])
function playSound(e) {
if (held) {
setHeld(false);
buffer.stop();
} else {
if (e.shiftKey) {
buffer.loop = true;
setHeld(true);
}
buffer.start();
}
}
function stopSound() {
if (!held) {
buffer.stop();
}
}
return (
<Button
held={held}
onMouseDown={playSound}
onTouchStart={playSound}
onTouchEnd={stopSound}
onMouseUp={stopSound}
>
{title}
</Button>
);
}