Skip to content

Commit 5684522

Browse files
authored
Merge pull request #40 from VirusShell/fix-gcd-infinite-loop
Fix infinite loop in gcd() with non-numeric inputs
2 parents d3ed10b + b1d610d commit 5684522

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

js/ResolutionMaster.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,23 @@ class ResolutionMasterCanvas {
645645
ctx.stroke();
646646
}
647647

648+
static gcd(a, b) {
649+
a = Math.abs(Math.floor(Number(a))) || 1;
650+
b = Math.abs(Math.floor(Number(b))) || 1;
651+
652+
while (b !== 0) {
653+
const t = a % b;
654+
a = b;
655+
b = t;
656+
}
657+
return a;
658+
}
659+
660+
static aspectRatioString(w, h) {
661+
const g = ResolutionMasterCanvas.gcd(w, h);
662+
return `${w / g}:${h / g}`;
663+
}
664+
648665
drawInfoText(ctx, y) {
649666
const node = this.node;
650667
if (this.widthWidget && this.heightWidget) {
@@ -653,21 +670,7 @@ class ResolutionMasterCanvas {
653670
const mp = ((width * height) / 1000000).toFixed(2);
654671
const pResolution = this.getClosestPResolution(width, height);
655672

656-
function gcd(a, b) {
657-
while (b !== 0) {
658-
const t = a % b;
659-
a = b;
660-
b = t;
661-
}
662-
return a;
663-
}
664-
665-
function aspectRatioString(w, h) {
666-
const g = gcd(w, h);
667-
return `${w / g}:${h / g}`;
668-
}
669-
670-
const aspectRatio = aspectRatioString(width, height);
673+
const aspectRatio = ResolutionMasterCanvas.aspectRatioString(width, height);
671674

672675
ctx.fillStyle = "#bbb";
673676
ctx.font = "12px Arial";

0 commit comments

Comments
 (0)