Skip to content
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

Add video recorder functionality #67

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions hydra-server/app/src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ class Menu {
this.sketches = obj.sketches
this.editor = obj.editor
this.hydra = obj.hydra
this.recorder = undefined
this.recordingStartTime = undefined
this.recordingInterval = undefined

// variables related to popup window
this.closeButton = document.getElementById("close-icon")
this.clearButton = document.getElementById("clear-icon")
this.shareButton = document.getElementById("share-icon")
this.recordingTime = document.getElementById("recording-time")
this.recorderButton = document.getElementById("recorder-icon")
this.shuffleButton = document.getElementById("shuffle-icon")
this.editorText = document.getElementsByClassName('CodeMirror-scroll')[0]

this.recorderButton.onclick = this.recordSketch.bind(this)
this.shuffleButton.onclick = this.shuffleSketches.bind(this)
this.shareButton.onclick = this.shareSketch.bind(this)
this.clearButton.onclick = this.clearAll.bind(this)
Expand Down Expand Up @@ -47,6 +53,45 @@ class Menu {
})
}

downloadRecording (e) {
var videoData = [ e.data ]
var blob = new Blob(videoData, {'type': 'video/webm'})
var videoURL = URL.createObjectURL(blob)
var link = document.createElement('a')
link.href = videoURL
link.download = "file.webm"
link.click()
}

setTime () {
let timeSinceStart = new Date().getTime() - this.recordingStartTime
let minutes = ""+Math.floor((timeSinceStart % (1000 * 60 * 60)) / (1000 * 60))
let seconds = ""+Math.floor((timeSinceStart % (1000 * 60)) / 1000)
this.recordingTime.innerText = minutes.padStart(2,"0")
+ ":" + seconds.padStart(2, "0")
}

recordSketch (){
if (this.recordingStartTime) {
this.recordingStartTime = false
this.recorderButton.className = "far fa-dot-circle icon"
this.recorderButton.style = ""
this.recordingTime.innerText = ""
clearInterval(this.recordingInterval)
this.recorder.stop()
} else {
this.recordingStartTime = new Date().getTime()
this.recordingInterval = setInterval(this.setTime.bind(this),1)
this.recorderButton.className = "fas fa-dot-circle icon"
this.recorderButton.style = "color:red"
let canvas = document.querySelector("canvas")
let canvasStream = canvas.captureStream(30)
this.recorder = new MediaRecorder(canvasStream, {'mimeType': 'video/webm', 'videoBitsPerSecond': 2500000})
this.recorder.ondataavailable = this.downloadRecording
this.recorder.start()
}
}

showConfirmation(successCallback, terminateCallback) {
var c = prompt("Pressing OK will share this sketch to \nhttps://twitter.com/hydra_patterns.\n\nInclude your name or twitter handle (optional):")
console.log('confirm value', c)
Expand Down
50 changes: 50 additions & 0 deletions hydra-server/public/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,20 @@ class Menu {
this.sketches = obj.sketches
this.editor = obj.editor
this.hydra = obj.hydra
this.recorder = undefined
this.recordingStartTime = false
this.recordingInterval = false

// variables related to popup window
this.closeButton = document.getElementById("close-icon")
this.clearButton = document.getElementById("clear-icon")
this.shareButton = document.getElementById("share-icon")
this.recordingTime = document.getElementById("recording-time")
this.recorderButton = document.getElementById("recorder-icon")
this.shuffleButton = document.getElementById("shuffle-icon")
this.editorText = document.getElementsByClassName('CodeMirror-scroll')[0]

this.recorderButton.onclick = this.recordSketch.bind(this)
this.shuffleButton.onclick = this.shuffleSketches.bind(this)
this.shareButton.onclick = this.shareSketch.bind(this)
this.clearButton.onclick = this.clearAll.bind(this)
Expand Down Expand Up @@ -612,6 +618,50 @@ class Menu {
})
}

downloadRecording (e) {
var videoData = [ e.data ]
var blob = new Blob(videoData, {'type': 'video/webm'})
var videoURL = URL.createObjectURL(blob)
var link = document.createElement('a')
link.href = videoURL
link.download = "file.webm"
link.click()
}

setTime () {
console.log("time!")
let timeSinceStart = new Date().getTime() - this.recordingStartTime
let minutes = ""+Math.floor((timeSinceStart % (1000 * 60 * 60)) / (1000 * 60))
let seconds = ""+Math.floor((timeSinceStart % (1000 * 60)) / 1000)
this.recordingTime.innerText = minutes.padStart(2,"0") + ":" + seconds.padStart(2, "0")
}

recordSketch (){
if (this.recordingStartTime) {
console.log("stop recording")
this.recordingStartTime = false
this.recorderButton.className = "far fa-dot-circle icon"
this.recorderButton.style = ""
this.recordingTime.innerText = ""
clearInterval(this.recordingInterval)
this.recorder.stop()
} else {
console.log("start recording")
this.recordingStartTime = new Date().getTime()
this.recordingInterval = setInterval(this.setTime.bind(this),1)
this.recorderButton.className = "fas fa-dot-circle icon"
this.recorderButton.style = "color:red"
let canvas = document.querySelector("canvas")
let canvasStream = canvas.captureStream(30)
this.recorder = new MediaRecorder(canvasStream, {'mimeType': 'video/webm', 'videoBitsPerSecond': 2500000})
// this.recorder.setVideoSize(canvas.width, canvas.height)
// this.recorder.setVideoFrameRate(30)
// this.recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
this.recorder.ondataavailable = this.downloadRecording
this.recorder.start()
}
}

showConfirmation(successCallback, terminateCallback) {
var c = prompt("Pressing OK will share this sketch to \nhttps://twitter.com/hydra_patterns.\n\nInclude your name or twitter handle (optional):")
console.log('confirm value', c)
Expand Down
2 changes: 2 additions & 0 deletions hydra-server/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
<div id="modal-header">
<div><!--<i class="fas fa-bars icon"></i>--></div>
<div>
<i id="recording-time" style="font-family:monospace"></i>
<i id="recorder-icon" title="start recording" class="far fa-dot-circle icon" aria-hidden="true"></i>
<i id="share-icon" title="upload to gallery" class="fas fa-upload icon" aria-hidden="true"></i>
<i id="clear-icon" title="clear all" class="fa fa-trash icon" aria-hidden="true"></i>
<i id="shuffle-icon" title="show random sketch" class="fas fa-random icon" aria-hidden="true"></i>
Expand Down