mirror of
https://github.com/photonstorm/phaser
synced 2024-12-26 04:53:38 +00:00
28 lines
709 B
JavaScript
28 lines
709 B
JavaScript
var GetAspectRatio = require('./GetAspectRatio');
|
|
|
|
// Fits the target rectangle into the source rectangle.
|
|
// Preserves aspect ration.
|
|
// Scales and centers the target rectangle to the source rectangle
|
|
|
|
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.right / 2) - target.width / 2,
|
|
(source.bottom / 2) - target.height / 2
|
|
);
|
|
};
|
|
|
|
module.exports = FitInside;
|