Skip to content

Commit 8c4c836

Browse files
committed
Keep map scale by adopting field of view on view port resize
1 parent 163aa21 commit 8c4c836

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed

src/BasicWorldWindowController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ define([
457457
// Clamp tilt to between 0 and +90 to prevent the viewer from going upside down.
458458
lookAt.tilt = WWMath.clamp(lookAt.tilt, 0, 90);
459459

460-
// Normalize heading to between -180 and +180.
460+
// Normalize roll to between -180 and +180.
461461
lookAt.roll = Angle.normalizedDegrees(lookAt.roll);
462462

463463
// Apply 2D limits when the globe is 2D.

src/WorldWindow.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ define([
175175
* @type {Rectangle}
176176
* @readonly
177177
*/
178-
this.viewport = new Rectangle(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
178+
this.viewport = new Rectangle(0, 0, 0, 0);
179179

180180
/**
181181
* The globe displayed.
@@ -811,13 +811,25 @@ define([
811811
width = gl.canvas.clientWidth * this.pixelScale,
812812
height = gl.canvas.clientHeight * this.pixelScale;
813813

814-
if (gl.canvas.width != width ||
815-
gl.canvas.height != height) {
814+
if (gl.canvas.width != width || gl.canvas.height != height
815+
|| this.viewport.width === 0 || this.viewport.height === 0) {
816816

817817
// Make the canvas drawing buffer size match its screen size.
818818
gl.canvas.width = width;
819819
gl.canvas.height = height;
820820

821+
// Keep map scale by adopting field of view on view port resize
822+
if (this.viewport.height !== 0) {
823+
try {
824+
this.camera.fieldOfView *= height / this.viewport.height;
825+
} catch (ignore) {
826+
// Keep original field of view in case new one does not fit requirements
827+
}
828+
} else if (width > height) {
829+
// Apply initial field of view to the longest viewport side
830+
this.camera.fieldOfView *= height / width;
831+
}
832+
821833
// Set the WebGL viewport to match the canvas drawing buffer size.
822834
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
823835
this.viewport = new Rectangle(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);

src/geom/Camera.js

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,99 @@ define([
3333
*/
3434
this.position = new Position(30, -110, 10e6);
3535

36+
// Intentionally not documented
37+
this._heading = 0;
38+
39+
// Intentionally not documented
40+
this._tilt = 0;
41+
42+
// Intentionally not documented
43+
this._roll = 0;
44+
45+
// Intentionally not documented
46+
this._fieldOfView = 45;
47+
};
48+
49+
Object.defineProperties(Camera.prototype, {
3650
/**
3751
* Camera heading, in degrees clockwise from north.
3852
* @type {Number}
3953
* @default 0
54+
* @throws {ArgumentError} If the specified heading is out of range.
4055
*/
41-
this.heading = 0;
56+
heading: {
57+
get: function () {
58+
return this._heading;
59+
},
60+
set: function (value) {
61+
if (value < -180 || value > 180) {
62+
throw new ArgumentError(
63+
Logger.logMessage(Logger.LEVEL_SEVERE, "Camera", "setHeading", "Invalid heading")
64+
);
65+
}
66+
this._heading = value;
67+
}
68+
},
4269

4370
/**
4471
* Camera tilt, in degrees.
4572
* @default 0
73+
* @throws {ArgumentError} If the specified tilt is out of range.
4674
*/
47-
this.tilt = 0;
75+
tilt: {
76+
get: function () {
77+
return this._tilt;
78+
},
79+
set: function (value) {
80+
if (value < -90 || value > 90) {
81+
throw new ArgumentError(
82+
Logger.logMessage(Logger.LEVEL_SEVERE, "Camera", "setTilt", "Invalid tilt")
83+
);
84+
}
85+
this._tilt = value;
86+
}
87+
},
4888

4989
/**
5090
* Camera roll, in degrees.
5191
* @type {Number}
5292
* @default 0
93+
* @throws {ArgumentError} If the specified roll is out of range.
5394
*/
54-
this.roll = 0;
95+
roll: {
96+
get: function () {
97+
return this._roll;
98+
},
99+
set: function (value) {
100+
if (value < -180 || value > 180) {
101+
throw new ArgumentError(
102+
Logger.logMessage(Logger.LEVEL_SEVERE, "Camera", "setRoll", "Invalid roll")
103+
);
104+
}
105+
this._roll = value;
106+
}
107+
},
55108

56109
/**
57110
* Camera vertical field of view, in degrees
58111
* @type {Number}
59112
* @default 45
113+
* @throws {ArgumentError} If the specified field of view is out of range.
60114
*/
61-
this.fieldOfView = 45;
62-
};
115+
fieldOfView: {
116+
get: function () {
117+
return this._fieldOfView;
118+
},
119+
set: function (value) {
120+
if (value < 0 || value > 180) {
121+
throw new ArgumentError(
122+
Logger.logMessage(Logger.LEVEL_SEVERE, "Camera", "setFieldOfView", "Invalid field of view")
123+
);
124+
}
125+
this._fieldOfView = value;
126+
}
127+
}
128+
});
63129

64130
/**
65131
* Indicates whether the components of this object are equal to those of a specified object.

0 commit comments

Comments
 (0)