Search code examples
javascriptjcrop

Jcrop is returning decimals for the object on callbacks


Using the jcrop library and I can not track down why the return object on callbacks can and sometimes contain decimals. I expect all x1, x2, y1, y2, width and height to be integers.

my initialization for jcrop is:

$('img.jsEditable').Jcrop({
    bgColor: 'black',
    bgOpacity: .4,
    setSelect: [0, 0, crop_width, crop_height],
    aspectRatio: 1,
    onSelect: saveCoordinates,
    minSize: [crop_width, crop_height],
    maxSize: [400, 400]
  });

where crop_width and crop_height is simply variables I set somewhere else, and happens to be 150. I am expecting (as in the demo's) for them to be a set pixel.

Any thoughts?
Any thoughts?


Solution

  • This appears to be a difference between Jcrop 0.9.9 and 0.9.10. If you go to the demo from the website, http://deepliquid.com/projects/Jcrop/demos.php?demo=handler and drag the box around you will only get integers in the callback. The demo is not up to date and is using Jcrop 0.9.9.

    However, if you download Jcrop you get version 0.9.10. The demo included in the download also uses Jcrop 0.9.10 and you get decimals in the callback. If you swap out version 0.9.10 in the demo and use 0.9.9 instead you only get integer coordinates in the callbacks.

    Looking into this further, it looks like in version 0.9.10 in jquery.Jcrop.js on line 656 the rounding of the coordinates was removed. The return went from:

    return [ Math.round(xa), Math.round(ya), Math.round(xb), Math.round(yb) ];
    

    to

    return [xa, ya, xb, yb];
    

    I'm not sure of the reasons for this (I looked through the commit comments on github but nothing jumped out at me) but if you add the rounding back in you won't get decimals any more on the callbacks.