Skip to content

Commit

Permalink
Update background blur sample
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois authored Jan 25, 2023
1 parent 59bb5ed commit ea226e6
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions image-capture/background-blur.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
navigator.mediaDevices.getUserMedia({ video: true })
.then((mediaStream) => {
document.querySelector("video").srcObject = mediaStream;
.then((stream) => {
document.querySelector("video").srcObject = stream;

const [track] = mediaStream.getVideoTracks();
const [track] = stream.getVideoTracks();
const capabilities = track.getCapabilities();
const settings = track.getSettings();

Expand All @@ -11,27 +11,37 @@ navigator.mediaDevices.getUserMedia({ video: true })
}

// Listen to background blur changes.
track.onconfigurationchange = onconfigurationchange;
track.addEventListener("configurationchange", configurationChange);

// Check whether the user can toggle background blur or not.
// Check whether the user can toggle background blur in the web app.
if (capabilities.backgroundBlur?.length !== 2) {
throw Error(`Background blur toggle is not supported by ${track.label}`);
}

const toggleButton = document.querySelector("button");
toggleButton.onclick = () => {
const settings = track.getSettings();
track.applyConstraints({
advanced: [{ backgroundBlur: !settings.backgroundBlur }],
});
};
toggleButton.disabled = false;
const button = document.querySelector("button");
button.addEventListener("click", buttonClick);
button.disabled = false;
})
.catch((error) => log("Argh!", `${error}`));

function onconfigurationchange(event) {
function buttonClick() {
const stream = document.querySelector("video").srcObject;
const [track] = stream.getVideoTracks();
const settings = track.getSettings();
const constraints = {
advanced: [{ backgroundBlur: !settings.backgroundBlur }],
};
track.applyConstraints(constraints)
.then(() => {
const settings = track.getSettings();
log(`Background blur is now ${settings.backgroundBlur ? "ON" : "OFF"}`);
})
.catch((error) => log("Argh!", `${error}`));
}

function configurationChange(event) {
const settings = event.target.getSettings();
if ("backgroundBlur" in settings) {
log(`Background blur is now ${settings.backgroundBlur ? "ON" : "OFF"}`);
log(`Background blur changed to ${settings.backgroundBlur ? "ON" : "OFF"}`);
}
}

0 comments on commit ea226e6

Please sign in to comment.