usescreenshot-react
v1.2.2
Published
React hook allows you to take screenshots of webpages or parts of it
Downloads
13
Maintainers
Readme
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/pngquality
- A number between 0 and 1
The use useScreenshot
hook returns an object containing the following properties:
image: string | undefined
- Screenshot in base64 formattakeScreenshot = (captureRef: HTMLElement, options?: Options) => Promise<string>
- Function for creating screenshot from html node and return image stringisLoading: boolean
- Indicates if the screenshot is loadingisError: boolean
- Indicates whether an error occurred during screenshot loadingclear = (): void
- Clear screenshot string
takeScreenshot(captureRef: HTMLElement, options?: Options)
captureRef
- Ref to the HTMLElement for which to for which the screenshot should be takenoptions
- configuration html2canvas options to take a screenshot
License
MIT © n0mver