Skip to content

Commit 9b1ac70

Browse files
committed
Added ability for camera to zoomX and zoomY
1 parent e80ed94 commit 9b1ac70

1 file changed

Lines changed: 116 additions & 21 deletions

File tree

src/cameras/2d/BaseCamera.js

Lines changed: 116 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ var BaseCamera = new Class({
291291
this._scrollY = 0;
292292

293293
/**
294-
* The Camera zoom value. Change this value to zoom in, or out of, a Scene.
294+
* The Camera horizontal zoom value. Change this value to zoom in, or out of, a Scene.
295295
*
296296
* A value of 0.5 would zoom the Camera out, so you can now see twice as much
297297
* of the Scene as before. A value of 2 would zoom the Camera in, so every pixel
@@ -301,13 +301,32 @@ var BaseCamera = new Class({
301301
*
302302
* Be careful to never set this value to zero.
303303
*
304-
* @name Phaser.Cameras.Scene2D.BaseCamera#_zoom
304+
* @name Phaser.Cameras.Scene2D.BaseCamera#_zoomX
305305
* @type {number}
306306
* @private
307307
* @default 1
308-
* @since 3.11.0
308+
* @since 3.50.0
309+
*/
310+
this._zoomX = 1;
311+
312+
/**
313+
* The Camera vertical zoom value. Change this value to zoom in, or out of, a Scene.
314+
*
315+
* A value of 0.5 would zoom the Camera out, so you can now see twice as much
316+
* of the Scene as before. A value of 2 would zoom the Camera in, so every pixel
317+
* now takes up 2 pixels when rendered.
318+
*
319+
* Set to 1 to return to the default zoom level.
320+
*
321+
* Be careful to never set this value to zero.
322+
*
323+
* @name Phaser.Cameras.Scene2D.BaseCamera#_zoomY
324+
* @type {number}
325+
* @private
326+
* @default 1
327+
* @since 3.50.0
309328
*/
310-
this._zoom = 1;
329+
this._zoomY = 1;
311330

312331
/**
313332
* The rotation of the Camera in radians.
@@ -793,13 +812,14 @@ var BaseCamera = new Class({
793812
var c = Math.cos(this.rotation);
794813
var s = Math.sin(this.rotation);
795814

796-
var zoom = this.zoom;
815+
var zoomX = this.zoomX;
816+
var zoomY = this.zoomY;
797817

798818
var scrollX = this.scrollX;
799819
var scrollY = this.scrollY;
800820

801-
var sx = x + ((scrollX * c - scrollY * s) * zoom);
802-
var sy = y + ((scrollX * s + scrollY * c) * zoom);
821+
var sx = x + ((scrollX * c - scrollY * s) * zoomX);
822+
var sy = y + ((scrollX * s + scrollY * c) * zoomY);
803823

804824
// Apply transform to point
805825
output.x = (sx * ima + sy * imc) + ime;
@@ -864,7 +884,8 @@ var BaseCamera = new Class({
864884
var halfWidth = width * 0.5;
865885
var halfHeight = height * 0.5;
866886

867-
var zoom = this.zoom;
887+
var zoomX = this.zoomX;
888+
var zoomY = this.zoomY;
868889
var matrix = this.matrix;
869890

870891
var originX = width * this.originX;
@@ -896,8 +917,8 @@ var BaseCamera = new Class({
896917
// Basically the pixel value of what it's looking at in the middle of the cam
897918
this.midPoint.set(midX, midY);
898919

899-
var displayWidth = width / zoom;
900-
var displayHeight = height / zoom;
920+
var displayWidth = width / zoomX;
921+
var displayHeight = height / zoomY;
901922

902923
this.worldView.setTo(
903924
midX - (displayWidth / 2),
@@ -906,7 +927,7 @@ var BaseCamera = new Class({
906927
displayHeight
907928
);
908929

909-
matrix.applyITRS(this.x + originX, this.y + originY, this.rotation, zoom, zoom);
930+
matrix.applyITRS(this.x + originX, this.y + originY, this.rotation, zoomX, zoomY);
910931
matrix.translate(-originX, -originY);
911932
},
912933

@@ -1326,23 +1347,33 @@ var BaseCamera = new Class({
13261347
*
13271348
* Changing the zoom does not impact the Camera viewport in any way, it is only applied during rendering.
13281349
*
1350+
* As of Phaser 3.50 you can now set the horizontal and vertical zoom values independently.
1351+
*
13291352
* @method Phaser.Cameras.Scene2D.BaseCamera#setZoom
13301353
* @since 3.0.0
13311354
*
1332-
* @param {number} [value=1] - The zoom value of the Camera. The minimum it can be is 0.001.
1355+
* @param {number} [x=1] - The horizontal zoom value of the Camera. The minimum it can be is 0.001.
1356+
* @param {number} [y=x] - The vertical zoom value of the Camera. The minimum it can be is 0.001.
13331357
*
13341358
* @return {this} This Camera instance.
13351359
*/
1336-
setZoom: function (value)
1360+
setZoom: function (x, y)
13371361
{
1338-
if (value === undefined) { value = 1; }
1362+
if (x === undefined) { x = 1; }
1363+
if (y === undefined) { y = x; }
13391364

1340-
if (value === 0)
1365+
if (x === 0)
13411366
{
1342-
value = 0.001;
1367+
x = 0.001;
13431368
}
13441369

1345-
this.zoom = value;
1370+
if (y === 0)
1371+
{
1372+
y = 0.001;
1373+
}
1374+
1375+
this.zoomX = x;
1376+
this.zoomY = y;
13461377

13471378
return this;
13481379
},
@@ -1717,12 +1748,76 @@ var BaseCamera = new Class({
17171748

17181749
get: function ()
17191750
{
1720-
return this._zoom;
1751+
return (this._zoomX + this._zoomY) / 2;
1752+
},
1753+
1754+
set: function (value)
1755+
{
1756+
this._zoomX = value;
1757+
this._zoomY = value;
1758+
1759+
this.dirty = true;
1760+
}
1761+
1762+
},
1763+
1764+
/**
1765+
* The Camera horizontal zoom value. Change this value to zoom in, or out of, a Scene.
1766+
*
1767+
* A value of 0.5 would zoom the Camera out, so you can now see twice as much
1768+
* of the Scene as before. A value of 2 would zoom the Camera in, so every pixel
1769+
* now takes up 2 pixels when rendered.
1770+
*
1771+
* Set to 1 to return to the default zoom level.
1772+
*
1773+
* Be careful to never set this value to zero.
1774+
*
1775+
* @name Phaser.Cameras.Scene2D.BaseCamera#zoomX
1776+
* @type {number}
1777+
* @default 1
1778+
* @since 3.50.0
1779+
*/
1780+
zoomX: {
1781+
1782+
get: function ()
1783+
{
1784+
return this._zoomX;
1785+
},
1786+
1787+
set: function (value)
1788+
{
1789+
this._zoomX = value;
1790+
this.dirty = true;
1791+
}
1792+
1793+
},
1794+
1795+
/**
1796+
* The Camera vertical zoom value. Change this value to zoom in, or out of, a Scene.
1797+
*
1798+
* A value of 0.5 would zoom the Camera out, so you can now see twice as much
1799+
* of the Scene as before. A value of 2 would zoom the Camera in, so every pixel
1800+
* now takes up 2 pixels when rendered.
1801+
*
1802+
* Set to 1 to return to the default zoom level.
1803+
*
1804+
* Be careful to never set this value to zero.
1805+
*
1806+
* @name Phaser.Cameras.Scene2D.BaseCamera#zoomY
1807+
* @type {number}
1808+
* @default 1
1809+
* @since 3.50.0
1810+
*/
1811+
zoomY: {
1812+
1813+
get: function ()
1814+
{
1815+
return this._zoomY;
17211816
},
17221817

17231818
set: function (value)
17241819
{
1725-
this._zoom = value;
1820+
this._zoomY = value;
17261821
this.dirty = true;
17271822
}
17281823

@@ -1810,7 +1905,7 @@ var BaseCamera = new Class({
18101905

18111906
get: function ()
18121907
{
1813-
return this.width / this.zoom;
1908+
return this.width / this.zoomX;
18141909
}
18151910

18161911
},
@@ -1833,7 +1928,7 @@ var BaseCamera = new Class({
18331928

18341929
get: function ()
18351930
{
1836-
return this.height / this.zoom;
1931+
return this.height / this.zoomY;
18371932
}
18381933

18391934
}

0 commit comments

Comments
 (0)