phaser/src/geom/rectangle/FitInside.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 Photon Storm Ltd.
2018-02-12 16:01:20 +00:00
* @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');
2017-10-13 13:11:54 +00:00
/**
2018-12-20 12:12:20 +00:00
* Adjusts the target rectangle, changing its width, height and position,
* so that it fits inside the area of the source rectangle, while maintaining its original
* aspect ratio.
*
* Unlike the `FitOutside` function, there may be some space inside the source area not covered.
2017-10-13 13:11:54 +00:00
*
* @function Phaser.Geom.Rectangle.FitInside
* @since 3.0.0
*
2018-03-27 13:27:08 +00:00
* @generic {Phaser.Geom.Rectangle} O - [target,$return]
*
2018-12-20 12:12:20 +00:00
* @param {Phaser.Geom.Rectangle} target - The target rectangle to adjust.
* @param {Phaser.Geom.Rectangle} source - The source rectangle to envlope the target in.
2017-10-13 13:11:54 +00:00
*
2018-12-20 12:12:20 +00:00
* @return {Phaser.Geom.Rectangle} The modified target rectangle instance.
2017-10-13 13:11:54 +00:00
*/
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;