npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsnapcam

v1.0.1

Published

A library that facilitates the integration of camera support into web applications.

Downloads

8

Readme

JSnapCam

The JSnapCam is a JavaScript class that facilitates webcam interaction and streaming. It provides methods to access and manage video input devices, start and stop webcam streaming, take photos, and more.

Table of Contents

Introduction

The JSnapCam simplifies webcam interactions in web applications. It allows you to:

  • Access and list available video input devices.
  • Start and stop webcam streaming.
  • Flip between front-facing and rear-facing cameras (if available).
  • Take photos and capture webcam frames as base64-encoded images.
  • Store and retrieve the last selected camera choice using local storage.

Installation

Use Git Clone

git https://github.com/iguilhermeluis/JSnapCam.git

OR Use NPM

NPM

npm install jsnapcam
# or
yarn add jsnapcam

To use the JSnapCam in your project, you can either include the source code directly in your JavaScript bundle or import the class from a module.

Usage

1. Include script tag in html

<script
  type="text/javascript"
  src="https://unpkg.com/jsnapcam/dist/jsnapcam.min.js"
></script>
or Import into javascript
import Webcam from "jsnapcam";

2. Place elements in HTML

<video id="webcam" autoplay playsinline width="640" height="480"></video>
<canvas id="canvas" class="d-none"></canvas>
<audio id="snapSound" src="audio/snap.wav" preload="auto"></audio>

3. Call constructor in javascript

const webcamElement = document.getElementById("webcam");
const canvasElement = document.getElementById("canvas");
const snapSoundElement = document.getElementById("snapSound");
const webcam = new Webcam(
  webcamElement,
  "user",
  canvasElement,
  snapSoundElement
);

4. Start Webcam

webcam
  .start()
  .then((result) => {
    console.log("webcam started");
  })
  .catch((err) => {
    console.log(err);
  });

5. Take Photo

var picture = webcam.snap();

6. Stop Webcam

webcam.stop();

API Reference

Constructor

constructor(
  webcamElement,
  facingMode,
  canvasElement,
  snapSoundElement,
  ratio,
  activateAutofocusIfItExists,
  rememberLastCameraChoice
);

Creates a new instance of the JSnapCam.

  • webcamElement: The HTML video element that will display the webcam stream.
  • `facingMode (optional): The camera's facing mode (either "user" or "environment"). Default is "user".
  • canvasElement (optional): The HTML canvas element for capturing images. Default is null.
  • snapSoundElement (optional): The HTML audio element to play a sound when taking a photo. Default is null.
  • ratio (optional): The aspect ratio for capturing images. Default is 16:9.
  • activateAutofocusIfItExists (optional): Enables continuous autofocus if supported. Default is true.
  • rememberLastCameraChoice (optional): Enable saving the last camera choice to local storage. Default is true.

Properties

  • facingMode - Gets or sets the current facing mode of the camera (either "user" or "environment").

  • webcamList - Gets the list of available video input devices (MediaDeviceInfo[]).

  • webcamCount - Gets the number of available video input devices.

  • selectedDeviceId - Gets or sets the ID of the selected video input device.

  • streamList- Gets the list of camera streams (MediaStream[]).

Methods

  • start(startStream) - Starts the webcam and begins streaming if specified.
  • stream() - Starts streaming the webcam to the video element.
  • stop() - Stops the webcam streaming and releases the media tracks.
  • snap() - Takes a photo from the webcam and returns the image as a base64 string.
  • flip() - Flips the camera's facing mode between "user" and "environment".
  • getInfoVideoInputs() - Gets information about available video input devices.

Contributing ✨

Contributions to the JSnapCam are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.

Thanks goes to these wonderful people (emoji key):

Credit

This project was built with inspiration and guidance from webcam-easy by Benson Ruan. Their work provided valuable insights and contributed to the development of this project.

License

The JSnapCam is licensed under the MIT License. Feel free to use it in your projects, both personal and commercial.