fancybox icon indicating copy to clipboard operation
fancybox copied to clipboard

Rotate Images

Open tiagoboldt opened this issue 8 years ago • 40 comments

I would very much appreciate if fancyBox was able to rotate the image being overlayed. I can image it as being an option such as:

<script>
    $(el).fancybox({
      'enableRotation': true,
    })
</script>

The option would place two buttons, to rotate the image +90º and -90º. It could also be made available in the buttons helper.

Your thoughts?

tiagoboldt avatar Feb 22 '16 15:02 tiagoboldt

Maybe in the future :)

fancyapps avatar Apr 27 '16 08:04 fancyapps

Add this code to js file (fancybox 3) Remmeber set "instance.opts.rotate" is true in variable defaults

`// ========================================================================== // // Rotate image // Adds rotate functionality // // ========================================================================== ;(function (document, $) { 'use strict';

var RotateImage = function( instance ) {

	this.instance = instance;

	this.init();

};

$.extend( RotateImage.prototype, {

	$button		: null,
	angle		: 0,

	init : function() {
		var self = this;

		self.$button = $('<button data-rotate-left class="fancybox-button fancybox-button--rotate" title="Xoay ảnh sang trái"><i class="fa fa-undo" aria-hidden="true"></i></button>')
			.appendTo( this.instance.$refs.buttons )
			.on('touchend click', function(e) {
				self.rotate('left');
			});
			
		self.$button = $('<button data-rotate-right class="fancybox-button fancybox-button--rotate" title="Xoay ảnh sang phải"><i class="fa fa-repeat" aria-hidden="true"></i></button>')
			.appendTo( this.instance.$refs.buttons )
			.on('touchend click', function(e) {
				self.rotate('right');
			});
	},

	rotate : function (direct) {
		var self = this;

		var image = self.instance.current.$image[0]; 
		var sign = (direct == 'right') ? "" : "-";
		
		self.angle += 90;
		
		$(image).css('webkitTransform', 'rotate(' + sign + self.angle + 'deg)');
		$(image).css('mozTransform', 'rotate(' + sign + self.angle + 'deg)');
	},
});

$(document).on('onInit.fb', function(e, instance) {
	if ( !!instance.opts.rotate) {
		instance.Rotate = new RotateImage( instance );
	}

});

}(document, window.jQuery));`

hiidemo avatar Sep 11 '17 09:09 hiidemo

@hiidemo Didn't work. If I add your code to the Fancybox file and set "instance.opts.rotate: true," in defaults, the image doesn't open in Fancybox anymore.

Wndworks avatar Dec 20 '17 15:12 Wndworks

I think part of the problem may lie in the fact that the code was not posted correctly. Notice how }(document, window.jQuery)); is not even in the code window?

JamesAndersonJr avatar Dec 29 '17 00:12 JamesAndersonJr

Screenshot

image

hiidemo avatar Dec 29 '17 04:12 hiidemo

@JamesAndersonJr Yes, i did notice that and fixed it from the beginning. The code doesn't show any errors. I don't think that it's just a copy paste fail.

@hiidemo That's exactly what i'm trying to achieve. Can you help me out here? Is it possible to show the complete final script?

Update: When i'm adding instance.opts.rotate : true to the defaults, the console is showing the error SyntaxError: Unexpected token .. I guess i'm doing something wrong here.

Wndworks avatar Dec 30 '17 20:12 Wndworks

For fancybox 3 I did this....

/*adding custom item to fancybox menu to rotate image*/

$(document).on('onInit.fb', function (e, instance) {
  if ($('.fancybox-toolbar').find('#rotate_button').length === 0) {
    $('.fancybox-toolbar').prepend('<button id="rotate_button" class="fancybox-button" title="Rotate Image"><i class="fa fa-repeat"></i></button>');
  }
  var click = 1;
  $('.fancybox-toolbar').on('click', '#rotate_button', function () {
    var n = 90 * ++click;
    $('.fancybox-image-wrap img').css('webkitTransform', 'rotate(-' + n + 'deg)');
    $('.fancybox-image-wrap img').css('mozTransform', 'rotate(-' + n + 'deg)');
  });
});

paulhonnoll avatar Jan 25 '18 19:01 paulhonnoll

@paulhonnoll That's a nice solution! Thank you!

Wndworks avatar Jan 26 '18 22:01 Wndworks

@paulhonnoll Great Job! I have just 1 recommendation: change your preceding comment to 'block' style, for error-free copy-and-pasting. i.e.:

/* adding custom item to fancybox menu to rotate image */...

JamesAndersonJr avatar Jan 27 '18 22:01 JamesAndersonJr

Thanks @JamesAndersonJr , made the update!

paulhonnoll avatar Jan 28 '18 04:01 paulhonnoll

Really nice addition, would be useful :)

Eldenroot avatar Apr 03 '18 17:04 Eldenroot

So is the code above working? I am looking for something like this because some uploaded files are rotated by 90°, so it would be nice to add a button :)

Eldenroot avatar Sep 30 '18 16:09 Eldenroot

