admin管理员组文章数量:1022982
I have created a function which resizes the size of the crop box. The user enters height and width, clicks "set" and job is done.
$('body').on('click', '#cropImageModal .set_box_size', function () {
var width = $(this).parents('.panel-body').find('.width').val();
var height = $(this).parents('.panel-body').find('.height').val();
document.getElementById('imageToCrop').cropper.setCropBoxData({width: parseInt(width), height: parseInt(height)});
});
Except we have a problem when, which is when resize is performed for some reason crop box is scaled down depending on the size of the image.
i.e. I have an image 700x385 and I set the box to be 700x300 and click "set"... result? the result is 510x219
I have narrowed down to the issue within cropperJS and it is to do with a code on lines 2662-2667:
if (self.ready && self.cropped) {
data = {
x: cropBoxData.left - canvasData.left,
y: cropBoxData.top - canvasData.top,
width: cropBoxData.width,
height: cropBoxData.height
};
// issue is below
ratio = imageData.width / imageData.naturalWidth;
each(data, function (n, i) {
n /= ratio;
data[i] = rounded ? Math.round(n) : n;
});
}
If I remove "ratio" code above the problem is solved but now getCroppedCanvas
doesn't work properly due to the added black background to the cropped image.
By the way, same can be seen when using /, if you are to:
- click "Get Crop Box Data"
- adjust the width/height in the box where result presented
- click "Set Crop Box Data"
- The Width / Height parameters to the right of the image do not match up
I have created a function which resizes the size of the crop box. The user enters height and width, clicks "set" and job is done.
$('body').on('click', '#cropImageModal .set_box_size', function () {
var width = $(this).parents('.panel-body').find('.width').val();
var height = $(this).parents('.panel-body').find('.height').val();
document.getElementById('imageToCrop').cropper.setCropBoxData({width: parseInt(width), height: parseInt(height)});
});
Except we have a problem when, which is when resize is performed for some reason crop box is scaled down depending on the size of the image.
i.e. I have an image 700x385 and I set the box to be 700x300 and click "set"... result? the result is 510x219
I have narrowed down to the issue within cropperJS and it is to do with a code on lines 2662-2667:
if (self.ready && self.cropped) {
data = {
x: cropBoxData.left - canvasData.left,
y: cropBoxData.top - canvasData.top,
width: cropBoxData.width,
height: cropBoxData.height
};
// issue is below
ratio = imageData.width / imageData.naturalWidth;
each(data, function (n, i) {
n /= ratio;
data[i] = rounded ? Math.round(n) : n;
});
}
If I remove "ratio" code above the problem is solved but now getCroppedCanvas
doesn't work properly due to the added black background to the cropped image.
By the way, same can be seen when using http://fengyuanchen.github.io/cropperjs/, if you are to:
- click "Get Crop Box Data"
- adjust the width/height in the box where result presented
- click "Set Crop Box Data"
- The Width / Height parameters to the right of the image do not match up
- I did find a solution, i will post it later. – Vlad Vladimir Hercules Commented May 24, 2018 at 8:05
- I have added an answer to this question. – Vlad Vladimir Hercules Commented May 24, 2018 at 9:33
- Your solution works, thanks! – croppio. Commented May 24, 2018 at 20:13
1 Answer
Reset to default 6The solution was to figure out the ratio and then perform the calculation of adjusted width and height using calculated ratio:
var width = parseInt($(this).parents('.panel-body').find('.width').val());
var height = parseInt($(this).parents('.panel-body').find('.height').val());
var ratio = document.getElementById('imageToCrop').cropper.imageData.width / document.getElementById('imageToCrop').cropper.imageData.naturalWidth;
document.getElementById('imageToCrop').cropper.setCropBoxData({width: width*ratio, height: height*ratio});
I have created a function which resizes the size of the crop box. The user enters height and width, clicks "set" and job is done.
$('body').on('click', '#cropImageModal .set_box_size', function () {
var width = $(this).parents('.panel-body').find('.width').val();
var height = $(this).parents('.panel-body').find('.height').val();
document.getElementById('imageToCrop').cropper.setCropBoxData({width: parseInt(width), height: parseInt(height)});
});
Except we have a problem when, which is when resize is performed for some reason crop box is scaled down depending on the size of the image.
i.e. I have an image 700x385 and I set the box to be 700x300 and click "set"... result? the result is 510x219
I have narrowed down to the issue within cropperJS and it is to do with a code on lines 2662-2667:
if (self.ready && self.cropped) {
data = {
x: cropBoxData.left - canvasData.left,
y: cropBoxData.top - canvasData.top,
width: cropBoxData.width,
height: cropBoxData.height
};
// issue is below
ratio = imageData.width / imageData.naturalWidth;
each(data, function (n, i) {
n /= ratio;
data[i] = rounded ? Math.round(n) : n;
});
}
If I remove "ratio" code above the problem is solved but now getCroppedCanvas
doesn't work properly due to the added black background to the cropped image.
By the way, same can be seen when using /, if you are to:
- click "Get Crop Box Data"
- adjust the width/height in the box where result presented
- click "Set Crop Box Data"
- The Width / Height parameters to the right of the image do not match up
I have created a function which resizes the size of the crop box. The user enters height and width, clicks "set" and job is done.
$('body').on('click', '#cropImageModal .set_box_size', function () {
var width = $(this).parents('.panel-body').find('.width').val();
var height = $(this).parents('.panel-body').find('.height').val();
document.getElementById('imageToCrop').cropper.setCropBoxData({width: parseInt(width), height: parseInt(height)});
});
Except we have a problem when, which is when resize is performed for some reason crop box is scaled down depending on the size of the image.
i.e. I have an image 700x385 and I set the box to be 700x300 and click "set"... result? the result is 510x219
I have narrowed down to the issue within cropperJS and it is to do with a code on lines 2662-2667:
if (self.ready && self.cropped) {
data = {
x: cropBoxData.left - canvasData.left,
y: cropBoxData.top - canvasData.top,
width: cropBoxData.width,
height: cropBoxData.height
};
// issue is below
ratio = imageData.width / imageData.naturalWidth;
each(data, function (n, i) {
n /= ratio;
data[i] = rounded ? Math.round(n) : n;
});
}
If I remove "ratio" code above the problem is solved but now getCroppedCanvas
doesn't work properly due to the added black background to the cropped image.
By the way, same can be seen when using http://fengyuanchen.github.io/cropperjs/, if you are to:
- click "Get Crop Box Data"
- adjust the width/height in the box where result presented
- click "Set Crop Box Data"
- The Width / Height parameters to the right of the image do not match up
- I did find a solution, i will post it later. – Vlad Vladimir Hercules Commented May 24, 2018 at 8:05
- I have added an answer to this question. – Vlad Vladimir Hercules Commented May 24, 2018 at 9:33
- Your solution works, thanks! – croppio. Commented May 24, 2018 at 20:13
1 Answer
Reset to default 6The solution was to figure out the ratio and then perform the calculation of adjusted width and height using calculated ratio:
var width = parseInt($(this).parents('.panel-body').find('.width').val());
var height = parseInt($(this).parents('.panel-body').find('.height').val());
var ratio = document.getElementById('imageToCrop').cropper.imageData.width / document.getElementById('imageToCrop').cropper.imageData.naturalWidth;
document.getElementById('imageToCrop').cropper.setCropBoxData({width: width*ratio, height: height*ratio});
本文标签: javascriptIssue with setCropBoxData in cropper jsStack Overflow
版权声明:本文标题:javascript - Issue with setCropBoxData in cropper js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745587741a2157699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论