mardi 3 mars 2015

Accessing function results from another function?


I have these functions



function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();

reader.onload = function (e) {

var img = new Image;
img.src = reader.result;

var imgwidth = img.width;
var imgheight = img.height;
console.log(imgwidth);
console.log(imgheight);

$('#imgcrop').attr('src', e.target.result);
$('#preview').attr('src', e.target.result);
$('.crop').Jcrop({

onSelect: updateCoords,
boxWidth: 300,
boxHeight: 300,
onChange: showPreview,
aspectRatio: 1,
bgOpacity: .4,
setSelect: [ 100, 100, 50, 50 ]
});
};

reader.readAsDataURL(input.files[0]);
}
}

$("#imgInp").change(function() {
console.log(this);
readURL(this);
});

function updateCoords(c) {
console.log(c);
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
}

function showPreview(coords) {
var rx = 100 / coords.w;
var ry = 100 / coords.h;

$('#preview').css({
width: Math.round(rx * imgwidth) + 'px',
height: Math.round(ry * imgheight) + 'px',
marginLeft: '-' + Math.round(rx * coords.x) + 'px',
marginTop: '-' + Math.round(ry * coords.y) + 'px'
});
}


And I need to access imgwidth and imgheight from the file reader function to use in the showpreview function, but I'm having trouble with the scope of those functions. How can I access these values, or at least pass them as parameters to use in the other function?


Thanks!





Aucun commentaire:

Enregistrer un commentaire