Skip to content
Snippets Groups Projects
Verified Commit 6e16459c authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: Improved camera capture functionality and UI responsiveness

- camera-capture.mjs
  - Added a private full-screen symbol to manage full-screen states and errors more effectively.
  - Streamlined the camera initialization process by ensuring the stream is stopped before fetching devices. This prevents excess resource usage.
  - Updated the camera selection dropdown to only show if there are available devices and made styling adjustments for better presentation.

- camera-capture.pcss
  - Adjusted the positioning of the full-screen element to sit more comfortably in the UI by changing top spacing from 2rem to 1rem for better alignment.

- camera-capture.mjs (stylesheet)
  - Reformatted import statements and simplified rule insertions for clarity and maintainability, promoting cleaner code practices.

These changes enhance user experience by preventing unnecessary resource use, improving UI layout, and ensuring that errors are handled gracefully.
parent 8f5342f3
No related branches found
No related tags found
No related merge requests found
...@@ -43,6 +43,12 @@ const controlElementSymbol = Symbol("copyElement"); ...@@ -43,6 +43,12 @@ const controlElementSymbol = Symbol("copyElement");
*/ */
const videoElementSymbol = Symbol("videoElement"); const videoElementSymbol = Symbol("videoElement");
/**
* @private
* @type {symbol}
*/
const fullScreenElementSymbol = Symbol("fullScreenElement");
/** /**
* @private * @private
* @type {symbol} * @type {symbol}
...@@ -206,11 +212,18 @@ function initCameraControl() { ...@@ -206,11 +212,18 @@ function initCameraControl() {
!navigator.mediaDevices || !navigator.mediaDevices ||
!navigator.mediaDevices.getUserMedia !navigator.mediaDevices.getUserMedia
) { ) {
self[fullScreenElementSymbol].style.display = "none";
addErrorAttribute(self, "Browser not supported"); addErrorAttribute(self, "Browser not supported");
return; return;
} }
navigator.mediaDevices.enumerateDevices().then((devices) => { navigator.mediaDevices
.getUserMedia({ video: true })
.then((stream) => {
stream.getTracks().forEach((track) => track.stop());
return navigator.mediaDevices.enumerateDevices();
})
.then((devices) => {
const cameras = devices.filter((device) => device.kind === "videoinput"); const cameras = devices.filter((device) => device.kind === "videoinput");
if (cameras.length === 0) { if (cameras.length === 0) {
...@@ -218,16 +231,20 @@ function initCameraControl() { ...@@ -218,16 +231,20 @@ function initCameraControl() {
return; return;
} }
// Nur Dropdown anzeigen, wenn mehr als 1 Kamera vorhanden if (cameras.length > 0) {
if (cameras.length > 1) {
const select = document.createElement("select"); const select = document.createElement("select");
select.setAttribute("data-monster-role", "cameraSelector"); select.setAttribute("data-monster-role", "cameraSelector");
select.style.marginBottom = "0.5rem"; select.style.marginBottom = "0.5rem";
select.style.width = "100%";
select.style.maxWidth = "100%";
select.style.height = "1.4rem";
select.style.marginTop = "0";
select.style.marginBottom = "0";
cameras.forEach((camera, index) => { cameras.forEach((camera, index) => {
const option = document.createElement("option"); const option = document.createElement("option");
option.value = camera.deviceId; option.value = camera.deviceId;
option.text = camera.label || `Kamera ${index + 1}`; option.text = camera.label || `Camera ${index + 1}`;
select.appendChild(option); select.appendChild(option);
}); });
...@@ -235,15 +252,19 @@ function initCameraControl() { ...@@ -235,15 +252,19 @@ function initCameraControl() {
startCameraWithDeviceId.call(self, select.value); startCameraWithDeviceId.call(self, select.value);
}); });
// Vor dem Video-Element einfügen self[fullScreenElementSymbol].style.top = "2rem";
self[controlElementSymbol].insertBefore(select, self[videoElementSymbol]); self[controlElementSymbol].insertBefore(select, self[videoElementSymbol]);
} }
// Mit der ersten Kamera starten
startCameraWithDeviceId.call(self, cameras[0].deviceId); startCameraWithDeviceId.call(self, cameras[0].deviceId);
})
.catch((err) => {
self[fullScreenElementSymbol].style.display = "none";
addErrorAttribute(self, err);
}); });
} }
function startCameraWithDeviceId(deviceId) { function startCameraWithDeviceId(deviceId) {
const self = this; const self = this;
navigator.mediaDevices navigator.mediaDevices
...@@ -254,8 +275,11 @@ function startCameraWithDeviceId(deviceId) { ...@@ -254,8 +275,11 @@ function startCameraWithDeviceId(deviceId) {
self[emptyHistoryStateElementSymbol].style.display = "none"; self[emptyHistoryStateElementSymbol].style.display = "none";
self[videoElementSymbol].srcObject = stream; self[videoElementSymbol].srcObject = stream;
self[fullScreenElementSymbol].style.display = "block";
}) })
.catch(function (e) { .catch(function (e) {
self[fullScreenElementSymbol].style.display = "none";
addErrorAttribute(self, e); addErrorAttribute(self, e);
}); });
} }
...@@ -444,10 +468,13 @@ function initControlReferences() { ...@@ -444,10 +468,13 @@ function initControlReferences() {
this[canvasElementSymbol] = this.shadowRoot.querySelector(`canvas`); this[canvasElementSymbol] = this.shadowRoot.querySelector(`canvas`);
// data-monster-role="emptyHistoryState"
this[emptyHistoryStateElementSymbol] = this.shadowRoot.querySelector( this[emptyHistoryStateElementSymbol] = this.shadowRoot.querySelector(
`[data-monster-role="emptyHistoryState"]`, `[data-monster-role="emptyHistoryState"]`,
); );
this[fullScreenElementSymbol] = this.shadowRoot.querySelector(
`[data-monster-role="full-screen"]`,
);
} }
/** /**
...@@ -458,7 +485,7 @@ function getTemplate() { ...@@ -458,7 +485,7 @@ function getTemplate() {
// language=HTML // language=HTML
return ` return `
<div data-monster-role="control" part="control"> <div data-monster-role="control" part="control">
<monster-full-screen part="full-screen" data-monster-role="full-screen" data-monster-option-selector="[data-monster-role=control]"></monster-full-screen> <monster-full-screen part="full-screen" style="display: none" data-monster-role="full-screen" data-monster-option-selector="[data-monster-role=control]"></monster-full-screen>
<monster-state data-monster-role="emptyHistoryState"> <monster-state data-monster-role="emptyHistoryState">
<svg slot="visual" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" <svg slot="visual" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="350" width="350"
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
[data-monster-role="full-screen"] { [data-monster-role="full-screen"] {
position:absolute; position:absolute;
top:2rem; top:1rem;
right:1rem; right:1rem;
z-index: var(--monster-z-index-dropdown); z-index: var(--monster-z-index-dropdown);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment