| 643 | |
| 644 | // #region create UI |
| 645 | function resizeToFitWithMinSize(curW, curH, maxW, maxH, minSize) { |
| 646 | // Calculate the aspect ratio of the rectangle |
| 647 | const aspectRatio = curW / curH; |
| 648 | |
| 649 | // Calculate the new width and height to fit inside the window while maintaining aspect ratio |
| 650 | let newWidth = maxW; |
| 651 | let newHeight = maxW / aspectRatio; |
| 652 | |
| 653 | // If the new height exceeds the window height, resize based on height |
| 654 | if (newHeight > maxH) { |
| 655 | newHeight = maxH; |
| 656 | newWidth = maxH * aspectRatio; |
| 657 | } |
| 658 | if (newWidth > maxW) { |
| 659 | newWidth = maxW; |
| 660 | newHeight = maxW / aspectRatio; |
| 661 | } |
| 662 | |
| 663 | // Apply minimum size constraints |
| 664 | if (newWidth < minSize) { |
| 665 | newWidth = minSize; |
| 666 | newHeight = minSize / aspectRatio; |
| 667 | } |
| 668 | if (newHeight < minSize) { |
| 669 | newHeight = minSize; |
| 670 | newWidth = minSize * aspectRatio; |
| 671 | } |
| 672 | |
| 673 | // Return the new dimensions |
| 674 | return { |
| 675 | width: newWidth, |
| 676 | height: newHeight, |
| 677 | }; |
| 678 | } |
| 679 | |
| 680 | function chooseImg(srcs, _x, _y) { |
| 681 | let { x, y } = validateMouse(_x, _y); |