generated from allen-cell-animated/github-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
88 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React, { useState, useEffect, useRef } from "react"; | ||
import Recorder from "../src/simularium/StreamRecorder"; | ||
|
||
interface RecordMovieComponentProps { | ||
trajectoryTitle: string; | ||
} | ||
|
||
const RecordMovieComponent = ({ | ||
trajectoryTitle, | ||
}: RecordMovieComponentProps) => { | ||
const [isRecording, setIsRecording] = useState(false); | ||
let [recordingDuration, setRecordingDuration] = useState<number>(0); | ||
let [recordingStatus, setRecordingStatus] = | ||
useState<string>("Not recording"); | ||
let [outputStatus, setOutputStatus] = useState<string>(""); | ||
const recorderRef = useRef<Recorder | null>(null); | ||
|
||
useEffect(() => { | ||
// Access the existing canvas element | ||
const canvasEl = document.querySelector("canvas"); | ||
if (canvasEl) { | ||
recorderRef.current = new Recorder(canvasEl, trajectoryTitle); | ||
} | ||
}, [trajectoryTitle]); | ||
|
||
// this useEffect is a timer that updates the recording duration | ||
useEffect(() => { | ||
let intervalId; | ||
|
||
if (isRecording) { | ||
intervalId = setInterval(() => { | ||
setRecordingDuration((prevTimer) => prevTimer + 1); | ||
}, 1000); | ||
} | ||
|
||
return () => { | ||
clearInterval(intervalId); | ||
}; | ||
}, [isRecording]); | ||
|
||
const startRecording = async () => { | ||
if (recorderRef.current) { | ||
await recorderRef.current.setupStream(); | ||
setRecordingDuration(0); | ||
setIsRecording(true); | ||
setRecordingStatus("Recording in progress..."); | ||
setOutputStatus(""); | ||
} | ||
}; | ||
|
||
const stopRecording = async () => { | ||
// when we start recording again, reset ouput status to recording in progress | ||
if (recorderRef.current) { | ||
setIsRecording(false); | ||
setRecordingStatus("Not recording"); | ||
setOutputStatus("Processing..."); | ||
await recorderRef.current.onCompletedRecording(); | ||
setOutputStatus( | ||
"Your movie has been downloaded, duration: " + | ||
recordingDuration + | ||
" seconds" | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button onClick={startRecording} disabled={isRecording}> | ||
Start Recording | ||
</button> | ||
<button onClick={stopRecording} disabled={!isRecording}> | ||
Stop Recording | ||
</button> | ||
<div>{recordingStatus}</div> | ||
<div> | ||
{isRecording | ||
? "Recording duration: " + recordingDuration + " seconds" | ||
: ""} | ||
</div> | ||
<div>{outputStatus}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default RecordMovieComponent; |
This file was deleted.
Oops, something went wrong.
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