How the compression works
Your image is drawn onto an in-memory canvas. The tool then runs a binary search on JPEG quality (10%–100%) using canvas.toBlob to find the highest quality that stays at or under your target KB. If even the lowest quality is still too large, the image is progressively scaled down by 10% steps and retried until the target is met.