Skip to content

Commit

Permalink
Merge pull request #6 from DY-MAKE/feature/camera-preview
Browse files Browse the repository at this point in the history
camera preview
  • Loading branch information
WhiteKr authored Mar 6, 2024
2 parents dd15191 + 9fddcf8 commit 2b3fd01
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions apps/frontend/components/atoms/CameraPreview/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
'use client';

import { useEffect, useRef } from 'react';

import { cn } from '@/utils/cn';

type CameraPreviewProps = React.HTMLAttributes<HTMLVideoElement>;
interface CameraPreviewProps {
className?: string;
}

export const CameraPreview = ({
className,
...args
}: CameraPreviewProps) => {
return <video className={cn(className)} {...args} />;
}: CameraPreviewProps): JSX.Element => {
const videoRef = useRef<HTMLVideoElement>(null);

useEffect(() => {
const stream: Promise<MediaStream> =
navigator.mediaDevices.getUserMedia({
video: true,
audio: false,
});
const videoCurrent: HTMLVideoElement | null = videoRef.current;

stream.then((s: MediaStream) => {
if (videoCurrent) {
videoCurrent.srcObject = s;
}
});

return () => {
if (videoCurrent) {
videoCurrent.srcObject = null;
}
};
}, []);

return (
<video
ref={videoRef}
autoPlay
muted
className={cn('pointer-events-none', className)}
/>
);
};

0 comments on commit 2b3fd01

Please sign in to comment.