javascriptcropperjs

Cropperjs fixed cropping area height/width to crop image


I'm using cropper.js on MVC 5 Razor page where i do not want user to select cropping area with mouse, I want to initialize cropper object with predefined clipping (selected) area that user can't resize, but user can move clipping area on the picture to crop a part of picture.

I was not able to find any option to disable clipping, i checked on their Git page and it looks like it does not have ability, moreover i posted my request on Git page but did not get any response. i need help how can i workaround to achieve this.

        window.onload = function() {

            var Cropper = window.Cropper;
            var URL = window.URL || window.webkitURL;
            var container = document.querySelector('.img-container');
            var image = container.getElementsByTagName('img').item(0);
            var download = ''; 
            var actions = document.getElementById('actions');
            var dataX = 128; 
            var dataY = 72; 
            var dataHeight = 1024; 
            var dataWidth = 576; 
            var dataRotate = 0;  
            var dataScaleX = -1; 
            var dataScaleY = 1;  
            var options = {
                aspectRatio: 16 / 9,
                preview: '.img-preview',
                ready: function (e) {
                    console.log(e.type);
                },
                cropstart: function (e) {
                    console.log(e.type, e.detail.action);
                },
                cropmove: function (e) {
                    console.log(e.type, e.detail.action);
                },
                cropend: function (e) {
                    console.log(e.type, e.detail.action);
                },
                crop: function (e) {
                    var data = e.detail;

                    console.log(e.type);
                    dataX.value = Math.round(data.x);
                    dataY.value = Math.round(data.y);
                    dataHeight.value = Math.round(data.height);
                    dataWidth.value = Math.round(data.width);
                    dataRotate.value = typeof data.rotate !== 'undefined' ? data.rotate : '';
                    dataScaleX.value = typeof data.scaleX !== 'undefined' ? data.scaleX : '';
                    dataScaleY.value = typeof data.scaleY !== 'undefined' ? data.scaleY : '';
                },
                zoom: function (e) {
                    console.log(e.type, e.detail.ratio);
                }
            };

            var cropper = new Cropper(image, options);
        }
    .img-container {
        min-height: 497px;
        max-width: 497px;
        margin-bottom: 1rem;
        background-color: white;
        text-align: center;
        width: 100%;
    }

    .img-container > img {
        max-width: 100%;
    }

    img {
        vertical-align: middle;
        border-style: none;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.5/cropper.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.5/cropper.css" rel="stylesheet"/>

<div class="img-container">
    <img src="https://images.pexels.com/photos/266011/pexels-photo-266011.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260" id="croppr" />
</div>


Solution

  • With reference to help by @Chris W. I was able to setup with fixed clipping area.

            var image = document.getElementById('croppr');
            var cropper = new Cropper(image, {
                dragMode: 'move',
                aspectRatio: 16 / 9,
                autoCropArea: 0.65,
                restore: false,
                guides: false,
                center: false,
                highlight: false,
                cropBoxMovable: true,
                cropBoxResizable: false,
                toggleDragModeOnDblclick: false,
            });
        .img-container {
            min-height: 497px;
            max-width: 497px;
            margin-bottom: 1rem;
            background-color: white;
            text-align: center;
            width: 100%;
        }
    
        .img-container > img {
            max-width: 100%;
        }
    
        img {
            vertical-align: middle;
            border-style: none;
        }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.5/cropper.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.5/cropper.css" rel="stylesheet"/>
    
    <div class="img-container">
        <img src="https://images.pexels.com/photos/266011/pexels-photo-266011.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260" id="croppr" />
    </div>