Skip to content

Commit

Permalink
Add support save screenshot for MJPEG source
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 17, 2024
1 parent 626f8fe commit 2e6215e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions custom_components/webrtc/www/webrtc-camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,21 @@ class WebRTCCamera extends VideoRTC {
}

saveScreenshot() {
const canvas = document.createElement('canvas');
canvas.width = this.video.videoWidth;
canvas.height = this.video.videoHeight;
canvas.getContext('2d').drawImage(this.video, 0, 0, canvas.width, canvas.height);
const a = document.createElement('a');

if (this.video.videoWidth && this.video.videoHeight) {
const canvas = document.createElement('canvas');
canvas.width = this.video.videoWidth;
canvas.height = this.video.videoHeight;
canvas.getContext('2d').drawImage(this.video, 0, 0, canvas.width, canvas.height);
a.href = canvas.toDataURL('image/jpeg');
} else if (this.video.poster && this.video.poster.startsWith('data:image/jpeg')) {
a.href = this.video.poster;
} else {
return;
}

const ts = new Date().toISOString().substring(0, 19).replaceAll('-', '').replaceAll(':', '');
const a = document.createElement('a');
a.href = canvas.toDataURL('image/jpeg');
a.download = `snapshot_${ts}.jpeg`;
a.click();
}
Expand Down

0 comments on commit 2e6215e

Please sign in to comment.