-
Notifications
You must be signed in to change notification settings - Fork 90
/
App.tsx
146 lines (131 loc) · 3.17 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
import React, { 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 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: 1;
cursor: default;
}
@media (max-width: 400px) {
padding: 40px 5px;
}
`;
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 App = () => {
const [numberOfCameras, setNumberOfCameras] = useState(0);
const [image, setImage] = useState<string | null>(null);
const camera = useRef<CameraType>(null);
return (
<Wrapper>
<Camera ref={camera} aspectRatio="cover" numberOfCamerasCallback={setNumberOfCameras} />
<Control>
<ImagePreview image={image} />
<TakePhotoButton
onClick={() => {
if (camera.current) {
const photo = camera.current.takePhoto();
console.log(photo);
setImage(photo);
}
}}
/>
<ChangeFacingCameraButton
disabled={numberOfCameras <= 1}
onClick={() => {
if (camera.current) {
const result = camera.current.switchCamera();
console.log(result);
}
}}
/>
</Control>
</Wrapper>
);
};
export default App;