Skip to content

Commit 5ceaf29

Browse files
feat: Scanner
1 parent 861f801 commit 5ceaf29

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/components/Scanner/scanner.tsx

+22-5
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,34 @@ class Scanner extends Component<ScannerProps, ScannerState> {
5959
);
6060
};
6161

62+
updateVideoSize() {
63+
const video = document.querySelector("video");
64+
if (video) {
65+
const aspectRatio = video.videoWidth / video.videoHeight;
66+
const windowAspectRatio = window.innerWidth / window.innerHeight;
67+
68+
if (aspectRatio > windowAspectRatio) {
69+
video.style.width = "100%";
70+
video.style.height = "auto";
71+
} else {
72+
video.style.width = "auto";
73+
video.style.height = "100%";
74+
}
75+
}
76+
}
77+
6278
componentDidMount() {
6379
const { facingMode } = this.state;
6480
console.log(facingMode);
6581
this.handleClick();
6682
Quagga.onDetected(this._onDetected);
83+
this.updateVideoSize();
84+
window.addEventListener("resize", this.updateVideoSize);
6785
}
6886

6987
componentWillUnmount() {
7088
Quagga.offDetected(this._onDetected);
89+
window.removeEventListener("resize", this.updateVideoSize);
7190
}
7291

7392
_onDetected = (result: any) => {
@@ -90,11 +109,9 @@ class Scanner extends Component<ScannerProps, ScannerState> {
90109
const vid: React.CSSProperties = {
91110
position: "fixed",
92111
zIndex: 999,
93-
left: 0,
94-
top: 0,
95-
width: "100%",
96-
height: "100%",
97-
objectFit: "cover",
112+
left: "50%",
113+
top: "50%",
114+
transform: "translate(-50%, -50%)",
98115
};
99116

100117
return (

0 commit comments

Comments
 (0)