/
FitInside.js
47 lines (42 loc) · 1.35 KB
/
FitInside.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var GetAspectRatio = require('./GetAspectRatio');
/**
* 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.
*
* @function Phaser.Geom.Rectangle.FitInside
* @since 3.0.0
*
* @generic {Phaser.Geom.Rectangle} O - [target,$return]
*
* @param {Phaser.Geom.Rectangle} target - The target rectangle to adjust.
* @param {Phaser.Geom.Rectangle} source - The source rectangle to envelop the target in.
*
* @return {Phaser.Geom.Rectangle} The modified target rectangle instance.
*/
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
target.setSize(source.width, source.width / ratio);
}
return target.setPosition(
source.centerX - (target.width / 2),
source.centerY - (target.height / 2)
);
};
module.exports = FitInside;