Skip to content
Snippets Groups Projects
Commit 628c5025 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Move the video element creation into its own function

parent 4bd285dc
No related branches found
No related tags found
No related merge requests found
...@@ -63,6 +63,38 @@ async function getVideoDevices() { ...@@ -63,6 +63,38 @@ async function getVideoDevices() {
} }
} }
/**
* @param {string} deviceId
* @returns {object|null} a video element or null
*/
async function createVideoElement(deviceId) {
let videoId = deviceId;
let constraint = { video: { deviceId: videoId } };
if ( (videoId === 'environment' || videoId === '') ) {
console.log("vid:", videoId);
constraint = { video: { facingMode: "environment" } };
} else if ( videoId === 'user' ) {
console.log("vid2:", videoId);
constraint = { video: { facingMode: "user" } };
}
let stream = null;
try {
stream = await navigator.mediaDevices.getUserMedia(constraint);
} catch(e) {
console.log(e);
}
if (stream !== null) {
let video = document.createElement("video");
video.srcObject = stream;
return video;
}
return null;
}
/** /**
* Notification web component * Notification web component
...@@ -164,45 +196,21 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -164,45 +196,21 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
this.stopScanning(); this.stopScanning();
this.stopScan = false; this.stopScan = false;
this._askPermission = true;
let video = document.createElement("video");
let canvasElement = this._("#canvas"); let canvasElement = this._("#canvas");
let qrContainer = this._("#qr"); let qrContainer = this._("#qr");
let scroll = false; let scroll = false;
let okColor = getComputedStyle(this).getPropertyValue('--dbp-success-bg-color'); this._askPermission = true;
let notOkColor = getComputedStyle(this).getPropertyValue('--dbp-danger-bg-color');
let videoId = this._activeCamera;
let constraint = { video: { deviceId: videoId } };
if ( (videoId === 'environment' || videoId === '') ) {
console.log("vid:", videoId);
constraint = { video: { facingMode: "environment" } };
} else if ( videoId === 'user' ) {
console.log("vid2:", videoId);
constraint = { video: { facingMode: "user" } };
}
let stream = null;
this._loadingMessage = i18n.t('no-camera-access'); this._loadingMessage = i18n.t('no-camera-access');
try { let video = await createVideoElement(this._activeCamera);
stream = await navigator.mediaDevices.getUserMedia(constraint);
} catch(e) {
console.log(e);
}
this._askPermission = false; this._askPermission = false;
if (stream !== null) { if (video !== null) {
video.srcObject = stream;
video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen
video.play(); video.play();
this._videoRunning = true; this._videoRunning = true;
if (this._requestID !== null) {
cancelAnimationFrame(this._requestID);
this._requestID = null;
}
console.assert(this._requestID === null); console.assert(this._requestID === null);
this._videoElement = video; this._videoElement = video;
this._loading = true; this._loading = true;
...@@ -264,10 +272,13 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -264,10 +272,13 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
canvas.fill(); canvas.fill();
canvas.beginPath(); canvas.beginPath();
if (code) if (code) {
let okColor = getComputedStyle(that).getPropertyValue('--dbp-success-bg-color');
let notOkColor = getComputedStyle(that).getPropertyValue('--dbp-danger-bg-color');
canvas.fillStyle = matched ? okColor : notOkColor; canvas.fillStyle = matched ? okColor : notOkColor;
else } else {
canvas.fillStyle = 'white'; canvas.fillStyle = 'white';
}
let borderWidth = Math.max(maskWidth, maskHeight) / 50; let borderWidth = Math.max(maskWidth, maskHeight) / 50;
canvas.moveTo(maskStartX,maskStartY); canvas.moveTo(maskStartX,maskStartY);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment