Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropper.js how to get cropper instance from handled event

I'm trying to realize auto resizing or cropbox when box size less than minimum allowed image size, for now I made this:

var cropper = document.getElementById('image_cropper').cropper;

        if (!cropper instanceof Cropper) {
            console.warning('cropper not initialized');
            return false;
        }

        var data = cropper.getData();

        if (data.width < app.avatar.min_width || data.height < app.avatar.min_height) {
            data.width = app.avatar.min_width;
            data.height = app.avatar.min_height;

            cropper.setData(data);

            return;
        }

But want to do this calc on cropend event handled:

canvas.addEventListener('cropend', function (event) {
                var data = cropper.getData();

                if (data.width < app.avatar.min_width || data.height < app.avatar.min_height) {
                    data.width = app.avatar.min_width;
                    data.height = app.avatar.min_height;

                    cropper.setData(data);

                }
            });

And my question is: how can I get cropper instance from event?

like image 205
Oleg Shakhov Avatar asked Oct 28 '25 09:10

Oleg Shakhov


1 Answers

I found the solution: I can get cropper from canvas that is a target element of event, like this: event.target.cropper

like image 190
Oleg Shakhov Avatar answered Oct 31 '25 13:10

Oleg Shakhov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!