From 1f4a4d8f35912dc05379dc38610d1cecd3cb7747 Mon Sep 17 00:00:00 2001 From: "usmanpakistan@gmail.com" Date: Fri, 26 Apr 2024 20:28:25 +0500 Subject: [PATCH] dist added --- dist/maxrects-packer.cjs | 13 +++++++++++-- dist/maxrects-packer.mjs | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/dist/maxrects-packer.cjs b/dist/maxrects-packer.cjs index dbb90a7..b63e728 100644 --- a/dist/maxrects-packer.cjs +++ b/dist/maxrects-packer.cjs @@ -501,11 +501,19 @@ return false; let tmpWidth = Math.max(this.width, node.x + node.width - this.padding + this.border); let tmpHeight = Math.max(this.height, node.y + node.height - this.padding + this.border); + let tmpFits = !(tmpWidth > this.maxWidth || tmpHeight > this.maxHeight); if (this.options.allowRotation) { // do extra test on rotated node whether it's a better choice const rotWidth = Math.max(this.width, node.x + node.height - this.padding + this.border); const rotHeight = Math.max(this.height, node.y + node.width - this.padding + this.border); - if (rotWidth * rotHeight < tmpWidth * tmpHeight) { + const rotFits = !(rotWidth > this.maxWidth || rotHeight > this.maxHeight); + // only compare when both rects will fit into bin + if (tmpFits && rotFits && rotWidth * rotHeight < tmpWidth * tmpHeight) { + tmpWidth = rotWidth; + tmpHeight = rotHeight; + } + // if rot fits and tmpFits not then do not compare area and set rot directly (some cases area of not rotated is smaller but will not fit) + if (rotFits && !tmpFits) { tmpWidth = rotWidth; tmpHeight = rotHeight; } @@ -517,7 +525,8 @@ if (this.options.square) { tmpWidth = tmpHeight = Math.max(tmpWidth, tmpHeight); } - if (tmpWidth > this.maxWidth + this.padding || tmpHeight > this.maxHeight + this.padding) { + tmpFits = !(tmpWidth > this.maxWidth || tmpHeight > this.maxHeight); + if (!tmpFits) { return false; } this.expandFreeRects(tmpWidth + this.padding, tmpHeight + this.padding); diff --git a/dist/maxrects-packer.mjs b/dist/maxrects-packer.mjs index 4efceec..9819210 100644 --- a/dist/maxrects-packer.mjs +++ b/dist/maxrects-packer.mjs @@ -495,11 +495,19 @@ class MaxRectsBin extends Bin { return false; let tmpWidth = Math.max(this.width, node.x + node.width - this.padding + this.border); let tmpHeight = Math.max(this.height, node.y + node.height - this.padding + this.border); + let tmpFits = !(tmpWidth > this.maxWidth || tmpHeight > this.maxHeight); if (this.options.allowRotation) { // do extra test on rotated node whether it's a better choice const rotWidth = Math.max(this.width, node.x + node.height - this.padding + this.border); const rotHeight = Math.max(this.height, node.y + node.width - this.padding + this.border); - if (rotWidth * rotHeight < tmpWidth * tmpHeight) { + const rotFits = !(rotWidth > this.maxWidth || rotHeight > this.maxHeight); + // only compare when both rects will fit into bin + if (tmpFits && rotFits && rotWidth * rotHeight < tmpWidth * tmpHeight) { + tmpWidth = rotWidth; + tmpHeight = rotHeight; + } + // if rot fits and tmpFits not then do not compare area and set rot directly (some cases area of not rotated is smaller but will not fit) + if (rotFits && !tmpFits) { tmpWidth = rotWidth; tmpHeight = rotHeight; } @@ -511,7 +519,8 @@ class MaxRectsBin extends Bin { if (this.options.square) { tmpWidth = tmpHeight = Math.max(tmpWidth, tmpHeight); } - if (tmpWidth > this.maxWidth + this.padding || tmpHeight > this.maxHeight + this.padding) { + tmpFits = !(tmpWidth > this.maxWidth || tmpHeight > this.maxHeight); + if (!tmpFits) { return false; } this.expandFreeRects(tmpWidth + this.padding, tmpHeight + this.padding);