-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathApp.tsx
240 lines (221 loc) · 5.93 KB
/
App.tsx
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import React, { useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
import { Camera, CameraType } from './Camera';
const Wrapper = styled.div`
position: fixed;
width: 100%;
height: 100%;
z-index: 1;
`;
const Control = styled.div`
position: fixed;
display: flex;
right: 0;
width: 20%;
min-width: 130px;
min-height: 130px;
height: 100%;
background: rgba(0, 0, 0, 0.8);
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
padding: 50px;
box-sizing: border-box;
flex-direction: column-reverse;
@media (max-aspect-ratio: 1/1) {
flex-direction: row;
bottom: 0;
width: 100%;
height: 20%;
}
@media (max-width: 400px) {
padding: 10px;
}
`;
const Button = styled.button`
outline: none;
color: white;
opacity: 1;
background: transparent;
background-color: transparent;
background-position-x: 0%;
background-position-y: 0%;
background-repeat: repeat;
background-image: none;
padding: 0;
text-shadow: 0px 0px 4px black;
background-position: center center;
background-repeat: no-repeat;
pointer-events: auto;
cursor: pointer;
z-index: 2;
filter: invert(100%);
border: none;
&:hover {
opacity: 0.7;
}
`;
const TakePhotoButton = styled(Button)`
background: url('https://img.icons8.com/ios/50/000000/compact-camera.png');
background-position: center;
background-size: 50px;
background-repeat: no-repeat;
width: 80px;
height: 80px;
border: solid 4px black;
border-radius: 50%;
&:hover {
background-color: rgba(0, 0, 0, 0.3);
}
`;
const TorchButton = styled(Button)`
background: url('https://img.icons8.com/ios/50/000000/light.png');
background-position: center;
background-size: 50px;
background-repeat: no-repeat;
width: 80px;
height: 80px;
border: solid 4px black;
border-radius: 50%;
&.toggled {
background-color: rgba(0, 0, 0, 0.3);
}
`;
const ChangeFacingCameraButton = styled(Button)`
background: url(https://img.icons8.com/ios/50/000000/switch-camera.png);
background-position: center;
background-size: 40px;
background-repeat: no-repeat;
width: 40px;
height: 40px;
padding: 40px;
&:disabled {
opacity: 0;
cursor: default;
padding: 60px;
}
@media (max-width: 400px) {
padding: 40px 5px;
&:disabled {
padding: 40px 25px;
}
}
`;
const ImagePreview = styled.div<{ image: string | null }>`
width: 120px;
height: 120px;
${({ image }) => (image ? `background-image: url(${image});` : '')}
background-size: contain;
background-repeat: no-repeat;
background-position: center;
@media (max-width: 400px) {
width: 50px;
height: 120px;
}
`;
const FullScreenImagePreview = styled.div<{ image: string | null }>`
width: 100%;
height: 100%;
z-index: 100;
position: absolute;
background-color: black;
${({ image }) => (image ? `background-image: url(${image});` : '')}
background-size: contain;
background-repeat: no-repeat;
background-position: center;
`;
const App = () => {
const [numberOfCameras, setNumberOfCameras] = useState(0);
const [image, setImage] = useState<string | null>(null);
const [showImage, setShowImage] = useState<boolean>(false);
const camera = useRef<CameraType>(null);
const [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
const [activeDeviceId, setActiveDeviceId] = useState<string | undefined>(undefined);
const [torchToggled, setTorchToggled] = useState<boolean>(false);
useEffect(() => {
(async () => {
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter((i) => i.kind == 'videoinput');
setDevices(videoDevices);
})();
});
return (
<Wrapper>
{showImage ? (
<FullScreenImagePreview
image={image}
onClick={() => {
setShowImage(!showImage);
}}
/>
) : (
<Camera
ref={camera}
aspectRatio="cover"
facingMode="environment"
numberOfCamerasCallback={(i) => setNumberOfCameras(i)}
videoSourceDeviceId={activeDeviceId}
errorMessages={{
noCameraAccessible: 'No camera device accessible. Please connect your camera or try a different browser.',
permissionDenied: 'Permission denied. Please refresh and give camera permission.',
switchCamera:
'It is not possible to switch camera to different one because there is only one video device accessible.',
canvas: 'Canvas is not supported.',
}}
videoReadyCallback={() => {
console.log('Video feed ready.');
}}
/>
)}
<Control>
<select
onChange={(event) => {
setActiveDeviceId(event.target.value);
}}
>
{devices.map((d) => (
<option key={d.deviceId} value={d.deviceId}>
{d.label}
</option>
))}
</select>
<ImagePreview
image={image}
onClick={() => {
setShowImage(!showImage);
}}
/>
<TakePhotoButton
onClick={() => {
if (camera.current) {
const photo = camera.current.takePhoto();
console.log(photo);
setImage(photo as string);
}
}}
/>
{camera.current?.torchSupported && (
<TorchButton
className={torchToggled ? 'toggled' : ''}
onClick={() => {
if (camera.current) {
setTorchToggled(camera.current.toggleTorch());
}
}}
/>
)}
<ChangeFacingCameraButton
disabled={numberOfCameras <= 1}
onClick={() => {
if (camera.current) {
const result = camera.current.switchCamera();
console.log(result);
}
}}
/>
</Control>
</Wrapper>
);
};
export default App;