Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
devicecam background: remove duplicate code
Browse files Browse the repository at this point in the history
Change-Id: I06c7d0ccbe98fe40f125cf35d02ae0dfb42ebf50
  • Loading branch information
andi34 committed Jun 1, 2020
1 parent 8dbca61 commit b2360f9
Showing 1 changed file with 22 additions and 45 deletions.
67 changes: 22 additions & 45 deletions resources/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const photoBooth = (function () {
resultPage.hide();
startPage.addClass('open');
if (config.previewCamBackground) {
public.startPreview();
public.startVideo('preview');
}
}

Expand All @@ -105,38 +105,10 @@ const photoBooth = (function () {
$('#mySidenav').toggleClass('sidenav--open');
}

public.startPreview = function () {
if (!navigator.mediaDevices) {
return;
}

const getMedia = (navigator.mediaDevices.getUserMedia || navigator.mediaDevices.webkitGetUserMedia || navigator.mediaDevices.mozGetUserMedia || false);

if (!getMedia) {
return;
}

if (config.previewCamFlipHorizontal) {
$('#video--preview').addClass('flip-horizontal');
}

getMedia.call(navigator.mediaDevices, webcamConstraints)
.then(function (stream) {
$('#video--preview').show();
videoPreview.srcObject = stream;
public.stream = stream;
wrapper.css('background-image', 'none');
wrapper.css('background-color', 'transparent');
})
.catch(function (error) {
console.log('Could not get user media: ', error)
});
}

public.startVideo = function () {
public.startVideo = function (mode) {

if (config.previewCamBackground) {
public.stopPreview();
public.stopVideo('preview');
}

if (!navigator.mediaDevices) {
Expand All @@ -151,32 +123,37 @@ const photoBooth = (function () {

if (config.previewCamFlipHorizontal) {
$('#video--view').addClass('flip-horizontal');
$('#video--preview').addClass('flip-horizontal');
}

getMedia.call(navigator.mediaDevices, webcamConstraints)
.then(function (stream) {
$('#video--view').show();
videoView.srcObject = stream;
if (mode === 'preview') {
$('#video--preview').show();
videoPreview.srcObject = stream;
public.stream = stream;
wrapper.css('background-image', 'none');
wrapper.css('background-color', 'transparent');
} else {
$('#video--view').show();
videoView.srcObject = stream;
}
public.stream = stream;
})
.catch(function (error) {
console.log('Could not get user media: ', error)
});
}

public.stopVideo = function () {
public.stopVideo = function (mode) {
if (public.stream) {
const track = public.stream.getTracks()[0];
track.stop();
$('#video--view').hide();
}
}

public.stopPreview = function () {
if (public.stream) {
const track = public.stream.getTracks()[0];
track.stop();
$('#video--preview').hide();
if (mode === 'preview') {
$('#video--preview').hide();
} else {
$('#video--view').hide();
}
}
}

Expand All @@ -193,7 +170,7 @@ const photoBooth = (function () {
}

if (config.previewFromCam) {
public.startVideo();
public.startVideo('view');
}

if (config.previewFromIPCam) {
Expand Down Expand Up @@ -253,7 +230,7 @@ const photoBooth = (function () {
videoSensor.height = videoView.videoHeight;
videoSensor.getContext('2d').drawImage(videoView, 0, 0);
}
public.stopVideo();
public.stopVideo('view');
}

if (config.previewFromIPCam) {
Expand Down

0 comments on commit b2360f9

Please sign in to comment.