Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@georapbox/capture-photo-element

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@georapbox/capture-photo-element

A custom element that implements the MediaDevices.getUserMedia() method of the MediaDevices interface to capture a photo in the browser.

  • 1.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
13
decreased by-18.75%
Maintainers
1
Weekly downloads
 
Created
Source

npm version npm license

<capture-photo>

A custom element that implements the MediaDevices.getUserMedia() method of the MediaDevices interface to capture a photo in the browser.

API documentationDemo

Install

$ npm install --save @georapbox/capture-photo-element

Usage

Script

import { CapturePhoto } from './node_modules/@georapbox/capture-photo-element/dist/capture-photo.min.js';

// Manually define the element.
CapturePhoto.defineCustomElement();

Alternatively, you can import the automatically defined custom element.

import './node_modules/@georapbox/capture-photo-element/dist/capture-photo-defined.min.js';

Markup

<capture-photo facing-mode="environment" camera-resolution="320x240"></capture-photo>

Style

By default, the component is style-free to remain as less opinionated as possible. However, you can style the various elements of the component using the ::part() CSS pseudo-elements provided for this purpose. Below are demonstrated all available parts for styling.

capture-photo::part(video) {
  /* The video element */
}

capture-photo::part(actions-container) {
  /* Actions container element - where actions buttons are placed */
}

capture-photo::part(capture-button) {
  /* The button responsible to take picture */
}

capture-photo::part(facing-mode-button) {
  /* The button responsible to change camera's facing mode */
}

capture-photo::part(output-container) {
  /* Output container - where the final output photo is placed */  
}

capture-photo::part(output-image) {
  /* The generated photo */
}

API

Properties

NameReflectsTypeDefaultDescription
outputDisabled
output-disabled
BooleanfalseOptional. Defines if the generated image is added in DOM. Use it if you don't need to display the generated image or if you need to display it somewhere else in DOM.
facingMode
facing-mode
StringnullOptional. The preferred camera to be used if the device supports more than one (mostly for mobile devices). Available values: "user" and "environment" for the front and the rear camera accordingly.
cameraResolution
camera-resolution
StringnullOptional. Defines the ideal camera resolution constraint. It must be of the format [width]x[height], eg 640x480. The browser will try to honour this, but may return other resolutions if an exact match is not available. Please refer to constraints documentation for more details of how constraints work.
zoomNumbernullOptional. Defines the camera's zoom level if supported by the device.
loadingBooleanfalseReadonly. Indicates if the component is ready for interaction. It is used internally but is also exposed as a readonly property for purposes such as styling, etc.

Slots

NameDescription
capture-buttonOverride the default capture photo button with your own.
capture-button-contentOverride the default content of the capture photo button with your own content.
facing-mode-buttonOverride the default facing mode button with your own.
facing-mode-button-contentOverride the default content of the facing mode button with your own content.
Slots usage examples
Override the default buttons with your own elements
<capture-photo>
  <button slot="capture-button" type="button">
    Take picture
  </button>
  
  <a slot="facing-mode-button" href="#" role="button">
    Change camera
  </a>
</capture-photo>
Override just the content of the default buttons
<capture-photo>
  <span slot="capture-button-content">Take picture</span>
  
  <span slot="facing-mode-button-content">Change camera</span>
</capture-photo>

CSS Parts

NameDescription
videoThe video element.
actions-containerThe action buttons container element.
capture-buttonThe capture photo button.
facing-mode-buttonThe facing mode button.
output-containerThe output container element.
output-imageThe output image element.

Methods

NameTypeDescriptionArguments
defineCustomElementStaticDefines/registers the custom element with the name provided. If no name is provided, the default name is used. The method checks if the element is already defined, hence will skip trying to redefine it.elementName='capture-photo'
isSupportedStaticReturns true if MediaDevices.getUserMedia() is supported by the platform, otherwise returns false.-
capturePrototypeCaptures a photo using the element's properties.-

Events

NameDescriptionEvent Detail
capture-photo:video-playEmitted when camera's video stream starts playing. It is triggered during initial load, or when facing mode or camera resolution mode change are requested.{ video: HTMLVideoElement }
capture-photo:facing-mode-changeEmitted when the camera's facing mode changes.{ facingMode: 'user' | 'environment' }
capture-photo:camera-resolution-changeEmitted when the camera's resolution changes.{ cameraResolution: String }
capture-photo:zoom-changeEmitted when the camera's zoom level changes.{ zoom: Number }
capture-photo:successEmitted when a photo is captured successfully.{ dataURI: String, width: Number, height: Number }
capture-photo:errorEmitted when an error occurs. An error might occur because camera permission is denied, a photo cannot be captured for any reason, the video stream cannot start for any reason, etc.{ error: DOMException }

Example

Below is a full usage example, with custom configuration and styling. Check the demo page for a demonstration.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>capture-photo-element demo</title>
  <style>
    capture-photo:not(:defined) {
      display: none;
    }

    capture-photo {
      overflow: hidden;
    }

    capture-photo::part(video) {
      width: 100%;
      padding: 1rem 1rem 0 1rem;
      background-color: #000000;
    }

    capture-photo::part(actions-container) {
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 2rem;
      padding: 1rem 0;
      margin-bottom: 1rem;
      background-color: #000000;
    }

    capture-photo::part(capture-button) {
      min-width: 60px;
      width: 60px;
      height: 60px;
      background-color: #cccccc;
      border: 5px solid #383838;
      color: #000000;
      border-radius: 50%;
      font-size: 1rem;
      cursor: pointer;
      text-indent: -9999px;
      overflow: hidden;
      -webkit-appearance: none;
      -moz-appearance: none;
    }

    capture-photo::part(facing-mode-button) {
      margin-right: calc(-40px - 2rem); /* facing mode button width + actions buttons gap */
      min-width: 40px;
      width: 40px;
      height: 40px;
      background-color: #ffffff;
      border: 0;
      line-height: 1;
      border-radius: 50%;
      cursor: pointer;
      -webkit-appearance: none;
      -moz-appearance: none;
    }

    capture-photo::part(output-container) {
      overflow-x: auto;
    }

    capture-photo::part(output-image) {
      max-width: 300px;
      height: auto;
      border: 5px solid #000;
    }

    capture-photo[loading]::part(video) {
      background-image: url(assets/icons/spinner.svg);
      background-size: 60px;
      background-position: center center;
      background-repeat: no-repeat;
    }

    capture-photo[loading]::part(capture-button),
    capture-photo[loading]::part(facing-mode-button) {
      opacity: 0.7;
      cursor: not-allowed;
    }
  </style>
</head>
<body>
  <capture-photo>
    <span slot="facing-mode-button-content">
      <img src="assets/icons/reverse.svg" width="26" height="26" alt="Change facing mode">
    </span>
  </capture-photo>

  <script type="module">
    import { CapturePhoto } from './capture-photo.js';

    CapturePhoto.defineCustomElement();
  </script>
</body>
</html>

Changelog

For API updates and breaking changes, check the CHANGELOG.

Browser support

Browsers without native custom element support require a polyfill.

  • Firefox
  • Chrome
  • Microsoft Edge
  • Safari

License

The MIT License (MIT)

Keywords

FAQs

Package last updated on 20 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc