Last active
December 24, 2015 10:29
Revisions
-
MartinSvarrer revised this gist
Oct 1, 2013 . 1 changed file with 29 additions and 30 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,34 +1,33 @@ function fit (targetWidth, targetHeight, containerWidth, containerHeight, options) { var settings = { cover: options && options.cover !== undefined ? options.cover : true, align: options && options.align !== undefined ? options.align : 'center middle' } var ratioWidth = containerWidth / targetWidth, ratioHeight = containerHeight / targetHeight, ratioTarget = targetWidth / targetHeight, ratioContainer = containerWidth / containerHeight; var scaleRatioHorizontally = settings.cover ? ratioHeight : ratioWidth, scaleRatioVertically = settings.cover ? ratioWidth : ratioHeight; var s = ratioTarget >= ratioContainer ? scaleRatioHorizontally : scaleRatioVertically, w = Math.round(targetWidth * s), h = Math.round(targetHeight * s); var left = 0.0, top = 0.0; if (settings.align.indexOf('center') !== -1) left = Math.round(0.5 * (containerWidth - w)); else if (settings.align.indexOf('right') !== -1) left = containerWidth - w; if (settings.align.indexOf('middle') !== -1) top = Math.round(0.5 * (containerHeight - h)); else if (settings.align.indexOf('bottom') !== -1) top = containerHeight - h; return { top: top, left: left, scale: s, width: w, height: h }; } -
MartinSvarrer created this gist
Oct 1, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ function fit (targetWidth, targetHeight, containerWidth, containerHeight, options) { var settings = { cover: options && options.cover !== undefined ? options.cover : true, align: options && options.align !== undefined ? options.align : 'center middle' } var ratioWidth = containerWidth / targetWidth, ratioHeight = containerHeight / targetHeight, ratioTarget = targetWidth / targetHeight, ratioContainer = containerWidth / containerHeight; var scaleRatioHorizontally = settings.cover ? ratioHeight : ratioWidth, scaleRatioVertically = settings.cover ? ratioWidth : ratioHeight; var s = ratioTarget >= ratioContainer ? scaleRatioHorizontally : scaleRatioVertically, w = Math.round(targetWidth * s), h = Math.round(targetHeight * s); var left = 0.0, top = 0.0; if (settings.align.indexOf('center') !== -1) left = Math.round(0.5 * (containerWidth - w)); else if (settings.align.indexOf('right') !== -1) left = containerWidth - w; if (settings.align.indexOf('middle') !== -1) top = Math.round(0.5 * (containerHeight - h)); else if (settings.align.indexOf('bottom') !== -1) top = containerHeight - h; return { top: top, left: left, scale: s, width: w, height: h }; }