2016-12-20 17:07:16 +00:00
|
|
|
var GetAspectRatio = require('./GetAspectRatio');
|
|
|
|
|
|
|
|
// Fits the target rectangle into the source rectangle.
|
2016-12-22 01:32:21 +00:00
|
|
|
// Preserves aspect ratio.
|
2016-12-20 17:07:16 +00:00
|
|
|
// 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(
|
2016-12-22 01:32:21 +00:00
|
|
|
(source.right / 2) - (target.width / 2),
|
|
|
|
(source.bottom / 2) - (target.height / 2)
|
2016-12-20 17:07:16 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = FitInside;
|