tapmodo / Jcrop

Jcrop - The Javascript Image Cropping Engine
https://jcrop.com
Other
4.31k stars 939 forks source link

setSelect reloads the wrong selection intermittently #21

Open thegrubbsian opened 12 years ago

thegrubbsian commented 12 years ago

While using the setSelect method of the Jcrop object I'm finding that sometimes it reloads the selection in a totally wrong area of the image. For example if I have 100x100 pixel image with a selection of { x: 10, y:10, x2: 30, y2: 30} when I reload I might get a much smaller selected area in a completely different part of the image. Has anyone run into this and/or have ideas on a fix? It's very intermittent and I can't seem to find a pattern for how to reproduce it. Here is the code we're using:

$(function() {

  jcrop_options = {
    aspectRatio: 1,
    onSelect: function(coords) {
      updateCropFormFields(coords);
      showCropFormButton(); 
    }
  };

  if ($(".image_cropper img").length > 0) {
    loadSelectionIfAvailable();
  }

  function loadSelectionIfAvailable() {
    var form = $("#finalize_crop_form");
    var x = parseInt($("#image_crop_offset_x", form).val());
    var y = parseInt($("#image_crop_offset_y", form).val());
    var height = parseInt($("#image_crop_height", form).val());
    var width = parseInt($("#image_crop_width", form).val());
    jcrop = $.Jcrop(".image_cropper img", jcrop_options);
    jcrop.setSelect([x, y, height, width]);
  }

});
shkleinik commented 12 years ago

Can I vote for the issue?

I recently faced with the same problem. I tried to look into it deeper, but found in the code following comments: // Magic %-) // This function could use some optimization I think... And other in the same style :) It is funny of course, but I wanted to use the plugin in my production environment ;)

Could somebody provide me with info if this issue is going to be fixed and if yes what are the timeline expectations?

hazzik commented 12 years ago

I think that it is not an issue. setSelect accepts array of 4 values: [x1, y1, x2, y2]. So it should be fixed like following: setSelect([x, y, x+width, y+height])

shkleinik commented 12 years ago

Thank for quick response ))

I'll try your solution

shkleinik commented 12 years ago

This worked for me, thanks!