jQuery imgAreaSelect使用宽高比设置初始选择

con*_*nor 1 javascript jquery

我正在使用jQuery和imgAreaSelect插件.我正在使用区域选择插件,以便用户可以在上传之前将图像裁剪为16:9的宽高比.

我想显示初始裁剪选择,以便在选择文件时加载缩略图,并使用imgAreaSelect选择最大可能的16:9选择.我有缩略图加载等但只是无法获得宽高比部分.这是我到目前为止:

    // adds an image area select instance
    function addImgAreaSelect( img ){
            img.addClass( 'imgAreaSelect' ).imgAreaSelect({
                    handles : true,
                    aspectRatio : '16:9',
                    fadeSpeed : 1,
                    show : true
            });
            img.load(function(){ // set initial crop at 16:9 aspect ratio, calculate coordinates
                    // @todo
                    $( this ).imgAreaSelect({ x1 : 0, y1 : 0, x2 : this.width, y2 : this.height });

   });
}
Run Code Online (Sandbox Code Playgroud)

对此有任何帮助表示赞赏!谢谢

con*_*nor 9

这对我有用:

// adds an image area select instance
function addImgAreaSelect( img ){
        img.addClass( 'imgAreaSelect' ).imgAreaSelect({
                handles : true,
                aspectRatio : '16:9',
                fadeSpeed : 1,
                show : true
        });
        img.load(function(){ // display initial image selection 16:9
                    var height = ( this.width / 16 ) * 9;
                    if( height <= this.height ){     
                            var diff = ( this.height - height ) / 2;
                            var coords = { x1 : 0, y1 : diff, x2 : this.width, y2 : height + diff };
                    }   
                    else{ // if new height out of bounds, scale width instead
                            var width = ( this.height / 9 ) * 16; 
                            var diff = ( this.width - width ) / 2;
                            var coords = { x1 : diff, y1 : 0, x2 : width + diff, y2: this.height };
                    }   
                $( this ).imgAreaSelect( coords );
        });
}
Run Code Online (Sandbox Code Playgroud)