Last active
August 29, 2015 14:16
-
-
Save ashhitch/8dc9a4264a47f2b8e4d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Image Stretch module | |
*/ | |
var ImageStretcher = { | |
getDimensions: function(data) { | |
// calculate element coords to fit in mask | |
var ratio = data.imageRatio || (data.imageWidth / data.imageHeight), | |
slideWidth = data.maskWidth, | |
slideHeight = slideWidth / ratio; | |
if(slideHeight < data.maskHeight) { | |
slideHeight = data.maskHeight; | |
slideWidth = slideHeight * ratio; | |
} | |
return { | |
width: slideWidth, | |
height: slideHeight, | |
top: (data.maskHeight - slideHeight) / 2, | |
left: (data.maskWidth - slideWidth) / 2 | |
}; | |
}, | |
getRatio: function(image) { | |
if(image.prop('naturalWidth')) { | |
return image.prop('naturalWidth') / image.prop('naturalHeight'); | |
} else { | |
var img = new Image(); | |
img.src = image.prop('src'); | |
return img.width / img.height; | |
} | |
}, | |
imageLoaded: function(image, callback) { | |
var self = this; | |
var loadHandler = function() { | |
callback.call(self); | |
}; | |
if(image.prop('complete')) { | |
loadHandler(); | |
} else { | |
image.one('load', loadHandler); | |
} | |
}, | |
resizeHandler: function() { | |
var self = this; | |
jQuery.each(this.imgList, function(index, item) { | |
if(item.image.prop('complete')) { | |
self.resizeImage(item.image, item.container); | |
} | |
}); | |
}, | |
resizeImage: function(image, container) { | |
this.imageLoaded(image, function() { | |
var styles = this.getDimensions({ | |
imageRatio: this.getRatio(image), | |
maskWidth: container.width(), | |
maskHeight: container.height() | |
}); | |
image.css({ | |
width: styles.width, | |
height: styles.height, | |
marginTop: styles.top, | |
marginLeft: styles.left | |
}); | |
}); | |
}, | |
add: function(options) { | |
var container = jQuery(options.container ? options.container : window), | |
image = typeof options.image === 'string' ? container.find(options.image) : jQuery(options.image); | |
// resize image | |
this.resizeImage(image, container); | |
// add resize handler once if needed | |
if(!this.win) { | |
this.resizeHandler = jQuery.proxy(this.resizeHandler, this); | |
this.imgList = []; | |
this.win = jQuery(window); | |
this.win.on('resize orientationchange', this.resizeHandler); | |
} | |
// store item in collection | |
this.imgList.push({ | |
container: container, | |
image: image | |
}); | |
} | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function initBackgroundResize() { | |
jQuery('.visual-area').each(function() { | |
ImageStretcher.add({ | |
container: this, | |
image: 'img.bg-stretch' | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment