-
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.
Merge pull request #50 from MetaCell/feature/CELE-93
Feature/cele 93
- Loading branch information
Showing
6 changed files
with
231 additions
and
7 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
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
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
import * as htmlToImage from "html-to-image"; | ||
import { formatDate } from "../../../helpers/utils.ts"; | ||
|
||
function getOptions(htmlElement, targetResolution, quality, pixelRatio, filter) { | ||
const resolution = getResolutionFixedRatio(htmlElement, targetResolution); | ||
return { | ||
quality: quality, | ||
canvasWidth: resolution.width, | ||
canvasHeight: resolution.height, | ||
pixelRatio: pixelRatio, | ||
filter: filter, | ||
}; | ||
} | ||
|
||
export function downloadScreenshot( | ||
htmlElement, | ||
quality = 0.95, | ||
targetResolution = { width: 3840, height: 2160 }, | ||
pixelRatio = 1, | ||
filter = () => true, | ||
filename = `Canvas_${formatDate(new Date())}.png`, | ||
) { | ||
const options = getOptions(htmlElement, targetResolution, quality, pixelRatio, filter); | ||
|
||
htmlToImage.toBlob(htmlElement, options).then((blob) => { | ||
const link = document.createElement("a"); | ||
link.download = filename; | ||
link.href = window.URL.createObjectURL(blob); | ||
link.click(); | ||
}); | ||
} | ||
|
||
function getResolutionFixedRatio(htmlElement, target) { | ||
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, | ||
}; | ||
} |
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