Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to fit images to a bounding box #29

Open
mack3457 opened this issue May 12, 2013 · 1 comment
Open

Added option to fit images to a bounding box #29

mack3457 opened this issue May 12, 2013 · 1 comment

Comments

@mack3457
Copy link

To fit differently sized images nicely into a given environment, I added an option named 'bounding_box' holding the size, the images should be scaled to. Use [0, 0] to keep default behaviour.

Added a function setBoundingBox(width, height) to change behaviour afterwards.

--- js/jquery.rs.slideshow.js           2013-05-10 15:12:20.000000000 +0200
+++ js/jquery.rs.slideshow.bb.js        2013-05-12 06:37:52.000000000 +0200
@@ -448,6 +448,8 @@
                                        }
                                        $img.css({left: leftOffset});
                                        $img.css({top: topOffset});
+                                       $img.css({width: width});
+                                       $img.css({height: height});
                                        if (slide.image_title){
                                                $img.attr('title', slide.image_title);
                                        }
@@ -616,6 +618,18 @@
                                });
                                RssPrivateMethods._bindActiveIndex($slideshow);
                        });
+               },
+
+
+               /**
+               *       Scale images to bounding box keeping ascpect ratio.
+               *       Width or height = 0 keeps the size unchanged
+               */
+
+               setBoundingBox: function(width, height) {
+                       var bounding_box = $(this).data('rsf_slideshow').settings.bounding_box;
+                       bounding_box[0] = width;
+                       bounding_box[1] = height;
                }


@@ -843,6 +857,13 @@
                        var width = $img.outerWidth();
                        var height = $img.outerHeight();
                        if (width && height) {
+                               var bounding_box = $slideshow.data('rsf_slideshow').settings.bounding_box;
+                               if (bounding_box[0] > 0 && bounding_box[1] > 0) {
+                                       var fac = Math.min(bounding_box[0]/width,
+                                                       bounding_box[1]/height);
+                                       width = Math.ceil(width * fac);
+                                       height = Math.ceil(height * fac);
+                               }
                                $img.detach();
                                sucesss(width, height);
                                return true;
@@ -974,6 +995,9 @@
                        loop: true,
                        //      Start slideshow automatically on initialisation
                        autostart: true,
+                       //      Bounding box to scale images to. Keeps aspect ratio of images.
+                       //      [0,0] keeps the original image size
+                       bounding_box: [0,0],
                        //      Slides to add to the slideshow
                        slides: [],
                        //      Class of the div containing the slide image and caption
@dwalton76
Copy link

Thank you for posting this. The only problem I found is where you do:

if (bounding_box[0] > 0 && bounding_box[1] > 0) {

This needs to be:
if (bounding_box && bounding_box[0] > 0 && bounding_box[1] > 0) {

else it crashes if "bounding_box" is not defined. Other than that it works great for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants