所以,
目前,我找到了手动更新图像大小和位置等解决方案
1) 裁剪器的配置:
<Cropper
ref='cropper'
src={image}
style={{height: '100%', width: '100%'}}
viewMode={1}
background={false}
aspectRatio={1}
guides={false}
dragMode="none"
movable={false}
zoomable={false}
minCanvasWidth={320}
minCanvasHeight={320}
minCropBoxWidth={160}
minCropBoxHeight={160}
checkOrientation={true}
restore={true}
autoCropArea={0.5}
/>
2) 将监听器绑定到窗口大小调整事件并执行更新画布大小和居中图像的操作。我需要更新画布大小,因为在我的情况下图像应该小于窗口大小。
这里正在更新画布大小:
updateCanvasSize() {
let koef = 0.9, // how smaller than window viewport image should be
container = this.refs.cropper.getContainerData(),
allowedSize = Math.min(container.width, container.height) * koef,
image = this.refs.cropper.getImageData(),
imgW = Math.round(image.width),
imgH = Math.round(image.height),
originalW = Math.round(image.naturalWidth),
originalH = Math.round(image.naturalHeight),
newH, newW, scale = 1;
if (Math.abs((this.degrees / 90) % 2) > 0) { //if image was rotated
let t = imgW;
imgW = imgH;
imgH = t;
}
if (allowedSize < imgH || imgW > allowedSize) {
if (imgW > imgH) {
scale = allowedSize / imgW;
if (imgH * scale > allowedSize) {
scale = allowedSize / imgH;
}
}
else {
scale = allowedSize / imgH;
if (imgW * scale > allowedSize) {
scale = allowedSize / imgW;
}
}
}
newW = Math.round(scale * imgW);
newH = Math.round(scale * imgH);
/* this part is needed if you want to keep size of small images */
if (Math.abs((this.degrees / 90) % 2) > 0) {
if (newW >= originalH && newH >= originalW) {
newW = originalH;
newH = originalW;
}
} else {
if (newW >= originalW && newH >= originalH) {
newW = originalW;
newH = originalH;
}
}
/* end */
this.refs.cropper.setCanvasData({
width: newW,
height: newH
});
}
以及窗口内的居中:
centerImage() {
let container = this.refs.cropper.getContainerData(),
canvas = this.refs.cropper.getCanvasData(),
height = canvas.height,
width = canvas.width;
let top = Math.abs((container.height - height) / 2),
left = Math.abs((container.width - width) / 2);
this.refs.cropper.setCanvasData({
top,
left
});
}
另外,我在图像旋转后使用此更新。
希望对你有帮助