# virtual-bg

> Easily add virtual background effects to your video/camera input inside any web browser

Latest version **0.1.21** (published 2023-09-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install virtual-bg
pnpm add virtual-bg
yarn add virtual-bg
bun add virtual-bg
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.21 |
| Published | 2023-09-19 |
| First published | 2021-08-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 23 |
| Author | Akhil Rana |
| Maintainers | akhil-rana |
| Keywords | virtual, background, blur, mediapipe, bodypix, selfie, segment, segmentation, camera, webrtc, mediastream, canvas |

## Links

- npm: https://www.npmjs.com/package/virtual-bg
- Repository: https://github.com/akhil-rana/virtual-bg
- Issues: https://github.com/akhil-rana/virtual-bg/issues
- npm.io page: https://npm.io/package/virtual-bg

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 0.1.21 (latest) — 2023-09-19
- 0.1.2 (beta) — 2023-09-19
- 0.1.1 — 2021-08-29
- 0.1.0 — 2021-08-28
- 0.0.51 — 2021-08-27
- 0.0.5 — 2021-08-27
- 0.0.48 — 2021-08-27
- 0.0.47 — 2021-08-27
- 0.0.46 — 2021-08-27
- 0.0.45 — 2021-08-27
- 0.0.44 — 2021-08-27
- 0.0.43 — 2021-08-26
- 0.0.42 — 2021-08-26
- 0.0.41 — 2021-08-26
- 0.0.4 — 2021-08-26
- … 1 more at https://npm.io/package/virtual-bg/versions

## README

## Easily add virtual background effects to your video/camera input inside any web browser<br/>
#### Try a working demo [here](http://demo.virtualbg.akhilrana.com/)<br/><br/>

Install from npm using:

    npm install virtual-bg

or include directly in HTML:
```html
<script src="https://cdn.jsdelivr.net/npm/virtual-bg@latest/virtual-bg.min.js"></script>
```
### A simple example to apply blur/image background to camera input:<br/>
#### index.html
```html
<div>
    <video autoplay="true" id="inputVideoElement"></video>
    <canvas id="output_canvas"></canvas>
</div>
```

#### index.js
```js
import { segmentBackground, applyBlur, applyImageBackground } from'virtual-bg';

const inputVideoElement = document.querySelector('#inputVideoElement');
const outputCanvasElement = document.querySelector('#output_canvas');

let myStream = await navigator.mediaDevices.getUserMedia({
      video: true
});

inputVideoElement.srcObject = myStream;

// segments foreground & background
segmentBackground(inputVideoElement, outputCanvasElement);  

// applies a blur intensity of 7px to the background 
applyBlur(7); 

// applies an image background
const image = new Image();
image.src = 'https://imageurl.jpg'
applyImageBackground(image);
```
### API:<br/>

#### To separate foreground from background: <br/>
```js
segmentBackground(inputVideoElement, outputCanvasElement, modelSelection <optional>); 
```
 Parameters: <br>
- <b>`inputVideoElement`:</b> <[HTMLVideoElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement)> Source of video which is to be used for applying segmentation.
- <b>`outputCanvasElement`:</b> <[HTMLCanvasElement](HTMLCanvasElement)> Canvas Element where output is to be displayed. 
- <b>`modelSelection`:</b> Numerical value to select type of model to be used for segmentation. <br/>0 : General <br/> 1 : Landscape (default)<br/> General (0) can be used when there is only 1 person in the view and not far away from the camera.<br/> Landscape works better if there are multiple people or distance from the camera is more.
#### To apply blur effect to background: <br/>
```js
applyBlur(blurIntensity <optional>); 
```
Parameters: </br>
- <b>`blurIntensity`:</b> Numerical value in pixels to apply a blur filter to the background. This uses the same filter as used in [CSS blur filter](https://developer.mozilla.org/en-US/docs/Web/CSS/filter-function/blur())<br/> _default = 7_
#### To apply image to background: <br/>
```js
applyImageBackground(imageElement); 
```
Parameters: </br>
- <b>`imageElement`:</b> <[HTMLImageElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement)> Image element to be set as background. You can create a new element dynamically using the [Image()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image) constructor.
#### To apply video to background: <br/>
```js
applyVideoBackground(videoElement); 
```
Parameters: </br>
- <b>`videoElement`:</b> <[HTMLVideoElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement)> Video element to be set as background.
#### To apply screen capture to background: <br/>
```js 
applyScreenBackground(screenCaptureStream); 
```
Parameters: </br>
- <b>`screenCaptureStream`:</b> <[MediaStream](https://developer.mozilla.org/en-US/docs/Web/API/MediaStream)> Stream obtained from the [Screen Capture API](https://developer.mozilla.org/en-US/docs/Web/API/Screen_Capture_API)
#### To change foreground type. Can be used to set foreground as a smaller snip at the bottom right corner of the output like a YouTube/Twitch stream. <br/>
An example use case for this can be when user has set screen capture as the background and want to present something.
```js 
changeForegroundType(foregroundType, offset<optional>); 
```
Parameters: </br>
- <b>`foregroundType`: </b> `normal | presenter` Type of foreground. Presenter mode shrinks the foreground and displays it in the bottom right corner.
- <b>`offset`: </b> Numerical value in pixels for setting custom offset when presenter mode is selected.<br> _For example_: -100 offset will move the foreground by 100 pixels to the right.

<br/><br/>

<b>Note</b>: I'm using  [mediapipe's selfie segmentation](https://www.npmjs.com/package/@mediapipe/selfie_segmentation) model and library as a base. 


For any doubts/feedback/suggestions, please start an issue/discussion at https://github.com/akhil-rana/virtual-bg or contact me at contact@akhilrana.com

---
_Source: https://npm.io/package/virtual-bg · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
