Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cropper zoom out with negative coordinates results in wrong canvas draw #91

Open
JOMI195 opened this issue Mar 5, 2025 · 0 comments
Open

Comments

@JOMI195
Copy link

JOMI195 commented Mar 5, 2025

Hello guys,

in addition to a fixed cropper with imageRestriction={ImageRestriction.none}, I implemented a custom zoom slider to also zoom out of the image to create a black padding around it. When I use the cropper.getCanvas() function to save this image, the image gets moved because of negative coordinates. I provided 2 images at the bottom.

I looked up your implementation of the advanced-cropper canvas and saw that it gets cutted out. Is there a function, method, workaround to get save image like in the cropper preview?

function updateCanvas(canvas, source, coordinates, resultSize, options) {
    canvas.width = resultSize ? resultSize.width : coordinates.width;
    canvas.height = resultSize ? resultSize.height : coordinates.height;
    var ctx = canvas.getContext('2d');
    if (ctx) {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        if (options) {
            if (options.imageSmoothingEnabled) {
                ctx.imageSmoothingEnabled = options.imageSmoothingEnabled;
            }
            if (options.imageSmoothingQuality) {
                ctx.imageSmoothingQuality = options.imageSmoothingQuality;
            }
            if (options.fillColor) {
                ctx.fillStyle = options.fillColor;
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                ctx.save();
            }
        }
        var offsetX = coordinates.left < 0 ? -coordinates.left : 0;
        var offsetY = coordinates.top < 0 ? -coordinates.top : 0;
        ctx.drawImage(source, coordinates.left + offsetX, coordinates.top + offsetY, coordinates.width, coordinates.height, offsetX * (canvas.width / coordinates.width), offsetY * (canvas.height / coordinates.height), canvas.width, canvas.height);
    }
    return canvas;
}

Code: Implementation of advanced cropper.

Image

Image1: Cropper preview with ability to zoom out of image bounds

Image

Image2: Resulting image on save

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant