From 20a3d48b29ee3ec3f998897c5c6eef3a3b3da96a Mon Sep 17 00:00:00 2001 From: Wes Modes Date: Sat, 23 Dec 2023 09:31:34 -0800 Subject: [PATCH] Camera access modal --- eye/css/eye.css | 36 ++++++++++++++++++++++++++++++++++++ eye/index.html | 7 +++++++ eye/js/eye2.js | 38 +++++++++++++++++++++++++++----------- 3 files changed, 70 insertions(+), 11 deletions(-) diff --git a/eye/css/eye.css b/eye/css/eye.css index 1853f98..512e2b4 100644 --- a/eye/css/eye.css +++ b/eye/css/eye.css @@ -91,3 +91,39 @@ height: 100vh; border: solid 1px orange; } + +#modal { + position: absolute; + top: 0; + left: 0; + width: 100vw; + height: 100vh; + background-color: rgba(255,255,255,0.5); + display: flex; + justify-content: center; + align-items: center; +} + +#modal .content { + width: 40%; + height: 40%; + min-width: 200px; + min-height: 200px; + background-color: white; + border: solid 1px black; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + font-size: 1.5em; + text-align: center; +} + +#modal .content p { + margin: 0 0 0.5em 0; +} + +#modal .content .title { + font-size: 2em; + font-weight: bold; +} \ No newline at end of file diff --git a/eye/index.html b/eye/index.html index c7e3a34..425ac6c 100644 --- a/eye/index.html +++ b/eye/index.html @@ -27,5 +27,12 @@ + \ No newline at end of file diff --git a/eye/js/eye2.js b/eye/js/eye2.js index 46096a6..afee9dd 100644 --- a/eye/js/eye2.js +++ b/eye/js/eye2.js @@ -65,7 +65,7 @@ class Faces { this.displaySize = { width: $(this.canvasEl).width(), height: $(this.canvasEl).height() }; this.currentFaces = []; // Timestamp of the last face update - this.lastFaceUpdate = 0; + this.lastFaceUpdate = 0; } run() { @@ -82,11 +82,17 @@ class Faces { } async startVideo() { - // Start video stream - navigator.getUserMedia({ video: {} }, - stream => this.videoEl.srcObject = stream, - err => console.error(err) - ); + this.showCameraAccessModal(); // Show the modal before requesting camera access + + // Request camera access + try { + const stream = await navigator.mediaDevices.getUserMedia({ video: true }); + this.videoEl.srcObject = stream; + this.hideCameraAccessModal(); // Hide the modal on successful access + } catch (err) { + console.error("Camera access was denied or an error occurred:", err); + // Handle the error, maybe update the modal with an error message + } } async onPlay() { @@ -99,7 +105,7 @@ class Faces { // Detect faces and landmarks const detections = await faceapi.detectAllFaces(this.videoEl, new faceapi.TinyFaceDetectorOptions()); const now = Date.now(); - + if (detections.length) { // Clear the current faces this.currentFaces = []; @@ -185,6 +191,18 @@ class Faces { this.ctx.rect(topLeftX, topLeftY, faceRecord.width, faceRecord.height); this.ctx.stroke(); } + + // Method to show the camera access modal + showCameraAccessModal() { + // Assuming you have a modal with a specific ID or class + $("#modal").show(); + } + + // Method to hide the camera access modal + hideCameraAccessModal() { + $("#modal").hide(); + } + } /** @@ -483,16 +501,14 @@ class Eye { } // Document ready -// $(document).ready(function() { +$(document).ready(function() { const faceApp = new Faces(); faceApp.run(); - // Usage const eyeApp = new Eye(faceApp); eyeApp.run(); - // myEye.moveIrisRandomly(); -// }); +});