-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a5275c
commit 48b5fbf
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/apps/proof-of-us/src/components/CapturePhoto/CapturePhoto.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { FC, MouseEvent } from 'react'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
import ReactSketchCanvas from 'react-canvas-draw'; | ||
import { modalClass } from './styles.css'; | ||
|
||
export const CapturePhoto: FC = () => { | ||
const videoRef = useRef<HTMLVideoElement>(null); | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
const [bg, setBg] = useState(undefined); | ||
|
||
useEffect(() => { | ||
if (!videoRef.current && !isModalOpen) return; | ||
|
||
navigator.mediaDevices.getUserMedia({ video: true }).then((stream) => { | ||
if (!videoRef.current) return; | ||
videoRef.current.srcObject = stream; | ||
}); | ||
}, [isModalOpen]); | ||
|
||
const handleCapture = (evt: MouseEvent<HTMLButtonElement>) => { | ||
evt.preventDefault(); | ||
if (!canvasRef.current || !videoRef.current) return; | ||
console.log(canvasRef.current); | ||
//const canvas = canvasRef.current.querySelector('svg'); | ||
const context = canvasRef.current.ctx.drawing; | ||
|
||
console.log(videoRef.current, canvasRef.current.innerHTML); | ||
//setBg(videoRef.current); | ||
|
||
context?.drawImage(videoRef.current, 0, 0, 320, 240); | ||
|
||
(videoRef.current?.srcObject as MediaStream) | ||
?.getTracks() | ||
.forEach((t) => t.stop()); | ||
|
||
setIsModalOpen(false); | ||
}; | ||
|
||
const handleToggleCaptureModal = (evt: MouseEvent<HTMLButtonElement>) => { | ||
evt.preventDefault(); | ||
|
||
setIsModalOpen((v) => !v); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button onClick={handleToggleCaptureModal}>Capture</button> | ||
|
||
<div> | ||
<ReactSketchCanvas | ||
hideGridX={true} | ||
hideGridY={false} | ||
canvasWidth={320} | ||
canvasHeight={240} | ||
ref={canvasRef} | ||
/> | ||
</div> | ||
|
||
{isModalOpen && ( | ||
<section className={modalClass}> | ||
<input type="file" accept="image/*" capture="user" /> | ||
|
||
<video ref={videoRef} id="player" controls autoPlay></video> | ||
<button id="capture" onClick={handleCapture}> | ||
Capture | ||
</button> | ||
</section> | ||
)} | ||
</div> | ||
); | ||
}; |
11 changes: 11 additions & 0 deletions
11
packages/apps/proof-of-us/src/components/CapturePhoto/styles.css.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { style } from '@vanilla-extract/css'; | ||
|
||
export const modalClass = style([ | ||
{ | ||
position: 'absolute', | ||
background: 'white', | ||
inset: 0, | ||
}, | ||
]); | ||
|
||
export const notificationWrapperClass = style({}); |