Skip to content

Instantly share code, notes, and snippets.

@MartinSvarrer
Last active December 24, 2015 10:29

Revisions

  1. MartinSvarrer revised this gist Oct 1, 2013. 1 changed file with 29 additions and 30 deletions.
    59 changes: 29 additions & 30 deletions fit to object calculation
    Original 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 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;
    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('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 };
    }
    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 };
    }
  2. MartinSvarrer created this gist Oct 1, 2013.
    34 changes: 34 additions & 0 deletions fit to object calculation
    Original 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 };
    }