sales-frontend-utils 0.0.19 → 0.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -473,6 +473,37 @@ var convertTextToImg = ({
473
473
  ctx.fillText(targetStr, canvas.width / 2, canvas.height / 2);
474
474
  return canvas.toDataURL();
475
475
  };
476
+ function drawImageResizeCentered(base64, size) {
477
+ return new Promise((resolve) => {
478
+ const { canvasHeight, canvasWidth } = size;
479
+ const canvas = document.createElement("canvas");
480
+ canvas.width = canvasWidth;
481
+ canvas.height = canvasHeight;
482
+ const ctx = canvas.getContext("2d");
483
+ const img = new Image();
484
+ img.src = base64;
485
+ img.onload = () => {
486
+ const imageWidth = img.width;
487
+ const imageHeight = img.height;
488
+ const canvasRatio = canvasWidth / canvasHeight;
489
+ const imageRatio = imageWidth / imageHeight;
490
+ let newWidth;
491
+ let newHeight;
492
+ if (imageRatio > canvasRatio) {
493
+ newWidth = canvasWidth;
494
+ newHeight = newWidth / imageRatio;
495
+ } else {
496
+ newHeight = canvasHeight;
497
+ newWidth = newHeight * imageRatio;
498
+ }
499
+ const x = (canvasWidth - newWidth) / 2;
500
+ const y = (canvasHeight - newHeight) / 2;
501
+ ctx?.clearRect(0, 0, canvasWidth, canvasHeight);
502
+ ctx?.drawImage(img, x, y, newWidth, newHeight);
503
+ resolve(canvas.toDataURL());
504
+ };
505
+ });
506
+ }
476
507
 
477
508
  // src/utils/cookie-utils.ts
478
509
  var getCookie = (name) => {
@@ -675,6 +706,7 @@ exports.checkUserAgentDspApp = checkUserAgentDspApp;
675
706
  exports.convertTextToImg = convertTextToImg;
676
707
  exports.debounce = debounce;
677
708
  exports.deleteCookie = deleteCookie;
709
+ exports.drawImageResizeCentered = drawImageResizeCentered;
678
710
  exports.fileToBase64 = fileToBase64;
679
711
  exports.getApiHostNameFromEnvironment = getApiHostNameFromEnvironment;
680
712
  exports.getBrowserName = getBrowserName;