Socket
Socket
Sign inDemoInstall

virtual-bg

Package Overview
Dependencies
1
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.1 to 0.0.4

9

index.d.ts

@@ -1,5 +0,6 @@

export function start(
videoElement: HTMLVideoElement,
onResults: Function,
modelSelection: number
export function blurBackground(
inputVideoElement: HTMLVideoElement,
outputCanvasElement: HTMLCanvasElement,
blurIntensity?: number,
modelSelection?: number
): any;

@@ -9,3 +9,19 @@ const script = document.createElement('script');

export async function start(videoElement, onResults, modelSelection) {
const foregroundCanvasElement = document.createElement('canvas');
const backgroundCanvasElement = document.createElement('canvas');
let outputCanvasCtx = null;
export async function blurBackground(
inputVideoElement,
outputCanvasElement,
blurIntensity = 10,
modelSelection = 0
) {
foregroundCanvasElement.width = backgroundCanvasElement.width =
outputCanvasElement.width;
foregroundCanvasElement.height = backgroundCanvasElement.height =
outputCanvasElement.height;
outputCanvasCtx = outputCanvasElement.getContext('2d');
let selfieSegmentation = new SelfieSegmentation({

@@ -19,3 +35,10 @@ locateFile: (file) => {

});
selfieSegmentation.onResults(onResults);
selfieSegmentation.onResults((results) => {
mergeForegroundBackground(
foregroundCanvasElement,
backgroundCanvasElement,
blurIntensity,
results
);
});

@@ -26,6 +49,6 @@ let myStream = await navigator.mediaDevices.getUserMedia({

videoElement.srcObject = myStream;
videoElement.addEventListener('play', () => {
inputVideoElement.srcObject = myStream;
inputVideoElement.addEventListener('play', () => {
async function step() {
await selfieSegmentation.send({ image: videoElement });
await selfieSegmentation.send({ image: inputVideoElement });
requestAnimationFrame(step);

@@ -35,3 +58,45 @@ }

});
videoElement.addEventListener('pause', async () => {});
inputVideoElement.addEventListener('pause', async () => {});
}
function mergeForegroundBackground(
foregroundCanvasElement = foregroundCanvasElement,
backgroundCanvasElement = backgroundCanvasElement,
blurIntensity,
results
) {
makeCanvasLayer(results, foregroundCanvasElement, 'foreground');
makeCanvasLayer(results, backgroundCanvasElement, 'background');
outputCanvasCtx.drawImage(backgroundCanvasElement, 0, 0);
const backgroundCanvasCtx = backgroundCanvasElement.getContext('2d');
backgroundCanvasCtx.filter = `blur(${blurIntensity}px)`;
outputCanvasCtx.drawImage(foregroundCanvasElement, 0, 0);
}
function makeCanvasLayer(results, canvasElement, type) {
const canvasCtx = canvasElement.getContext('2d');
canvasCtx.save();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
canvasCtx.drawImage(
results.segmentationMask,
0,
0,
canvasElement.width,
canvasElement.height
);
if (type === 'foreground') {
canvasCtx.globalCompositeOperation = 'source-in';
}
canvasCtx.drawImage(
results.image,
0,
0,
canvasElement.width,
canvasElement.height
);
canvasCtx.restore();
}
{
"name": "virtual-bg",
"version": "0.0.1",
"description": "",
"version": "0.0.4",
"description": "Easily add virtual background effects to your video/camera input inside any web browser",
"repository": {
"type": "git",
"url": "https://github.com/akhil-rana/virtual-bg"
},
"homepage": "https://github.com/akhil-rana/virtual-bg",
"main": "index.js",

@@ -9,3 +14,4 @@ "scripts": {

},
"author": "",
"author": "Akhil Rana",
"typings": "./index.d.ts",
"license": "ISC",

@@ -12,0 +18,0 @@ "dependencies": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc