【问题标题】:CropperJS incorrect scaling canvas after browser resize浏览器调整大小后 CropperJS 不正确缩放画布
【发布时间】:2016-12-18 09:32:36
【问题描述】:


我正在使用 ReactCropper 女巫使用 CropperJs。 我正在使用裁剪和旋​​转功能。 我遇到了响应功能的问题。当将浏览器大小调整为更小,然后再更大时,图像变得太大(就像放大了很多一样)。 这是上传的图像,如您所见,它没有居中,但主要问题不在于。

在这里调整浏览器的大小

在再次放大浏览器窗口后,图像被移动和缩放。

我正在使用这些选项来配置 Cropper:

            <Cropper
                    ref='cropper'
                    src={image}
                    style={{height: '100%', width: '100%'}}
                    background={false}
                    viewMode={1}
                    zoomOnTouch={false}
                    zoomOnWheel={false}
                    aspectRatio={1}
                    guides={false}
                    restore={true}
                />

当我删除 viewMode={1} 时,没有缩放或移动问题,但裁剪框正在所有容器上移动(我不需要这种裁剪框行为,我需要它在图像内部移动)。

任何想法是什么问题?

感谢您的帮助

【问题讨论】:

  • 你有没有对此有所了解?我遇到的问题是我的浏览器在移动时会调整它的大小。不知道为什么。
  • @Schw2iizer 是的,我会在下面写答案

标签: javascript image reactjs crop


【解决方案1】:

所以,

目前,我找到了手动更新图像大小和位置等解决方案

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
        });
    }

另外,我在图像旋转后使用此更新。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2019-02-09
    • 2012-01-30
    相关资源
    最近更新 更多