phaser/src/geom/rectangle/FitInside.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2016-12-20 17:07:16 +00:00
var GetAspectRatio = require('./GetAspectRatio');
// Fits the target rectangle into the source rectangle.
// Preserves aspect ratio.
2016-12-20 17:07:16 +00:00
// Scales and centers the target rectangle to the source rectangle
2017-10-13 13:11:54 +00:00
/**
* [description]
*
* @function Phaser.Geom.Rectangle.FitInside
* @since 3.0.0
*
* @param {Phaser.Geom.Rectangle} target - [description]
* @param {Phaser.Geom.Rectangle} source - [description]
*
* @return {Phaser.Geom.Rectangle} [description]
*/
2016-12-20 17:07:16 +00:00
var FitInside = function (target, source)
{
var ratio = GetAspectRatio(target);
if (ratio < GetAspectRatio(source))
{
// Taller than Wide
target.setSize(source.height * ratio, source.height);
}
else
{
// Wider than Tall
2018-01-15 12:38:14 +00:00
target.setSize(source.width, source.width / ratio);
2016-12-20 17:07:16 +00:00
}
return target.setPosition(
2018-01-15 12:38:14 +00:00
source.centerX - (target.width / 2),
source.centerY - (target.height / 2)
2016-12-20 17:07:16 +00:00
);
};
module.exports = FitInside;