react中使用截图组件Cropper组件
<p>--最近项目用react,学习react并使用cropper组件裁剪图片。</p><p>(这里开发组件不够统一有用tsx(TypeScript + xml/html)写的组件,有用jsx(javascript+xml/html)写的组件</p>
<p>前言:cropper组件引入到项目中的手顺直接看官方文档;github:https://github.com/fengyuanchen/cropperjs#methods 在线演示url: https://fengyuanchen.github.io/cropper/</p>
<p>1.cropper组件以及各种操作的简单封装。</p>
<p> react-cropper.js文件</p>
<div class="cnblogs_code">
<pre>import React, { Component } from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import PropTypes from </span>'prop-types'<span style="color: rgba(0, 0, 0, 1)">;
import Cropper from </span>'cropperjs'<span style="color: rgba(0, 0, 0, 1)">;
const optionProps </span>=<span style="color: rgba(0, 0, 0, 1)"> [
</span>'dragMode'<span style="color: rgba(0, 0, 0, 1)">,
</span>'aspectRatio'<span style="color: rgba(0, 0, 0, 1)">,
</span>'data'<span style="color: rgba(0, 0, 0, 1)">,
</span>'crop'<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> unchangeable props start from here</span>
'viewMode'<span style="color: rgba(0, 0, 0, 1)">,
</span>'preview'<span style="color: rgba(0, 0, 0, 1)">,
</span>'responsive'<span style="color: rgba(0, 0, 0, 1)">,
</span>'restore'<span style="color: rgba(0, 0, 0, 1)">,
</span>'checkCrossOrigin'<span style="color: rgba(0, 0, 0, 1)">,
</span>'checkOrientation'<span style="color: rgba(0, 0, 0, 1)">,
</span>'modal'<span style="color: rgba(0, 0, 0, 1)">,
</span>'guides'<span style="color: rgba(0, 0, 0, 1)">,
</span>'center'<span style="color: rgba(0, 0, 0, 1)">,
</span>'highlight'<span style="color: rgba(0, 0, 0, 1)">,
</span>'background'<span style="color: rgba(0, 0, 0, 1)">,
</span>'autoCrop'<span style="color: rgba(0, 0, 0, 1)">,
</span>'autoCropArea'<span style="color: rgba(0, 0, 0, 1)">,
</span>'movable'<span style="color: rgba(0, 0, 0, 1)">,
</span>'rotatable'<span style="color: rgba(0, 0, 0, 1)">,
</span>'scalable'<span style="color: rgba(0, 0, 0, 1)">,
</span>'zoomable'<span style="color: rgba(0, 0, 0, 1)">,
</span>'zoomOnTouch'<span style="color: rgba(0, 0, 0, 1)">,
</span>'zoomOnWheel'<span style="color: rgba(0, 0, 0, 1)">,
</span>'wheelZoomRatio'<span style="color: rgba(0, 0, 0, 1)">,
</span>'cropBoxMovable'<span style="color: rgba(0, 0, 0, 1)">,
</span>'cropBoxResizable'<span style="color: rgba(0, 0, 0, 1)">,
</span>'toggleDragModeOnDblclick'<span style="color: rgba(0, 0, 0, 1)">,
</span>'minContainerWidth'<span style="color: rgba(0, 0, 0, 1)">,
</span>'minContainerHeight'<span style="color: rgba(0, 0, 0, 1)">,
</span>'minCanvasWidth'<span style="color: rgba(0, 0, 0, 1)">,
</span>'minCanvasHeight'<span style="color: rgba(0, 0, 0, 1)">,
</span>'minCropBoxWidth'<span style="color: rgba(0, 0, 0, 1)">,
</span>'minCropBoxHeight'<span style="color: rgba(0, 0, 0, 1)">,
</span>'ready'<span style="color: rgba(0, 0, 0, 1)">,
</span>'cropstart'<span style="color: rgba(0, 0, 0, 1)">,
</span>'cropmove'<span style="color: rgba(0, 0, 0, 1)">,
</span>'cropend'<span style="color: rgba(0, 0, 0, 1)">,
</span>'zoom'<span style="color: rgba(0, 0, 0, 1)">,
];
const unchangeableProps </span>=<span style="color: rgba(0, 0, 0, 1)"> optionProps;
class ReactCropper extends Component {
componentDidMount() {
const options </span>= Object.keys(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props)
.filter(propKey </span>=> optionProps.indexOf(propKey) !== -1<span style="color: rgba(0, 0, 0, 1)">)
.reduce((prevOptions, propKey) </span>=><span style="color: rgba(0, 0, 0, 1)">
Object.assign({}, prevOptions, { : </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props }), {});
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper = <span style="color: rgba(0, 0, 255, 1)">new</span> Cropper(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.img, options);
}
UNSAFE_componentWillReceiveProps(nextProps) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.src !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.src) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.reset().clear().replace(nextProps.src);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.aspectRatio !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.aspectRatio) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setAspectRatio(nextProps.aspectRatio);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.data !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.data) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setData(nextProps.data);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.dragMode !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.dragMode) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setDragMode(nextProps.dragMode);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.cropBoxData !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.cropBoxData) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setCropBoxData(nextProps.cropBoxData);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.canvasData !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.canvasData) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setCanvasData(nextProps.canvasData);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.moveTo !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.moveTo) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.moveTo.length > 1<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.moveTo(nextProps.moveTo, nextProps.moveTo);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.moveTo(nextProps.moveTo);
}
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.zoomTo !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.zoomTo) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.zoomTo(nextProps.zoomTo);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.rotateTo !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.rotateTo) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.rotateTo(nextProps.rotateTo);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.scaleX !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.scaleX) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.scaleX(nextProps.scaleX);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.scaleY !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.scaleY) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.scaleY(nextProps.scaleY);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (nextProps.enable !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.enable) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (nextProps.enable) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.enable();
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.disable();
}
}
Object.keys(nextProps).forEach((propKey) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
let isDifferentVal </span>= nextProps !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props;
const isUnchangeableProps </span>= unchangeableProps.indexOf(propKey) !== -1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">typeof</span> nextProps === 'function' && <span style="color: rgba(0, 0, 255, 1)">typeof</span> <span style="color: rgba(0, 0, 255, 1)">this</span>.props === 'function'<span style="color: rgba(0, 0, 0, 1)">) {
isDifferentVal </span>= nextProps.toString() !== <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.toString();
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (isDifferentVal &&<span style="color: rgba(0, 0, 0, 1)"> isUnchangeableProps) {
</span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Error(`prop: ${propKey} can'<span style="color: rgba(0, 0, 0, 1)">t be change after componentDidMount`);
}
});
}
componentWillUnmount() {
if (this.img) {
// Destroy the cropper, this makes sure events such as resize are cleaned up and do not leak
this.cropper.destroy();
delete this.img;
delete this.cropper;
}
}
setDragMode(mode) {
return this.cropper.setDragMode(mode);
}
setAspectRatio(aspectRatio) {
return this.cropper.setAspectRatio(aspectRatio);
}
getCroppedCanvas(options) {
return this.cropper.getCroppedCanvas(options);
}
setCropBoxData(data) {
return this.cropper.setCropBoxData(data);
}
getCropBoxData() {
return this.cropper.getCropBoxData();
}
setCanvasData(data) {
return this.cropper.setCanvasData(data);
}
getCanvasData() {
return this.cropper.getCanvasData();
}
getImageData() {
return this.cropper.getImageData();
}
getContainerData() {
return this.cropper.getContainerData();
}
setData(data) {
return this.cropper.setData(data);
}
getData(rounded) {
return this.cropper.getData(rounded);
}
crop() {
return this.cropper.crop();
}
move(offsetX, offsetY) {
return this.cropper.move(offsetX, offsetY);
}
moveTo(x, y) {
return this.cropper.moveTo(x, y);
}
zoom(ratio) {
return this.cropper.zoom(ratio);
}
zoomTo(ratio) {
return this.cropper.zoomTo(ratio);
}
rotate(degree) {
return this.cropper.rotate(degree);
}
rotateTo(degree) {
return this.cropper.rotateTo(degree);
}
enable() {
return this.cropper.enable();
}
disable() {
return this.cropper.disable();
}
reset() {
return this.cropper.reset();
}
clear() {
return this.cropper.clear();
}
replace(url, onlyColorChanged) {
return this.cropper.replace(url, onlyColorChanged);
}
scale(scaleX, scaleY) {
return this.cropper.scale(scaleX, scaleY);
}
scaleX(scaleX) {
return this.cropper.scaleX(scaleX);
}
scaleY(scaleY) {
return this.cropper.scaleY(scaleY);
}
render() {
const {
src,
alt,
crossOrigin,
style,
className,
} = this.props;
return (
<div
style={style}
className={className}
>
<img
crossOrigin={crossOrigin}
ref={(img) => { this.img = img; }}
src={src}
alt={alt === undefined ? </span>'picture'<span style="color: rgba(0, 0, 0, 1)"> : alt}
style={{ opacity: 0 }}
/>
</div>
);
}
}
ReactCropper.propTypes = {
style: PropTypes.object, // eslint-disable-line react/forbid-prop-types
className: PropTypes.string,
// react cropper options
crossOrigin: PropTypes.string,
src: PropTypes.string,
alt: PropTypes.string,
// props of option can be changed after componentDidmount
aspectRatio: PropTypes.number,
dragMode: PropTypes.oneOf([</span>'crop', 'move', 'none'<span style="color: rgba(0, 0, 0, 1)">]),
data: PropTypes.shape({
x: PropTypes.number,
y: PropTypes.number,
width: PropTypes.number,
height: PropTypes.number,
rotate: PropTypes.number,
scaleX: PropTypes.number,
scaleY: PropTypes.number,
}),
scaleX: PropTypes.number,
scaleY: PropTypes.number,
enable: PropTypes.bool,
cropBoxData: PropTypes.shape({
left: PropTypes.number,
top: PropTypes.number,
width: PropTypes.number,
height: PropTypes.number,
}),
canvasData: PropTypes.shape({
left: PropTypes.number,
top: PropTypes.number,
width: PropTypes.number,
height: PropTypes.number,
}),
zoomTo: PropTypes.number,
moveTo: PropTypes.arrayOf(PropTypes.number),
rotateTo: PropTypes.number,
// cropperjs options
// https://github.com/fengyuanchen/cropperjs#options
// aspectRatio, dragMode, data
viewMode: PropTypes.oneOf(),
preview: PropTypes.string,
responsive: PropTypes.bool,
restore: PropTypes.bool,
checkCrossOrigin: PropTypes.bool,
checkOrientation: PropTypes.bool,
modal: PropTypes.bool,
guides: PropTypes.bool,
center: PropTypes.bool,
highlight: PropTypes.bool,
background: PropTypes.bool,
autoCrop: PropTypes.bool,
autoCropArea: PropTypes.number,
movable: PropTypes.bool,
rotatable: PropTypes.bool,
scalable: PropTypes.bool,
zoomable: PropTypes.bool,
zoomOnTouch: PropTypes.bool,
zoomOnWheel: PropTypes.bool,
wheelZoomRatio: PropTypes.number,
cropBoxMovable: PropTypes.bool,
cropBoxResizable: PropTypes.bool,
toggleDragModeOnDblclick: PropTypes.bool,
minContainerWidth: PropTypes.number,
minContainerHeight: PropTypes.number,
minCanvasWidth: PropTypes.number,
minCanvasHeight: PropTypes.number,
minCropBoxWidth: PropTypes.number,
minCropBoxHeight: PropTypes.number,
ready: PropTypes.func,
cropstart: PropTypes.func,
cropmove: PropTypes.func,
cropend: PropTypes.func,
crop: PropTypes.func,
zoom: PropTypes.func,
};
ReactCropper.defaultProps = {
src: null,
dragMode: </span>'crop'<span style="color: rgba(0, 0, 0, 1)">,
data: null,
scaleX: 1,
scaleY: 1,
enable: true,
zoomTo: 1,
rotateTo: 0,
};
export default ReactCropper;</span></pre>
</div>
<p> </p>
<p>2.cropper组件调用的简单封装</p>
<p>CropperView.jsx文件</p>
<div class="cnblogs_code">
<pre>import React, { Component, useEffect } from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import $ from </span>"jquery"<span style="color: rgba(0, 0, 0, 1)">;
import Cropper from </span>'./cropper/react-cropper'<span style="color: rgba(0, 0, 0, 1)">
import </span>'cropperjs/dist/cropper.css'
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> global FileReader </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> showCropArea = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> class CropperView extends Component {
constructor(props) {
super(props);
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
cropResult: </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,
};
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> this.cropper = this;</span>
<span style="color: rgba(0, 0, 255, 1)">this</span>.onChange = <span style="color: rgba(0, 0, 255, 1)">this</span>.onChange.bind(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.src =<span style="color: rgba(0, 0, 0, 1)"> props.src;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> componentDidMount(){</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> useEffect(() => {</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> alert("cropZone" + this.cropper);</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> // if (typeof this.cropper.getCroppedCanvas() === 'undefined') {</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> // return;</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> // }</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> alert("left:" + this.cropper.getCropBoxData().left</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> + "top:" + this.cropper.getCropBoxData().top</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> + "width:" + this.cropper.getCropBoxData().width</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> + "height:" + this.cropper.getCropBoxData().height);</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }, );</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }</span>
<span style="color: rgba(0, 0, 0, 1)">
onChange(e) {
e.preventDefault();
let files;
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (e.dataTransfer) {
files </span>=<span style="color: rgba(0, 0, 0, 1)"> e.dataTransfer.files;
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (e.target) {
files </span>=<span style="color: rgba(0, 0, 0, 1)"> e.target.files;
}
const reader </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FileReader();
reader.onload </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({ src: reader.result });
};
reader.readAsDataURL(files[</span>0<span style="color: rgba(0, 0, 0, 1)">]);
}
cropZone() {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">typeof</span> <span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCroppedCanvas() === 'undefined'<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
}
alert(</span>"left:" + <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().left
</span>+ "top:" + <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().top
</span>+ "width:" + <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().width
</span>+ "height:" + <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().height);
$(</span>".cropper-crop-box"<span style="color: rgba(0, 0, 0, 1)">).hide();
$(</span>".cropper-drag-box"<span style="color: rgba(0, 0, 0, 1)">).hide();
$(</span>".cropper-wrap-box"<span style="color: rgba(0, 0, 0, 1)">).append(
</span>"<div style=\"width:" + <span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().width + ";"
+ "height:" + <span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().height + ";"
+ "background:#0000FF; opacity:0.3"
+ "position:absolute; left:" + <span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().left +";"
+ "top:" + <span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().top + ";\">"<span style="color: rgba(0, 0, 0, 1)">);
}
showCropZone() {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (showCropArea) {
$(</span>".cropper-crop-box").css("display", "block"<span style="color: rgba(0, 0, 0, 1)">);
$(</span>".cropper-drag-box").css("display", "block"<span style="color: rgba(0, 0, 0, 1)">);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
$(</span>".cropper-crop-box"<span style="color: rgba(0, 0, 0, 1)">).hide();
$(</span>".cropper-drag-box"<span style="color: rgba(0, 0, 0, 1)">).hide();
}
}
creatCrop(){
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.crop();
}
clearCrop(){
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.clear();
}
resetCrop(){
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.reset();
}
moveLeft(){
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.move(-5, 0<span style="color: rgba(0, 0, 0, 1)">);
}
moveRight(){
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.move(5, 0<span style="color: rgba(0, 0, 0, 1)">);
}
moveUp(){
console.log(</span>"====moveUp==="<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.move(0, -5<span style="color: rgba(0, 0, 0, 1)">);
}</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (err){
console.log(err);
}
}
moveDown(){
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.move(0, 5<span style="color: rgba(0, 0, 0, 1)">);
}
enlarge(){
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 放大</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> this.cropper.zoom(0.1);</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> allCanvasDate = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCanvasData();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> newCanvasDate =<span style="color: rgba(0, 0, 0, 1)"> {left:allCanvasDate.left, top: allCanvasDate.top,
width: allCanvasDate.width</span>*2, height: allCanvasDate.height*2<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.setCanvasData(newCanvasDate);
}</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (err){
console.log(err);
}
}
shrink(){
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 缩小</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> this.cropper.zoom(-0.1)</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> allCanvasDate = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCanvasData();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> newCanvasDate =<span style="color: rgba(0, 0, 0, 1)"> {left:allCanvasDate.left, top: allCanvasDate.top,
width: allCanvasDate.width</span>*0.5, height: allCanvasDate.height*0.5<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.setCanvasData(newCanvasDate);
}</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (err){
console.log(err);
}
}
test(){
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> allDate = <span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getData(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
alert(allDate.toString());
</span><span style="color: rgba(0, 0, 255, 1)">var</span> par = {x:allDate.x, y:allDate.y, width:allDate.width*2, height:allDate.height*2<span style="color: rgba(0, 0, 0, 1)">,
rotate:allDate.rotate, scaleX:allDate.scaleX, scaleY:allDate.scaleY}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.setData(par);
}</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (err){
console.log(err);
}
}
moveCrop(){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">this.cropper.movecrop();</span>
console.log("===move===crop==="<span style="color: rgba(0, 0, 0, 1)">);
}
reduceCrop(){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> par = {left:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().left, top:<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().top,
width:</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().width*0.8, height:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().height*0.8<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.setCropBoxData(par);
}
raiseCrop(){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> par = {left:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().left, top:<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().top,
width:</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().width*1.2, height:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().height*1.2<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.setCropBoxData(par);
}
CropLeft(){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> par = {left:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().left - 10, top:<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().top,
width:</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().width, height:<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().height}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.setCropBoxData(par);
}
CropRight(){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> par = {left:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().left + 10, top:<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().top,
width:</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().width, height:<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().height}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.setCropBoxData(par);
}
CropUp(){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> par = {left:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().left, top:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().top - 10<span style="color: rgba(0, 0, 0, 1)">,
width:</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().width, height:<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().height}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.setCropBoxData(par);
}
CropDown(){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> par = {left:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().left, top:<span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().top + 10<span style="color: rgba(0, 0, 0, 1)">,
width:</span><span style="color: rgba(0, 0, 255, 1)">this</span>.cropper.getCropBoxData().width, height:<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.getCropBoxData().height}
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.cropper.setCropBoxData(par);
}
render() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><div className="r-view" style={{ position:'absolute', left:'185px', top:"83px" }}>
<<span style="color: rgba(0, 0, 0, 1)">Cropper
style</span>={{ height:'100%', width:'auto'<span style="color: rgba(0, 0, 0, 1)"> }}
aspectRatio</span>={16 / 9<span style="color: rgba(0, 0, 0, 1)">}
preview</span>=".img-preview"<span style="color: rgba(0, 0, 0, 1)">
guides</span>={<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">}
src</span>={<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.src}
viewMode</span>={2<span style="color: rgba(0, 0, 0, 1)">}
minContainerWidth</span>={585<span style="color: rgba(0, 0, 0, 1)">}
minContainerHeight</span>={430<span style="color: rgba(0, 0, 0, 1)">}
ref</span>={cropper => { <span style="color: rgba(0, 0, 255, 1)">this</span>.cropper =<span style="color: rgba(0, 0, 0, 1)"> cropper; }}
zoomable</span>={<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">}
zoomOnTouch</span>={<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">}
</span>/>
</div>
<span style="color: rgba(0, 0, 0, 1)"> );
}
}</span></pre>
</div>
<p>3.cropper组件与各种按钮操作的绑定(原因:设备上不会支持手指在选择区域的操作以及图片的放大缩小操作),页面整合组件。</p>
<p> CropperScreen.tsx文件</p>
<div class="cnblogs_code">
<pre>import * as React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import styled from </span>"styled-components"<span style="color: rgba(0, 0, 0, 1)">;
import useTranslate from </span>"../../hooks/useTranslate"<span style="color: rgba(0, 0, 0, 1)">;
import CropView from </span>"./CropperView"<span style="color: rgba(0, 0, 0, 1)">;
import </span>'cropperjs/dist/cropper.css'<span style="color: rgba(0, 0, 0, 1)">
import FormView from </span>"./FormView"<span style="color: rgba(0, 0, 0, 1)">
const AppStyle </span>=<span style="color: rgba(0, 0, 0, 1)"> styled.div`
background: #CCC;
`;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> showCropArea = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> CropperScreen() {
const t </span>=<span style="color: rgba(0, 0, 0, 1)"> useTranslate();
const handleBackClicked </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
$(</span>"#viewable"<span style="color: rgba(0, 0, 0, 1)">).show();
$(</span>"#scan_settings_id").css("display","none"<span style="color: rgba(0, 0, 0, 1)">);
};
const doCropClicked </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> showCropArea = !showCropArea;</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> if (showCropArea) {</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> alert("disabled false");</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $("#btn_save_crop").removeAttr("disabled");</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> } else {</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> alert("disabled true");</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $("#btn_save_crop").attr("disabled", "true");</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> cropUser.showCropZone(showCropArea);</span>
<span style="color: rgba(0, 0, 0, 1)">
cropUser.clearCrop();
};
const doSaveCropClicked </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.cropZone();
};
const handReset </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.resetCrop();
cropUser.creatCrop();
}
const handMoveLeft </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.moveLeft();
}
const handMoveRight </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.moveRight();
}
const handMoveUp </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(</span>"====handMoveUp==="<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.moveUp();
}</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (err){
console.log(err);
}
}
const handMoveDown </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.moveDown();
}
const handMoveCrop </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
alert(</span>"unknow"<span style="color: rgba(0, 0, 0, 1)">);
cropUser.moveCrop();
}
const handReduceCrop </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.reduceCrop();
}
const handRaiseCrop </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.raiseCrop()
}
const handEnlarge </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.enlarge();
}
const handShrink </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.shrink();
}
const handCropLeft </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.CropLeft()
}
const handCropRight </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.CropRight()
}
const handCropUp </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.CropUp()
}
const handCropDown </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.CropDown()
}
const handTest </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
cropUser.test()
}
const handleSubmit </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
alert(</span>"====handleSubmit===="<span style="color: rgba(0, 0, 0, 1)">);
console.log(formUser);
console.log(formUser.state);
}
let maxItem </span>= 2<span style="color: rgba(0, 0, 0, 1)">;
let cropUser
let formUser
</span><span style="color: rgba(0, 0, 255, 1)">var</span> initValue = {name:'tom',job: '12'<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><AppStyle id="scan_settings_id" className="r-view" style={{display:"none", height:"470px"}}>
<header className="r-titlebar">
<a href="#" className="r-titlebar__back" onClick={handleBackClicked} />
<h1 className="r-titlebar__title">Preview</h1>
</header>
<div style={{position:"absolute", left:35}}>
</div>
<CropView ref={cropView => {cropUser = cropView}} src={require('../smartsdk-css/img/test.png')}/>
<div className="r-floating-island">
<div>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={doCropClicked}>Crop</button>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={doSaveCropClicked}>Save</button>
</div>
<div>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={handReset}>reset</button>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={handMoveCrop}>mcrop</button>
</div>
<div>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={handMoveLeft}>left</button>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={handMoveRight}>right</button>
</div>
<div>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={handMoveUp}>up</button>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={handMoveDown}>down</button>
</div>
<div>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={ handEnlarge}>+</button>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={ handShrink}>-</button>
</div>
<div>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={ handReduceCrop}>reduce</button>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={ handRaiseCrop}>raise</button>
</div>
<div>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={ handCropLeft}>cropr</button>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={ handCropRight}>cropl</button>
</div>
<div>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={ handCropUp}>cropu</button>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={ handCropDown}>cropd</button>
</div>
<div>
<button style={{width:'70px', height:'35px', marginLeft:'5px'}} onClick={ handTest}>test</button>
</div>
<span style="color: rgba(0, 0, 0, 1)">
{</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> <FormView handleSubmit={handleSubmit} ref={formView => {formUser = formView}} props = {initValue}/> </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">}
{</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> <button className="r-start-button">
{t("dapi:cba.common.start")}
</button> </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">}
</span></div>
</AppStyle >
<span style="color: rgba(0, 0, 0, 1)"> );
}</span></pre>
</div>
<p>4.最后调用整体的组件,页面展示</p>
<p><img src="https://img2020.cnblogs.com/blog/1571965/202003/1571965-20200326175856400-400030729.png"></p>
<p>页面</p>
<p><img src="https://img2020.cnblogs.com/blog/1571965/202003/1571965-20200326175919627-1252982698.png"></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/wang-liang-blogs/p/12576023.html
頁:
[1]