1.0.18 • Published 7 years ago

js-webcam v1.0.18

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
7 years ago

js-webcam

A simple core polyfill for access to user media camera

Using from react

First install the package

npm i js-webcam

Then you can use it from any component:

import React, { Component } from 'react';
import Webcam from 'js-webcam';

const WIDTH = 620;
const HEIGHT = 440;

class Selfie extends Component {
  componentDidMount() {
    this.trackerContext = this.trackerCanvas.getContext('2d');
    this.webcam = new Webcam(WIDTH, HEIGHT);
    this.webcam.start();
    this.startLoop();
  }

  componentWillUnmount() {
    this.stopLoop();
  }

  webcam;
  trackerContext;
  frameId;

  startLoop() {
    if (!this.frameId) {
      this.frameId = window.requestAnimationFrame(() => this.loop());
    }
  }

  loop() {
    this.update();
    this.frameId = window.requestAnimationFrame(() => this.loop());
  }

  stopLoop() {
    window.cancelAnimationFrame(this.frameId);
  }

  update() {
    this.trackerContext.clearRect(0, 0, WIDTH, HEIGHT);
    this.trackerContext.drawImage(this.webcam.domElement, 0, 0, WIDTH, HEIGHT);
  }

  render() {
    return (
      <div>
        <canvas ref={(c) => { this.trackerCanvas = c; }} width={WIDTH} height={HEIGHT} />
      </div>
    );
  }
}

const mapStateToProps = state => state;

export default Selfie;

The start function activate the frontal camera, if you need the back camera you can use startBackCamera instead.

    this.webcam.startBackCamera();

Or you can use the start function passing the facing mode parameter, on error and on success functions:

    const onError = (e) => {}; // call when the camera can not activate.
    const onSuccess = () => {}; // call when the camera can activate successfully
    const facingMode = 'user'; // or 'environment' for back camera.
    this.webcam.start(onSuccess, onError, facingMode);
1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago