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.
This commit is contained in:
Samuel Mannehed 2019-04-02 16:45:51 +02:00
parent 2aa3b5bc79
commit a136b4b078
1 changed files with 20 additions and 12 deletions

View File

@ -191,10 +191,16 @@ export default class Display {
} }
absX(x) { absX(x) {
if (this._scale === 0) {
return 0;
}
return x / this._scale + this._viewportLoc.x; return x / this._scale + this._viewportLoc.x;
} }
absY(y) { absY(y) {
if (this._scale === 0) {
return 0;
}
return y / this._scale + this._viewportLoc.y; return y / this._scale + this._viewportLoc.y;
} }
@ -495,21 +501,23 @@ export default class Display {
} }
autoscale(containerWidth, containerHeight) { autoscale(containerWidth, containerHeight) {
let scaleRatio;
if (containerWidth === 0 || containerHeight === 0) { if (containerWidth === 0 || containerHeight === 0) {
Log.Warn("Autoscale doesn't work when width or height is zero"); scaleRatio = 0;
return;
} } else {
const vp = this._viewportLoc; const vp = this._viewportLoc;
const targetAspectRatio = containerWidth / containerHeight; const targetAspectRatio = containerWidth / containerHeight;
const fbAspectRatio = vp.w / vp.h; const fbAspectRatio = vp.w / vp.h;
let scaleRatio;
if (fbAspectRatio >= targetAspectRatio) { if (fbAspectRatio >= targetAspectRatio) {
scaleRatio = containerWidth / vp.w; scaleRatio = containerWidth / vp.w;
} else { } else {
scaleRatio = containerHeight / vp.h; scaleRatio = containerHeight / vp.h;
} }
}
this._rescale(scaleRatio); this._rescale(scaleRatio);
} }