matiasgali / guillotine

jQuery plugin to crop images within an area (fully responsive), allowing to drag (touch support), zoom and rotate.
http://guillotine.js.org
327 stars 100 forks source link

Initialization issue in iOs and Mac OS Safari #33

Closed vladch closed 9 years ago

vladch commented 9 years ago

Hi, thanks for your great job with the plugin! I ran into a problem when the plugin canvas element would get 0 size, even is it is created within onload of the image. It was happening in my particular case all the time, it does happen in your live example when it is loaded fresh for the first time, consequent refreshes work fine. This is what I did to fix it:

Original code:

    Guillotine.prototype._wrap = function(element) {
      var canvas, el, guillotine, height, img, paddingTop, width, _ref, _ref1, _ref2;
      el = $(element);
      if (el.prop('tagName') === 'IMG') {
        img = document.createElement('img');
        img.src = el.attr('src');
       _ref = [img.width, img.height], width = _ref[0], height = _ref[1];
      } else {
        _ref1 = [el.width(), el.height()], width = _ref1[0], height = _ref1[1];
      }
     . . . . . . 

New code:

    Guillotine.prototype._wrap = function(element) {
      var canvas, el, guillotine, height, img, paddingTop, width, _ref, _ref1, _ref2;
      el = $(element);
      if (el.prop('tagName') === 'IMG') {
        // Replaced using locally created img element to determine dimensions with 
        // using naturalWidth/naturalHeight of the original image
        // Removed creating of the temporary element as this is not needed.
        _ref = [element.naturalWidth, element.naturalHeight], width = _ref[0], height = _ref[1];
      } else {
        _ref1 = [el.width(), el.height()], width = _ref1[0], height = _ref1[1];
      }
     . . . . . . 

naturalWidth/naturalHeight are supported by all the browsers, except for IE* and lower. I am not sure if it is an issue for your library, since those browsers have 1.3% market share as of February.

matiasgali commented 9 years ago

Hi @vladch, based on the code I'd say you're using an old version of the plugin.

Similar problems were reported on issue #15 and fixed on version 1.3 of Guillotine.

Check out the new code, it uses natural properties and degrades gracefully when not supported.

vladch commented 9 years ago

Thanks Matias, yes I was using the version from December. Will give the new version a shot.