Skip to content

Instantly share code, notes, and snippets.

@ameyamashiro
Created August 12, 2014 10:55
Show Gist options
  • Save ameyamashiro/f521d9f573a29dd39f5e to your computer and use it in GitHub Desktop.
Save ameyamashiro/f521d9f573a29dd39f5e to your computer and use it in GitHub Desktop.
Size Computer
(function() {
var SizeComputer, _computeWidth;
_computeWidth = function(originWidth, baseWidth, refWidth) {
var rate;
rate = originWidth / baseWidth;
return refWidth * rate;
};
SizeComputer = function(varArgs) {
var defaults, opts, tgtCurWidth, verticalRate;
defaults = {
originWidth: 0,
originHeight: 0,
baseWidth: 800,
maxBaseWidth: 800,
refWidth: $(window).width()
};
opts = $.extend(defaults, {}, varArgs);
if (opts.refWidth >= opts.maxBaseWidth) {
opts.refWidth = opts.maxBaseWidth;
}
tgtCurWidth = _computeWidth(opts.originWidth, opts.baseWidth, opts.refWidth);
verticalRate = opts.originHeight / opts.originWidth;
return {
width: tgtCurWidth,
height: Math.round(tgtCurWidth * verticalRate)
};
};
window.SizeComputer = SizeComputer;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment