Skip to content

Commit

Permalink
video recording
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Aug 11, 2023
1 parent 31f18a8 commit 9760346
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
5 changes: 3 additions & 2 deletions libs/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ folder.addButton({ title: 'export' }).on('click', () => {
})

folder.addButton({ title: 'reset' }).on('click', () => {
GUI.importState(defaultConfig)
localStorage.setItem('config', JSON.parse(defaultConfig))
if (!defaultConfig) return
GUI.importState(JSON.parse(defaultConfig))
localStorage.setItem('config', JSON.stringify(defaultConfig))
})

setTimeout(() => {
Expand Down
72 changes: 59 additions & 13 deletions libs/webgl/components/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useThree } from '@react-three/fiber'
import { useDebug } from '@studio-freight/hamo'
import { GUI } from 'libs/gui'
import { useCanvas } from 'libs/webgl/hooks/use-canvas'
import { useEffect, useMemo, useState } from 'react'
import { useEffect, useMemo, useRef, useState } from 'react'
import Dropzone from 'react-dropzone'
import { Image } from '../image'
import { Video } from '../video'
Expand All @@ -23,23 +23,69 @@ export function Content() {

const { size, gl } = useThree()

const mediaRecorderRef = useRef()

useEffect(() => {
const button = GUI.addButton({
title: 'export as image',
index: 10,
}).on('click', () => {
requestAnimationFrame(() => {
const link = document.createElement('a')
link.download = 'dithering.png'
link.href = gl.domElement.toDataURL()
link.click()
})
const exportFolder = GUI.addFolder({
title: 'export',
})

exportFolder
.addButton({
title: 'export as image',
index: 10,
})
.on('click', () => {
requestAnimationFrame(() => {
const link = document.createElement('a')
link.download = 'dithering.png'
link.href = gl.domElement.toDataURL()
link.click()
})
})

exportFolder
.addButton({
title: 'start recording',
})
.on('click', () => {
const videoStream = gl.domElement.captureStream(60)

mediaRecorderRef.current = new MediaRecorder(videoStream)
let chunks = []

mediaRecorderRef.current.addEventListener('dataavailable', (e) => {
chunks.push(e.data)
})

mediaRecorderRef.current.start()

mediaRecorderRef.current.addEventListener('stop', () => {
const blob = new Blob(chunks, { type: 'video/mp4' })
const videoURL = URL.createObjectURL(blob)
chunks = []
const link = document.createElement('a')
link.download = 'dithering.mp4'
link.href = videoURL
link.click()
})
})

exportFolder
.addButton({
title: 'stop recording',
})
.on('click', () => {
if (mediaRecorderRef.current) {
mediaRecorderRef.current.stop()
mediaRecorderRef.current = undefined
}
})

return () => {
button.dispose()
exportFolder.dispose()
}
}, [])
}, [gl])

const debug = useDebug()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function useDitheringEffect() {
.addBinding(CONFIG, 'granularity', {
min: 1,
step: 1,
max: 100,
max: 10,
label: 'granularity',
})
.on('change', ({ value }) => {
Expand Down

1 comment on commit 9760346

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"⚡️ Lighthouse report for the changes in this commit:

🔴 Performance: 43
🟠 Accessibility: 79
🟢 Best practices: 100
🟠 SEO: 67
🟢 PWA: 90

Lighthouse ran on https://gpu-dithering-b2teive47-studio-freight.vercel.app/"

Please sign in to comment.