mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
135 lines
5.6 KiB
TypeScript
135 lines
5.6 KiB
TypeScript
import { MouseEvent, useEffect, useRef, useState } from "react";
|
|
import { X, Send, RefreshCw } from 'lucide-react'
|
|
import { Button } from "@/components/ui/button";
|
|
import './index.css'
|
|
|
|
const hasGetUserMedia = () => !!navigator.mediaDevices?.getUserMedia;
|
|
|
|
const VIDEO_WIDTH = 480
|
|
const VIDEO_HEIGHT = 720
|
|
let STREAM = null as any
|
|
|
|
type Props = {
|
|
onClose: (e: MouseEvent<HTMLElement>) => any,
|
|
onSubmit: (photo: string) => any
|
|
}
|
|
|
|
export default function Camera({ onClose, onSubmit }: Props) {
|
|
const [isMounted, setIsMounted] = useState(false)
|
|
const [photo, setPhoto] = useState('')
|
|
|
|
const webcamRef = useRef<HTMLVideoElement>(null)
|
|
|
|
useEffect(() => {
|
|
setIsMounted(true)
|
|
|
|
return () => {
|
|
setIsMounted(false)
|
|
}
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
handleShowingCamera()
|
|
|
|
return () => {
|
|
if (STREAM) {
|
|
STREAM.getTracks().forEach((track: any) => track.stop());
|
|
}
|
|
}
|
|
}, [])
|
|
|
|
|
|
const handleShowingCamera = async () => {
|
|
if (!hasGetUserMedia()) {
|
|
console.log(navigator)
|
|
alert("Camera is not supported by your browser")
|
|
return
|
|
}
|
|
|
|
try {
|
|
// getUsermedia parameters
|
|
const constraints: MediaStreamConstraints = {
|
|
video: {
|
|
facingMode: { exact: 'user' },
|
|
width: VIDEO_WIDTH,
|
|
height: VIDEO_HEIGHT, // REQUIRED VALUE: 480 720 1080
|
|
frameRate: {
|
|
min: 30,
|
|
}
|
|
}
|
|
};
|
|
|
|
// Activate the webcam stream.
|
|
navigator.mediaDevices.getUserMedia(constraints)
|
|
.then(function (stream) {
|
|
STREAM = stream
|
|
webcamRef.current!.srcObject = stream
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
});
|
|
} catch (error) {
|
|
alert(error)
|
|
console.log(error)
|
|
}
|
|
}
|
|
|
|
const capture = async () => {
|
|
const canvas = document.createElement("canvas");
|
|
canvas.width = VIDEO_WIDTH
|
|
canvas.height = VIDEO_HEIGHT
|
|
|
|
try {
|
|
// console.log(canvas.width, canvas.height, webcamRef.current?.videoWidth, webcamRef.current?.videoHeight)
|
|
canvas.getContext('2d')!.drawImage(webcamRef.current!, 0, 0, webcamRef.current!.videoWidth, webcamRef.current!.videoHeight);
|
|
let image_data_url = canvas.toDataURL('image/jpeg');
|
|
|
|
console.log(image_data_url);
|
|
setPhoto(image_data_url)
|
|
if (STREAM) {
|
|
STREAM.getTracks().forEach((track: any) => track.stop());
|
|
}
|
|
} catch (err) {
|
|
console.error("Error: " + err);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className={`${isMounted ? 'open' : 'close'} custom-modal-background`} onClick={onClose}>
|
|
<div className={`${isMounted ? 'open' : 'close'} custom-modal w-11/12 md:!w-max`} style={{ translate: '-50% -50%' }}>
|
|
<div className="w-full overflow-y-auto" style={{ maxHeight: '95vh' }}>
|
|
<section className='flex flex-col items-center justify-center relative' onClick={(e) => e.stopPropagation()}>
|
|
<div className="relative bg-zinc-800 shadow-lg rounded-2xl" style={{ width: VIDEO_WIDTH, height: VIDEO_HEIGHT }}>
|
|
{photo ?
|
|
<img alt="" src={photo} style={{ transform: 'scaleX(-1)' }} className="object-cover h-full w-full rounded-2xl" />
|
|
:
|
|
<video ref={webcamRef} id="webcam" autoPlay playsInline className="h-full w-full rounded-2xl"></video>
|
|
}
|
|
<div className="absolute top-5 w-full flex justify-between items-center px-5">
|
|
<X className="cursor-pointer hover:text-zinc-400 duration-300 ml-16 sm:ml-0" size={28} onClick={onClose as any} />
|
|
{photo &&
|
|
<Button size='icon' variant='secondary' className="mr-16 sm:mr-0" onClick={() => onSubmit(photo)}><Send /></Button>
|
|
}
|
|
</div>
|
|
<div className="flex justify-center items-center absolute bottom-20 sm:bottom-5 w-full">
|
|
{!photo ?
|
|
<div className="bg-white rounded-full w-[88px] h-[88px] flex items-center justify-center cursor-pointer" onClick={capture}>
|
|
<div className="bg-zinc-950 rounded-full w-20 h-20 flex items-center justify-center">
|
|
<div className="bg-white rounded-full w-[70px] h-[70px]"></div>
|
|
</div>
|
|
</div>
|
|
:
|
|
<div className="bg-black bg-opacity-40 w-16 h-16 flex justify-center items-center rounded-full hover:text-zinc-400 mb-3 cursor-pointer duration-300">
|
|
<RefreshCw size={46} onClick={() => { setPhoto(''); handleShowingCamera() }} />
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
} |