mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
155 lines
7.6 KiB
TypeScript
155 lines
7.6 KiB
TypeScript
import { MouseEvent, useEffect, useRef, useState } from "react";
|
|
import { RefreshCw } from 'lucide-react'
|
|
import useMobileView from "@/hooks/useMobileView";
|
|
import './index.css'
|
|
|
|
const hasGetUserMedia = () => !!navigator.mediaDevices?.getUserMedia;
|
|
|
|
const VIDEO_WIDTH = 480
|
|
const VIDEO_HEIGHT = 720
|
|
const MOBILE_VIDEO_WIDTH = 360
|
|
const MOBILE_VIDEO_HEIGHT = 540
|
|
let STREAM = null as any
|
|
|
|
type Props = {
|
|
onClose: (e: MouseEvent<HTMLElement>) => any
|
|
onSubmit: (photo: string, landscape?: boolean, w?: number, h?: number) => any
|
|
}
|
|
|
|
export default function Camera({ onClose, onSubmit }: Props) {
|
|
const [streamWidth, setStreamWidth] = useState(VIDEO_WIDTH)
|
|
const [streamHeight, setStreamHeight] = useState(VIDEO_HEIGHT)
|
|
const [isMounted, setIsMounted] = useState(false)
|
|
const [photo, setPhoto] = useState('')
|
|
const { windowWidth } = useMobileView()
|
|
|
|
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 on your browser")
|
|
return
|
|
}
|
|
|
|
try {
|
|
// getUsermedia parameters
|
|
// const videoWidth = windowWidth < 480 ? { exact: VIDEO_HEIGHT, min: VIDEO_HEIGHT, max: VIDEO_HEIGHT } : { exact: VIDEO_WIDTH, min: VIDEO_WIDTH, max: VIDEO_WIDTH }
|
|
// const videoHeight = windowWidth < 480 ? { exact: VIDEO_WIDTH, min: VIDEO_WIDTH, max: VIDEO_WIDTH } : { exact: VIDEO_HEIGHT, min: VIDEO_HEIGHT, max: VIDEO_HEIGHT }
|
|
const videoWidth = windowWidth < 480 ? { exact: VIDEO_HEIGHT, min: VIDEO_HEIGHT, max: VIDEO_HEIGHT } : VIDEO_WIDTH
|
|
const videoHeight = windowWidth < 480 ? { exact: VIDEO_WIDTH, min: VIDEO_WIDTH, max: VIDEO_WIDTH } : VIDEO_HEIGHT
|
|
|
|
// getUsermedia parameters
|
|
const constraints: MediaStreamConstraints = {
|
|
video: {
|
|
facingMode: { exact: 'user' },
|
|
// width: VIDEO_WIDTH,
|
|
// height: VIDEO_HEIGHT, // REQUIRED VALUE: 480 720 1080
|
|
width: videoWidth,
|
|
height: videoHeight, // REQUIRED VALUE: 480 720 1080
|
|
frameRate: {
|
|
min: 30,
|
|
}
|
|
}
|
|
};
|
|
|
|
// Activate the webcam stream.
|
|
navigator.mediaDevices.getUserMedia(constraints)
|
|
.then(function (stream) {
|
|
let stream_settings = stream.getVideoTracks()[0].getSettings();
|
|
setStreamWidth(stream_settings.width!)
|
|
setStreamHeight(stream_settings.height!)
|
|
|
|
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 = streamWidth || VIDEO_WIDTH
|
|
canvas.height = streamHeight || 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 text-white`} 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 ${streamWidth >= streamHeight && 'flex justify-center items-center'}`} style={{ width: windowWidth < VIDEO_WIDTH ? MOBILE_VIDEO_WIDTH : VIDEO_WIDTH, height: windowWidth < VIDEO_WIDTH ? MOBILE_VIDEO_HEIGHT : VIDEO_HEIGHT }}>
|
|
{photo ?
|
|
<img alt="" src={photo} style={{ transform: 'scaleX(-1)' }} className={`${streamWidth < streamHeight ? 'h-full w-full object-cover' : 'w-full'} rounded-2xl`} />
|
|
:
|
|
<video ref={webcamRef} id="webcam" autoPlay playsInline className="h-full w-full rounded-2xl"></video>
|
|
}
|
|
<div className="absolute top-4 md:top-5 w-full flex justify-between items-center px-5">
|
|
<button className="bg-black bg-opacity-40 w-12 h-12 flex justify-center items-center rounded-full hover:text-zinc-400 duration-300 sm:ml-0" onClick={onClose as any}>
|
|
<i className="ti ti-chevron-left icon text-4xl"></i>
|
|
</button>
|
|
{photo &&
|
|
<button className="bg-his bg-opacity-90 text-white w-12 h-12 flex justify-center items-center rounded-full hover:bg-his/70 duration-300 sm:mr-0" onClick={() => onSubmit(photo, streamWidth > streamHeight, streamWidth, streamHeight)}>
|
|
<i className="ti ti-send icon text-2xl"></i>
|
|
</button>
|
|
// <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-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={40} onClick={() => { setPhoto(''); handleShowingCamera() }} />
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
} |