diff options
author | Samuel Mannehed <samuel@cendio.se> | 2019-04-02 16:45:51 +0200 |
---|---|---|
committer | Samuel Mannehed <samuel@cendio.se> | 2019-04-02 16:51:18 +0200 |
commit | a136b4b078e8ac316b80d1ee24cf8f9b400ba2d5 (patch) | |
tree | 88e8f74ceb3d5864d8d9e69abdaf28439640b5b3 | |
parent | 2aa3b5bc79a6ae33be93985469be27a6b787176f (diff) | |
download | novnc-a136b4b078e8ac316b80d1ee24cf8f9b400ba2d5.tar.gz |
Allow autoscale() with zero height or width
Commit 6e7e6f9 stopped the function from running if width or height was
zero, this commit reverts that change. This commit also makes the
resulting canvas 0x0 if autoscale is called with zero. By adding this
special case we can avoid division by zero in the calculations.
-rw-r--r-- | core/display.js | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/core/display.js b/core/display.js index 25a9595..1528384 100644 --- a/core/display.js +++ b/core/display.js @@ -191,10 +191,16 @@ export default class Display { } absX(x) { + if (this._scale === 0) { + return 0; + } return x / this._scale + this._viewportLoc.x; } absY(y) { + if (this._scale === 0) { + return 0; + } return y / this._scale + this._viewportLoc.y; } @@ -495,20 +501,22 @@ export default class Display { } autoscale(containerWidth, containerHeight) { - if (containerWidth === 0 || containerHeight === 0) { - Log.Warn("Autoscale doesn't work when width or height is zero"); - return; - } + let scaleRatio; - const vp = this._viewportLoc; - const targetAspectRatio = containerWidth / containerHeight; - const fbAspectRatio = vp.w / vp.h; + if (containerWidth === 0 || containerHeight === 0) { + scaleRatio = 0; - let scaleRatio; - if (fbAspectRatio >= targetAspectRatio) { - scaleRatio = containerWidth / vp.w; } else { - scaleRatio = containerHeight / vp.h; + + const vp = this._viewportLoc; + const targetAspectRatio = containerWidth / containerHeight; + const fbAspectRatio = vp.w / vp.h; + + if (fbAspectRatio >= targetAspectRatio) { + scaleRatio = containerWidth / vp.w; + } else { + scaleRatio = containerHeight / vp.h; + } } this._rescale(scaleRatio); |