jquery-fullsizable icon indicating copy to clipboard operation
jquery-fullsizable copied to clipboard

Image rotation

Open m3Lith opened this issue 10 years ago • 3 comments

It might not be needed for most people, but I'd love to have the optional function to be able to rotate the picture.

I'm going to try doing that myself at the moment.

m3Lith avatar Nov 03 '14 11:11 m3Lith

Hey, do you have an idea already on how to implement this? My first thought is towards a CSS rotation which shouldn't be to hard to implement.

MSchmidt avatar Nov 03 '14 13:11 MSchmidt

Yep, CSS transform option is the most lightweight IMO.

I'm not that good with JS/jQuery myself, so I haven't really reached anything working yet :/

m3Lith avatar Nov 03 '14 15:11 m3Lith

Anyway, here's my try on implementing this. Added rotateButton option:

    if (options.rotateButton) {
      $image_holder.append('<a id="fullsized_rotate"></a>');
      $(document).on('click', '#fullsized_rotate', function(e) {
        e.preventDefault();
        e.stopPropagation();
        return rotateImage();
      });
    }

The main function:

rotateImage = function() {
    var image, degg;
    image = images[current_image];
    degg = getRotationDegrees(image) + 90;
    return $(image).css('transform', 'rotate('+ degg +'deg)');
  };

And the angle calculation function:

  getRotationDegrees = function (obj) {
    var matrix = $(obj).css("-webkit-transform") ||
    $(obj).css("-moz-transform")    ||
    $(obj).css("-ms-transform")     ||
    $(obj).css("-o-transform")      ||
    $(obj).css("transform");
    if(matrix !== 'none') {
        var values = matrix.split('(')[1].split(')')[0].split(',');
        var a = values[0];
        var b = values[1];
        var angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
    } else { var angle = 0; }

    if(angle < 0) angle +=360;
    return angle;
  };

m3Lith avatar Nov 04 '14 09:11 m3Lith