diff --git a/README.md b/README.md index 3e2b34b..f99740f 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ You'll need to include jQuery (v1.9 or greater), the FocusPoint script, and Focu #### 3. Mark up your image container -Specify the image dimensions and focus point coordinates on the image container. The image will take up whatever space is available in the container, so make sure there is some space to fill by setting a height for the container in your CSS. Note: I know it shouldn't really be necessary to specify image dimensions but I've found this to be more reliable than reading the dimensions from the image. Example: +Specify the image dimensions and focus point coordinates on the image container. The image will take up whatever space is available in the container, so make sure there is some space to fill by setting a height for the container in your CSS. Example: ```html
``` +Note: setting `data-image-w` and `data-image-h` is optional but recommended. Ommiting these value means your image will not be positined correctly inside the frame until it has finished loading, which may cause a visible jump. + #### 4. Fire FocusPoint plugin Usually the best place for this will be inside your `$(document).ready()` function. @@ -109,6 +111,8 @@ If FocusPoint helped you impress a client and you want to say thanks, you're wel ## Changelog +#### v1.0.2 2014-09-05 +Made setting image width and height on shell optional #### v1.0.1 2014-09-04 Cleaned up variables #### v1.0.0 2014-08-19 diff --git a/bower.json b/bower.json index e173295..9b6af08 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "jquery-focuspoint", - "version": "1.0.1", + "version": "1.0.2", "homepage": "https://github.com/jonom/jquery-focuspoint", "authors": [ "Jonathon Menz" diff --git a/demos/css-js-comparison/index.html b/demos/css-js-comparison/index.html index c43388d..aa92d55 100644 --- a/demos/css-js-comparison/index.html +++ b/demos/css-js-comparison/index.html @@ -55,7 +55,7 @@

Original

Try resizing the window. Watch how the frames change to use all of the available space while preserving the focus of the image. The left frame uses FocusPoint to control the crop, while the right frame uses only CSS. - » Project Home

+ » Project Home

diff --git a/demos/full-screen/index.html b/demos/full-screen/index.html index 51b81f0..d3fba5c 100644 --- a/demos/full-screen/index.html +++ b/demos/full-screen/index.html @@ -40,7 +40,7 @@ - » Project Home + » Project Home diff --git a/demos/grid/bird.html b/demos/grid/bird.html index 2bcaab5..5ba6451 100644 --- a/demos/grid/bird.html +++ b/demos/grid/bird.html @@ -36,7 +36,9 @@
+ data-focus-y="-0.284" + data-image-w="667" + data-image-h="1000">
@@ -111,7 +113,7 @@

Original

-

Try resizing the window. Watch how the frames change to use all of the available space while preserving the focus of the image. » Project Home

+

Try resizing the window. Watch how the frames change to use all of the available space while preserving the focus of the image. » Project Home

diff --git a/demos/grid/dolphin.html b/demos/grid/dolphin.html index 3e87e6c..ab153bd 100644 --- a/demos/grid/dolphin.html +++ b/demos/grid/dolphin.html @@ -119,7 +119,7 @@

Original

-

Try resizing the window. Watch how the frames change to use all of the available space while preserving the focus of the image. » Project Home

+

Try resizing the window. Watch how the frames change to use all of the available space while preserving the focus of the image. » Project Home

diff --git a/demos/grid/kangaroo.html b/demos/grid/kangaroo.html index 4245b4e..dbb1d23 100644 --- a/demos/grid/kangaroo.html +++ b/demos/grid/kangaroo.html @@ -119,7 +119,7 @@

Original

-

Try resizing the window. Watch how the frames change to use all of the available space while preserving the focus of the image. » Project Home

+

Try resizing the window. Watch how the frames change to use all of the available space while preserving the focus of the image. » Project Home

diff --git a/demos/grid/lizard.html b/demos/grid/lizard.html index 453d5ca..a7fe57f 100644 --- a/demos/grid/lizard.html +++ b/demos/grid/lizard.html @@ -113,7 +113,7 @@

Original

-

Try resizing the window. Watch how the frames change to use all of the available space while preserving the focus of the image. » Project Home

+

Try resizing the window. Watch how the frames change to use all of the available space while preserving the focus of the image. » Project Home

diff --git a/focuspoint.jquery.json b/focuspoint.jquery.json index 1162d60..3d0eec7 100644 --- a/focuspoint.jquery.json +++ b/focuspoint.jquery.json @@ -13,7 +13,7 @@ "focus", "focuspoint" ], - "version": "1.0.1", + "version": "1.0.2", "author": { "name": "Jonathon Menz", "url": "http://jonathonmenz.com" @@ -21,7 +21,7 @@ "licenses": [ { "type": "MIT", - "url": "https://github.com/jonom/jquery-focuspoint/blob/1.0.1/MIT-LICENSE.txt" + "url": "https://github.com/jonom/jquery-focuspoint/blob/1.0.2/MIT-LICENSE.txt" } ], "bugs": "https://github.com/jonom/jquery-focuspoint/issues", diff --git a/js/jquery.focuspoint.js b/js/jquery.focuspoint.js index 072e6bc..51e3063 100755 --- a/js/jquery.focuspoint.js +++ b/js/jquery.focuspoint.js @@ -1,14 +1,15 @@ -/*! - * jQuery FocusPoint; version: 1.0.1 +/** + * jQuery FocusPoint; version: 1.0.2 * Author: http://jonathonmenz.com * Source: https://github.com/jonom/jquery-focuspoint * Copyright (c) 2014 J. Menz; MIT License + * @preserve */ ; (function($) { $.fn.focusPoint = function(options) { var settings = $.extend({ - // These are the defaults. + //These are the defaults. reCalcOnWindowResize: true }, options); return this.each(function() { @@ -34,6 +35,8 @@ image, imageW, imageH, + self, + imageTmp, wR, hR, hShift, @@ -52,31 +55,24 @@ focusOffsetY, yRemainder, containerYRemainder; - //Adjust the focus of frame + //Collect dimensions containerW = $(this).width(); containerH = $(this).height(); image = $(this).find('img').first(); imageW = $(this).data('imageW'); imageH = $(this).data('imageH'); - + //Get image dimensions if not set on container if (!imageW || !imageH) { - var self = this, - imageTmp = new Image(); - + self = this; + imageTmp = new Image(); imageTmp.onload = function(){ - $(self).data('imageW', this.width); $(self).data('imageH', this.height); - - $(self).adjustFocus(); + $(self).adjustFocus(); //adjust once image is loaded - may cause a visible jump }; - imageTmp.src = image.attr('src'); - - return false; + return false; //Don't proceed right now, will try again once image has loaded } - - if (!(containerW > 0 && containerH > 0 && imageW > 0 && imageH > 0)) { //Need dimensions to proceed return false; @@ -108,15 +104,17 @@ //Can't use width() as images may be display:none scaledImageWidth = Math.floor(imageW / hR); focusX = Math.floor(focusFactorX * scaledImageWidth); - //console.log('x'+focusX); //Calculate difference beetween focus point and center focusOffsetX = focusX - containerCenterX; //Reduce offset if necessary so image remains filled xRemainder = scaledImageWidth - focusX; containerXRemainder = containerW - containerCenterX; - if (xRemainder < containerXRemainder) focusOffsetX -= containerXRemainder - xRemainder; - if (focusOffsetX < 0) focusOffsetX = 0; - //console.log('x'+focusOffsetX); + if (xRemainder < containerXRemainder){ + focusOffsetX -= containerXRemainder - xRemainder; + } + if (focusOffsetX < 0) { + focusOffsetX = 0; + } //Shift to left hShift = focusOffsetX * -1; } else if (wR < hR) { @@ -132,8 +130,12 @@ //Reduce offset if necessary so image remains filled yRemainder = scaledImageHeight - focusY; containerYRemainder = containerH - containerCenterY; - if (yRemainder < containerYRemainder) focusOffsetY -= containerYRemainder - yRemainder; - if (focusOffsetY < 0) focusOffsetY = 0; + if (yRemainder < containerYRemainder) { + focusOffsetY -= containerYRemainder - yRemainder; + } + if (focusOffsetY < 0) { + focusOffsetY = 0; + } //Shift to top vShift = focusOffsetY * -1; } diff --git a/js/jquery.focuspoint.min.js b/js/jquery.focuspoint.min.js index 4011c89..465614e 100644 --- a/js/jquery.focuspoint.min.js +++ b/js/jquery.focuspoint.min.js @@ -1,7 +1,8 @@ -/*! - * jQuery FocusPoint; version: 1.0.1 +/** + * jQuery FocusPoint; version: 1.0.2 * Author: http://jonathonmenz.com * Source: https://github.com/jonom/jquery-focuspoint * Copyright (c) 2014 J. Menz; MIT License + * @preserve */ -;(function(a){a.fn.focusPoint=function(b){var c=a.extend({reCalcOnWindowResize:true},b);return this.each(function(){var d=a(this);d.removeClass("focus-left-top focus-left-center focus-left-bottom focus-center-top focus-center-center focus-center-bottom focus-right-top focus-right-center focus-right-bottom");d.adjustFocus();if(c.reCalcOnWindowResize){a(window).resize(function(){d.adjustFocus()})}})};a.fn.adjustFocus=function(){return this.each(function(){var f,n,p,e,l,v,r,u,o,k,d,i,t,x,g,c,j,b,h,s,w,q,m;f=a(this).width();n=a(this).height();p=a(this).find("img").first();e=a(this).data("imageW");l=a(this).data("imageH");if(!(f>0&&n>0&&e>0&&l>0)){return false}v=e/f;r=l/n;if(e>f&&l>n){if(v>r){p.css("max-width","");p.css("max-height","100%")}else{p.css("max-width","100%");p.css("max-height","")}}else{p.css("max-width","");p.css("max-height","")}u=0;o=0;if(v>r){k=Math.floor(f/2);d=(Number(a(this).data("focus-x"))+1)/2;i=Math.floor(e/r);t=Math.floor(d*i);x=t-k;g=i-t;c=f-k;if(g0&&s>0&&i>0&&e>0?(h=i/t,n=e/s,i>t&&e>s?h>n?(o.css("max-width",""),o.css("max-height","100%")):(o.css("max-width","100%"),o.css("max-height","")):(o.css("max-width",""),o.css("max-height","")),r=0,f=0,h>n?(u=Math.floor(t/2),d=(Number($(this).data("focus-x"))+1)/2,m=Math.floor(i/n),g=Math.floor(d*m),l=g-u,w=m-g,x=t-u,x>w&&(l-=x-w),0>l&&(l=0),r=-1*l):n>h&&(p=Math.floor(s/2),M=(Number($(this).data("focus-y"))+1)/2,b=Math.floor(e/h),j=b-Math.floor(M*b),v=j-p,F=b-j,W=s-p,W>F&&(v-=W-F),0>v&&(v=0),f=-1*v),o.css("left",r+"px"),void o.css("top",f+"px")):!1:(a=this,c=new Image,c.onload=function(){$(a).data("imageW",this.width),$(a).data("imageH",this.height),$(a).adjustFocus()},c.src=o.attr("src"),!1)})}}(jQuery); \ No newline at end of file