I tested it with Firefox, IE 10+ and Chrome and it was working. It does assume you are using fancybox 3 and font awesome for the fa-rotate.

paulhonnoll avatar Oct 01 '18 23:10 paulhonnoll

Wij don't rotate you your images in a photo editor before you upload the images to a web sertver?

acwolff avatar Oct 02 '18 08:10 acwolff

@Eldenroot i still use it, works perfect.

@acwolff Because in some cases you have to deal with website users that are uploading the images

Wndworks avatar Oct 02 '18 08:10 Wndworks

@Wndworks in that case, the up-loaders should rotate the images in a photo editor before they upload! I think it is not worthwhile to add code to FancyBox for this purpose!

acwolff avatar Oct 02 '18 09:10 acwolff

Unfortunately not all users do that :( so I would really welcome this featurr in FancyBox

@fancyapps

Eldenroot avatar Oct 02 '18 09:10 Eldenroot

@Wndworks - unfortunately not working for me, can you post your working code on codepen? thank you!

Eldenroot avatar Oct 13 '18 17:10 Eldenroot

Unbelivebel this feature is not more requested :o

seltix5 avatar Feb 09 '19 11:02 seltix5

hello all, i updated @hiidemo code and its working with the current fancybox version (3.5.6) :

var RotateImage = function (instance) {
    this.instance = instance;

    this.init();
};

$.extend(RotateImage.prototype, {
    $button_left: null,
    $button_right: null,
    transitionanimation: true,

    init: function () {
        var self = this;

        self.$button_left = $('<button data-rotate-left class="fancybox-button fancybox-button--rotate" title="Rotate to left"><i class="fas fa-undo" aria-hidden="true"></i></button>')
            .prependTo(this.instance.$refs.toolbar)
            .on('click', function (e) {
                self.rotate('left');
            });

        self.$button_right = $('<button data-rotate-right class="fancybox-button fancybox-button--rotate" title="Rotate to right"><i class="fas fa-redo" aria-hidden="true"></i></button>')
            .prependTo(this.instance.$refs.toolbar)
            .on('click', function (e) {
                self.rotate('right');
            });
    },

    rotate: function (direction) {
        var self = this;
        var image = self.instance.current.$image[0];
        var angle = parseInt(self.instance.current.$image.attr('data-angle')) || 0;

        if (direction == 'right') {
            angle += 90;
        } else {
            angle -= 90;
        }

        if (!self.transitionanimation) {
            angle = angle % 360;
        } else {
            $(image).css('transition', 'transform .3s ease-in-out');
        }

        self.instance.current.$image.attr('data-angle', angle);

        $(image).css('webkitTransform', 'rotate(' + angle + 'deg)');
        $(image).css('mozTransform', 'rotate(' + angle + 'deg)');
    }
});

$(document).on('onInit.fb', function (e, instance) {
    if (!!instance.opts.rotate) {
        instance.Rotate = new RotateImage(instance);
    }
});

The code can be added at the end of the "jquery.fancybox.js" file or just loaded after the file.

The icons are using last version of font awesome (v5).

Updates:

  • the current version of fancybox uses "this.instance.$refs.toolbar" instead of "this.instance.$refs.buttons"
  • changed the "appendTo" to "prependTo" to add the icons at left of the existent ones
  • updated the rotate counter since las one was not working correctly
  • added transition css
  • remove "touchend" event catcher because it was causing double rotation in mobiles

seltix5 avatar Feb 09 '19 20:02 seltix5

Thank you! Would be nice to get this into core :) maybe v3.6? :)

Eldenroot avatar Mar 12 '19 21:03 Eldenroot

@seltix5 - is possible to replace FA5 icons by svg?

Eldenroot avatar Mar 15 '19 15:03 Eldenroot

@Eldenroot Hello, yes, I supose. You probably will need the FA API for that since the svg is generated from JS and replace the <i> icons with the var with the generated icon :

var igloo = window.FontAwesome.icon({ prefix: 'fas', iconName: 'igloo' })

https://fontawesome.com/how-to-use/with-the-api/setup/getting-started

let me know if it works!

seltix5 avatar Mar 15 '19 16:03 seltix5

I mean use same way as FancyBox does for its icons, not FA

Eldenroot avatar Mar 15 '19 16:03 Eldenroot

Yes, you need to generate the svg code you want and put it there instead of the <i>

seltix5 avatar Mar 15 '19 16:03 seltix5

do you have any generated example which fits for this purpose?

Eldenroot avatar Mar 15 '19 16:03 Eldenroot

I do not since I use FA icons. I will need to seach online for a vector icon or create one and export it as SVG or use online converter with a vector image

seltix5 avatar Mar 15 '19 16:03 seltix5

that would be great!

Eldenroot avatar Mar 15 '19 16:03 Eldenroot

even more simple, you could just go to the FA icons page and copy the svg code from them =)

seltix5 avatar Mar 15 '19 17:03 seltix5

hmm, working example please? it is only for PRO not?

Eldenroot avatar Mar 15 '19 17:03 Eldenroot