Skip to content

Commit

Permalink
useCapture hook
Browse files Browse the repository at this point in the history
  • Loading branch information
maxibenner committed Aug 6, 2021
1 parent 47f7fa1 commit fdf4606
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
13 changes: 6 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@testing-library/jest-dom": "^5.11.10",
"@testing-library/react": "^11.2.6",
"@testing-library/user-event": "^12.8.3",
"ccapture.js": "^1.1.0",
"ccapture.js": "github:aceslowman/ccapture.js#npm-fix",
"ray-tracing-renderer": "^0.10.15",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
19 changes: 18 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,34 @@ import Viewer from "./components/three/Viewer"
import logo from "./assets/images/logo.png"
import "./globalStyles.css"
import useStore from "./states/modelState"

import useRecorder from "./helpers/useRecorder"

export default function App() {
const { gl } = useStore()
const recorder = useRecorder(gl?.domElement)

const handleStart = () => recorder.capture(2)

return (
<div style={styles.wrapper}>
<Viewer />
<img src={logo} alt="Logo" style={styles.logo} />
<ButtonPhoto gl={gl} style={styles.logo} />
<ControlPanel />
<div
style={{
position: "absolute",
top: 0,
left: 0,
width: "50px",
height: "100vh",
display: "flex",
flexDirection: "column",
justifyContent: "center",
}}
>
<button onClick={handleStart}>Start</button>
</div>
</div>
)
}
Expand Down
Empty file removed src/components/three/Controls.js
Empty file.
33 changes: 33 additions & 0 deletions src/helpers/useRecorder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect } from "react"

export default function useRecorder(element) {
var mediaRecorder, stream

useEffect(() => {
if (element) {
stream = element.captureStream(25)
mediaRecorder = new MediaRecorder(stream)
mediaRecorder.ondataavailable = (blob) => {
saveFile(blob)
}
}
}, [element])

function capture(durationInSeconds = 1) {
mediaRecorder.start()

setTimeout(() => {
mediaRecorder.stop()
}, durationInSeconds * 1000)
}

function saveFile(blob) {
var blobUrl = URL.createObjectURL(blob.data)
var link = document.createElement("a")
link.href = blobUrl
link.download = "aDefaultFileName.mp4"
link.click()
}

return { capture }
}

0 comments on commit fdf4606

Please sign in to comment.