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

usescreenshot-react

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

usescreenshot-react

React hook allows you to take screenshots of webpages or parts of it

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by33.33%
Maintainers
1
Weekly downloads
 
Created
Source

usescreenshot-react

React hook allows you to take screenshots of webpages or parts of it

NPM Package

Install

Using npm

npm install --save usescreenshot-react

Usage

import {useScreenshot, createFileName} from "usescreenshot-react";

Example

See example folder

import React, {useRef} from "react";
import {useScreenshot} from 'usescreenshot-react';

const Example = () => {
    const {image, takeScreenshot, isLoading, isError} = useScreenshot();
    const ref = useRef<HTMLDivElement>(null);
    
    const getImage = () => {
        if (!ref.current) {
            return
        }
        takeScreenshot(ref.current, {
            backgroundColor: null,
            logging: false,
        }).catch(console.log);
    }
    
    return isLoading ? (
        <div>Loading...</div>
    ) : (
        <div>
            {isError && <p>Error</p>}
            <div ref={ref}/>
            <h1>Capture Me</h1>
            {image && <img src={image} alt={'Screenshot'}/>}
            <button onClick={getImage}>Take screenshot</button>
        </div>
    );
}

API

createFileName(extension: string, name?: string): string - return file name. If the name is not specified, then the current date is returned

useScreenshot(type?: string, quality?: number)

  • type - String indicating the image format. The default format type is image/png
  • quality - A number between 0 and 1

HTMLCanvasElement.toDataURL()

The use useScreenshot hook returns an object containing the following properties:

  • image: string | undefined - Screenshot in base64 format
  • takeScreenshot = (captureRef: HTMLElement, options?: Options) => Promise<string> - Function for creating screenshot from html node and return image string
  • isLoading: boolean - Indicates if the screenshot is loading
  • isError: boolean - Indicates whether an error occurred during screenshot loading
  • clear = (): void - Clear screenshot string

takeScreenshot(captureRef: HTMLElement, options?: Options)

  • captureRef - Ref to the HTMLElement for which to for which the screenshot should be taken
  • options - configuration html2canvas options to take a screenshot

License

MIT © n0mver

Keywords

FAQs

Package last updated on 26 Oct 2021

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