From 05d4d87b2c35350709d6dfab2f55e41db6f18564 Mon Sep 17 00:00:00 2001 From: Rex Date: Thu, 8 Sep 2022 09:52:18 +0800 Subject: [PATCH] Get viewport under a camera --- src/scale/ScaleManager.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/scale/ScaleManager.js b/src/scale/ScaleManager.js index 33cfda323..6779f11a3 100644 --- a/src/scale/ScaleManager.js +++ b/src/scale/ScaleManager.js @@ -17,6 +17,7 @@ var Rectangle = require('../geom/rectangle/Rectangle'); var Size = require('../structs/Size'); var SnapFloor = require('../math/snap/SnapFloor'); var Vector2 = require('../math/Vector2'); +var Camera = require('../cameras/2d/Camera'); /** * @classdesc @@ -1483,17 +1484,24 @@ var ScaleManager = new Class({ }, /** - * Get Rectange of visible area, this Rectange does NOT factor in camera scroll. + * Get Rectange of visible area. * * @method Phaser.Scale.ScaleManager#getViewPort * @since 3.60.0 * + * @param {Phaser.Cameras.Scene2D.Camera} [camera] - The camera this viewport is respond upon. * @param {Phaser.Geom.Rectangle} [out] - The Rectangle of visible area. * * @return {Phaser.Geom.Rectangle} The Rectangle of visible area. */ - getViewPort: function (out) + getViewPort: function (camera, out) { + if (!(camera instanceof Camera)) + { + out = camera; + camera = undefined; + } + if (out === undefined) { out = new Rectangle(); @@ -1530,6 +1538,14 @@ var ScaleManager = new Class({ out.setTo(x, y, width, height); + if (camera) + { + out.width /= camera.zoomX; + out.height /= camera.zoomY; + out.centerX = camera.centerX + camera.scrollX; + out.centerY = camera.centerY + camera.scrollY; + } + return out; },