退房小提琴:Link
這是你應該使用的代碼,因爲你的JSON已經格式化以同樣的方式克羅珀取其輸入:
//get the data from your rails framework and save it in a variable, below I just pasted the same data you put in your question
var data = {"x":552.697358490566,"y":-72.49509433962258,"width":696.9599999999999,"height":696.9599999999999,"rotate":0,"scaleX":1,"scaleY":1};
//remember to change my-picture to the id of your img
$('#my-picture').cropper('setData', data);
//also make sure to bind this to your own button
$('#crop-button').click(function(e){
//this will transform the image into a blob, so you can submit it in form data
$(this).href = $('#my-picture').cropper("getCroppedCanvas").toBlob(function (blob) {
var formData = new FormData();
formData.append('croppedImage', blob);
//this is where you put your Rails path to upload
//it's going to be a POST, so you should know how to handle it
$.ajax('/path/to/upload', {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function() {
console.log('Upload success');
},
error: function() {
console.log('Upload error');
}
});
});
});
能告訴你的JavaScript你用來產生這個輸出? – pedrotorres
使用cropper.js .. $ image.cropper({ 的aspectRatio:1, autoCropArea:1, 嚴格:真, 指南:真實, 亮點:真實, 背景:真實, dragCrop:假的, cropBoxMovable:假的, cropBoxResizable:真實, 預覽: 「#主文件上傳區」, mouseWheelZoom:真實, VIEWMODE:3, minContainerHeight:100, minContainerWidth:100, doubleClickToggle:真實, 旋轉:假, }, crop:function(data){x = data.x,width = data.width etc ...} ** – underScorePassionFruit
您能否展示更多代碼?我想知道如何從您的rails params數據到您的Javascript。你是否試圖在你的輸入json上完成顯示的作物? – pedrotorres