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

Block the user from zooming the image #1232

Open
floede opened this issue Mar 7, 2025 · 6 comments
Open

Block the user from zooming the image #1232

floede opened this issue Mar 7, 2025 · 6 comments

Comments

@floede
Copy link

floede commented Mar 7, 2025

I'd like a way to set something like zoomable=false on the image.
Scalable=false does that, BUT it also makes the image show up on the canvas in it's natural size.

I'd like the image to be scaled for the canvas, but disallow the user to scale / zoom afterwards.

@fengyuanchen
Copy link
Owner

Listen on the action event, call event.preventDefault() when the event.detail.action is "scale".

@floede
Copy link
Author

floede commented Mar 8, 2025

Typescript will not allow me to use "event.detail.action".

I've tried this instead:

canvas?.addEventListener('action', (event) => {
  const customEvent = event as CustomEvent;
  if (customEvent.detail.action === 'scale') {
    console.log('Scale event');
  }
});

But preventDefault() does not stop the zoom.

@fengyuanchen
Copy link
Owner

Try to listen on the transform event of the CropperImage, call event.preventDefault() when the image scaling.

@floede
Copy link
Author

floede commented Mar 9, 2025

I can do that, but then the Image can't scale at all, and just displays at it's natural size.
I'd like image to fit the canvas, like the default behavior.

I've tried this:

cropperCanvas?.addEventListener('action', (event) => {
const customEvent = event as CustomEvent;
if (customEvent.detail.action === 'scale') {
isUserZoom = true;
}
});

cropperImage?.addEventListener('transform', (event) => {
if (isUserZoom) {
event.preventDefault();
isUserZoom = false;
}
});

But the user can still zoom the image a step or 2, before the event is cancelled.

@volaricn
Copy link

I have the exact same problem. Tried the same things with no luck:

  • setting "scalable" to false displays the image in natural size, it doesn't fit
  • the "action" event has the event.detail.action is "scale" and it gets triggered, but event.preventDefault does not prevent the scale
  • on the "transform" event, I can't know when it's initiated by scale or something else. So if I do preventDefault, the image doesn't fit the canvas at the beginning.

What I did find is a weird hacky fix that uses the oldMatrix data to allow it to fit the canvas initially, and then prevent it afterwards:

cropperImage?.addEventListener('transform', (event) => {
    // oldMatrix has scaleX and scaleY set to 1 until it's first set up, so using it to prevent scale/rotate/skew after that
    if (e.detail.oldMatrix[0] !== 1 && e.detail.oldMatrix[3] !== 1) {
      e.preventDefault();
    }
});

but it doesn't look as a great solution. I hope someone finds a better one

@mandalornl
Copy link

@floede and @volaricn Setting the scaleStep property to 0 on the canvas element seems to work.

const canvas = cropper.getCropperCanvas();
canvas.scaleStep = 0;

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

No branches or pull requests

4 participants