react-cropper截取图片

1、CropperCpn.js 

import React, {Component} from 'react';
import {Modal, Button} from 'antd';
import Cropper from 'react-cropper';
import 'cropperjs/dist/cropper.css';

export default class CropperCpn extends Component {
    state = {
        loading: false,
        height: 0,
        widht: 0,
    };
    dataUrl = '';

    _crop() {
        this.dataUrl = this.refs.cropper.getCroppedCanvas().toDataURL();
        let cropBox = document.querySelector('.cropper-crop-box');
        this.setState({
            height: cropBox.style.height,
            width: cropBox.style.width,
        })
    }

    handleSubmit = () => {
        this.props.sure(this.dataUrl);
    };

    render() {
        const style = {
            modal: {top: 0, paddingBottom: 0, margin: 0},
        };
        const {title, visible, content} = this.props;
        const {width, height} = this.state;
        return (
            <Modal title={title} width={1000} style={style.modal} visible={visible} centered
                // okText='确定'
                   closable={false}
                // cancelText='取消'
                // onCancel={this.props.onCancelPopup}
                   footer={<Button onClick={this.handleSubmit}>确定</Button>}
                   destroyOnClose={true}
                   confirmLoading={this.state.loading}
                // okButtonProps={{disabled: false}}
            >
                <Cropper
                    ref='cropper'
                    // src='https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2114375040,2314544443&fm=26&gp=0.jpg'
                    src={content}
                    style={{height: 'auto', width: '100%'}}
                    aspectRatio={16 / 9}
                    guides={false}
                    crop={this._crop.bind(this)}/>
                <p>宽度:{width} ,高度:{height}</p>
            </Modal>
        );
    }
}     

//使用方式
2、CropperUse.js 

import React, {Component} from 'react';
import {Modal, Button} from 'antd';
import Cropper from 'react-cropper';
import 'cropperjs/dist/cropper.css';

export default class CropperUse extends Component {
    state = {
        loading: false,
        height: 0,
        widht: 0,
    };
    dataUrl = '';

    _crop() {
        this.dataUrl = this.refs.cropper.getCroppedCanvas().toDataURL();
        let cropBox = document.querySelector('.cropper-crop-box');
        this.setState({
            height: cropBox.style.height,
            width: cropBox.style.width,
        })
    }

    handleSubmit = () => {
        this.props.sure(this.dataUrl);
    };

    render() {
        const style = {
            modal: {top: 0, paddingBottom: 0, margin: 0},
        };
        const {title, visible, content} = this.props;
        const {width, height} = this.state;
        return (
            <Modal title={title} width={1000} style={style.modal} visible={visible} centered
                // okText='确定'
                   closable={false}
                // cancelText='取消'
                // onCancel={this.props.onCancelPopup}
                   footer={<Button onClick={this.handleSubmit}>确定</Button>}
                   destroyOnClose={true}
                   confirmLoading={this.state.loading}
                // okButtonProps={{disabled: false}}
            >
                <Cropper
                    ref='cropper'
                    // src='https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2114375040,2314544443&fm=26&gp=0.jpg'
                    src={content}
                    style={{height: 'auto', width: '100%'}}
                    aspectRatio={16 / 9}
                    guides={false}
                    crop={this._crop.bind(this)}/>
                <p>宽度:{width} ,高度:{height}</p>
            </Modal>
        );
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值