github:https://github.com/react-cropper/react-cropper
doc:https://react-cropper.github.io/react-cropper/

效果

cropper.png

Quick Example

  1. import React, {Component} from 'react';
  2. import Cropper from 'react-cropper';
  3. import 'cropperjs/dist/cropper.css'; // see installation section above for versions of NPM older than 3.0.0
  4. // If you choose not to use import, you need to assign Cropper to default
  5. // var Cropper = require('react-cropper').default
  6. class Demo extends Component {
  7. _crop = () => {
  8. // image in dataUrl
  9. console.log(this.cropper.getCroppedCanvas().toDataURL(), 'base64格式');
  10. // image in blob
  11. const cvs = this.cropper.getCroppedCanvas({
  12. width: 540,
  13. });
  14. cvs.toBlob(blob => {
  15. console.log(blob, 'blob文件');
  16. });
  17. // 旋转照片
  18. this.cropper.rotate(-45);
  19. }
  20. onCropperInit = (cropper) => {
  21. this.cropper = cropper;
  22. }
  23. render() {
  24. return (
  25. <Cropper
  26. src="http://fengyuanchen.github.io/cropper/images/picture.jpg"
  27. style=
  28. // Cropper.js options
  29. initialAspectRatio={16 / 9}
  30. guides={false}
  31. crop={this._crop}
  32. onInitialized={this.onCropperInit}
  33. />
  34. );
  35. }
  36. }