Is there a screenshot plug-in implemented in react?

藏色散人
Release: 2022-12-30 11:12:35
Original
2968 people have browsed it

There are screenshot plug-ins in react, such as the react screenshot component "react-cropper", which can realize the image cropping function. The method of use is: first install "react-cropper"; then obtain it through getCroppedCanvas and other methods Cropped pictures are enough.

Is there a screenshot plug-in implemented in react?

The operating environment of this tutorial: windows7 system, react17.0.1 version, Dell G3 computer.

Related recommendations: "react tutorial"

How to use the react screenshot component react-cropper

When using React During development, we often encounter the situation of uploading images. If we want the uploaded images to meet certain specifications, we must crop the uploaded images on the client. At this time, we can use react-cropper. This image cropping component can help us easily implement the image cropping function.

The usage steps are as follows:

1. Installation:

npm install --save-dev react-cropper
Copy after login

2. The usage method is as follows:

import React from 'react' import Cropper from 'react-cropper' import 'cropperjs/dist/cropper.css' import {Button} from 'antd' export default class Crop extends React.Component { constructor() { super(); this.cropImage = this.cropImage.bind(this); } cropImage() { if (this.cropper.getCroppedCanvas() === 'null') { return false } this.props.getCropData(this.cropper.getCroppedCanvas().toDataURL()) } render() { return ( 
{ this.cropper = cropper; }} style={{height: 400, width: '100%'}} aspectRatio={246/346} guides={false} />
); } } import Cropper from 'react-cropper' import 'cropperjs/dist/cropper.css'
Copy after login

These two sentences introduce the Cropper component and its Style, Cropper component also has some commonly used attributes:

src: src is the src of the image to be cropped, usually the Base64 encoding of the image read by the upper component

aspectRatio: This is Control the proportion of the cropped image

There is also a button at the bottom of the cropping box to confirm whether to crop. From the above we can see its bound events:

cropImage() { if (this.cropper.getCroppedCanvas() === 'null') { return false } this.props.getCropData(this.cropper.getCroppedCanvas().toDataURL()) }
Copy after login

this.cropper allows us to use The ref attribute of react saves the reference of the DOM node of the Cropper component. If you are not sure, you can check the official React documentation. This component provides a getCroppedCanvas() method. This method returns the cropped image. We can use

The toDataURL() method converts it into Base64 encoding and uploads it to the upper-level component.

The above is the detailed content of Is there a screenshot plug-in implemented in react?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!