-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor screenshot code #54
Merged
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
55866cd
#93 implement 3D Screenshot functionality
Salam-Dalloul 24c01bf
#93 implement 3D Screenshot functionality
Salam-Dalloul 98fb187
#93 implement 3D video recording
Salam-Dalloul 6b6874e
move SceneControls functions to the SceneControls file
Salam-Dalloul 4c10dca
remove unused code
Salam-Dalloul 78af958
remove unused code
Salam-Dalloul 56ea5b1
resolve conflict
Salam-Dalloul 81138da
resolve conflict
Salam-Dalloul d551e27
refactor screenshot code
Salam-Dalloul af40bd2
#96 implement background colour change function
Salam-Dalloul 3a99861
change colors to string
Salam-Dalloul 605c595
update filename with the current workspace
Salam-Dalloul dec2942
handle Screenshot error properly
Salam-Dalloul cfd9711
handle Screenshot error properly
Salam-Dalloul 4d5fb2e
use string interpolation instead of concatenation
Salam-Dalloul 1e3703c
resolve conflict
Salam-Dalloul 481105b
Merge pull request #55 from MetaCell/feature/CELE-96
ddelpiano 3e783f6
resolve conflict
Salam-Dalloul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
115 changes: 115 additions & 0 deletions
115
applications/visualizer/frontend/src/components/viewers/ThreeD/Recorder.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,115 @@ | ||
import { formatDate } from "../../../helpers/utils.ts"; | ||
export class Recorder { | ||
private mediaRecorder: MediaRecorder | null = null; | ||
private recordedBlobs: Blob[] = []; | ||
private stream: MediaStream; | ||
private ctx: WebGLRenderingContext; | ||
// @ts-ignore | ||
private options: { mediaRecorderOptions?: MediaRecorderOptions; blobOptions?: BlobPropertyBag } = { | ||
mediaRecorderOptions: { mimeType: "video/webm" }, | ||
blobOptions: { type: "video/webm" }, | ||
}; | ||
private blobOptions: BlobPropertyBag = { type: "video/webm" }; | ||
|
||
constructor(canvas: HTMLCanvasElement, recorderOptions: { mediaRecorderOptions?: MediaRecorderOptions; blobOptions?: BlobPropertyBag }) { | ||
this.stream = canvas.captureStream(); | ||
const { mediaRecorderOptions, blobOptions } = recorderOptions; | ||
this.setupMediaRecorder(mediaRecorderOptions); | ||
this.recordedBlobs = []; | ||
this.blobOptions = blobOptions; | ||
this.ctx = canvas.getContext("webgl"); | ||
} | ||
|
||
handleDataAvailable(event) { | ||
if (event.data && event.data.size > 0) { | ||
this.recordedBlobs.push(event.data); | ||
} | ||
} | ||
|
||
setupMediaRecorder(options) { | ||
let error = ""; | ||
|
||
if (options == null) { | ||
options = { mimeType: "video/webm" }; | ||
} | ||
let mediaRecorder; | ||
try { | ||
mediaRecorder = new MediaRecorder(this.stream, options); | ||
} catch (e0) { | ||
error = `Unable to create MediaRecorder with options Object: ${e0}`; | ||
try { | ||
options = { mimeType: "video/webm,codecs=vp9" }; | ||
mediaRecorder = new MediaRecorder(this.stream, options); | ||
} catch (e1) { | ||
error = `Unable to create MediaRecorder with options Object: ${e1}`; | ||
try { | ||
options = { mimeType: "video/webm,codecs=vp8" }; // Chrome 47 | ||
mediaRecorder = new MediaRecorder(this.stream, options); | ||
} catch (e2) { | ||
error = | ||
"MediaRecorder is not supported by this browser.\n\n" + | ||
"Try Firefox 29 or later, or Chrome 47 or later, " + | ||
"with Enable experimental Web Platform features enabled from chrome://flags." + | ||
`Exception while creating MediaRecorder: ${e2}`; | ||
} | ||
} | ||
} | ||
|
||
if (!mediaRecorder) { | ||
throw new Error(error); | ||
} | ||
|
||
mediaRecorder.ondataavailable = (evt) => this.handleDataAvailable(evt); | ||
mediaRecorder.onstart = () => this.animationLoop(); | ||
|
||
this.mediaRecorder = mediaRecorder; | ||
this.options = options; | ||
if (!this.blobOptions) { | ||
const { mimeType } = options; | ||
this.blobOptions = { type: mimeType }; | ||
} | ||
} | ||
|
||
startRecording() { | ||
this.recordedBlobs = []; | ||
this.mediaRecorder.start(100); | ||
} | ||
|
||
stopRecording(options) { | ||
this.mediaRecorder.stop(); | ||
return this.getRecordingBlob(options); | ||
} | ||
|
||
download(filename, options) { | ||
if (!filename) { | ||
filename = `CanvasRecording_${formatDate(new Date())}.webm`; | ||
} | ||
const blob = this.getRecordingBlob(options); | ||
const url = window.URL.createObjectURL(blob); | ||
const a = document.createElement("a"); | ||
a.style.display = "none"; | ||
a.href = url; | ||
a.download = filename; | ||
document.body.appendChild(a); | ||
a.click(); | ||
setTimeout(() => { | ||
document.body.removeChild(a); | ||
window.URL.revokeObjectURL(url); | ||
}, 100); | ||
return blob; | ||
} | ||
|
||
getRecordingBlob(options) { | ||
if (!options) { | ||
options = this.blobOptions; | ||
} | ||
return new Blob(this.recordedBlobs, options); | ||
} | ||
|
||
animationLoop() { | ||
this.ctx.drawArrays(this.ctx.POINTS, 0, 0); | ||
if (this.mediaRecorder.state !== "inactive") { | ||
requestAnimationFrame(this.animationLoop); | ||
} | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
applications/visualizer/frontend/src/components/viewers/ThreeD/Screenshoter.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,63 @@ | ||
import * as THREE from "three"; | ||
|
||
function getResolutionFixedRatio(htmlElement: HTMLElement, target: { width: number; height: number }) { | ||
const current = { | ||
height: htmlElement.clientHeight, | ||
width: htmlElement.clientWidth, | ||
}; | ||
|
||
if ((Math.abs(target.width - current.width) * 9) / 16 > Math.abs(target.height - current.height)) { | ||
return { | ||
height: target.height, | ||
width: Math.round((current.width * target.height) / current.height), | ||
}; | ||
} | ||
return { | ||
height: Math.round((current.height * target.width) / current.width), | ||
width: target.width, | ||
}; | ||
} | ||
|
||
function getOptions(htmlElement: HTMLCanvasElement, targetResolution: { width: number; height: number }, pixelRatio: number) { | ||
const resolution = getResolutionFixedRatio(htmlElement, targetResolution); | ||
return { | ||
canvasWidth: resolution.width, | ||
canvasHeight: resolution.height, | ||
pixelRatio: pixelRatio, | ||
}; | ||
} | ||
|
||
export function downloadScreenshot( | ||
canvasRef: React.RefObject<HTMLCanvasElement>, | ||
sceneRef: React.RefObject<THREE.Scene>, | ||
cameraRef: React.RefObject<THREE.PerspectiveCamera>, | ||
) { | ||
if (!sceneRef.current || !cameraRef.current || !canvasRef.current) return; | ||
|
||
const options = getOptions(canvasRef.current, { width: 3840, height: 2160 }, 1); | ||
|
||
try { | ||
const tempRenderer = new THREE.WebGLRenderer({ preserveDrawingBuffer: true }); | ||
tempRenderer.setSize(options.canvasWidth, options.canvasHeight); | ||
tempRenderer.setPixelRatio(options.pixelRatio); // Set the resolution scaling | ||
|
||
cameraRef.current.aspect = options.canvasWidth / options.canvasHeight; | ||
cameraRef.current.updateProjectionMatrix(); | ||
|
||
tempRenderer.render(sceneRef.current, cameraRef.current); | ||
|
||
tempRenderer.domElement.toBlob((blob) => { | ||
if (blob) { | ||
const link = document.createElement("a"); | ||
link.href = URL.createObjectURL(blob); | ||
link.download = "screenshot.png"; | ||
link.click(); | ||
URL.revokeObjectURL(link.href); | ||
} | ||
}, "image/png"); | ||
|
||
tempRenderer.dispose(); | ||
} catch (e) { | ||
console.error("Error saving image:", e); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (non-blocking): I would prefer a more meaningful download name (potentially one that uses the workspace name or active neurons) but we can keep it like this until it becomes a request from the customer (if it happens)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@afonsobspinto i have updated the code for both screenshot and recording files naming with the workspace name