@threedddplus/logoeditor 0.0.173 → 0.0.175

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.
@@ -14689,6 +14689,9 @@ var SaveDesign = function SaveDesign(_ref) {
14689
14689
  var _useState = React.useState(''),
14690
14690
  designNameValidation = _useState[0],
14691
14691
  setDesignNameValidation = _useState[1];
14692
+ //@ts-ignore
14693
+ var tempCanvas = new fabric.fabric.Canvas();
14694
+ var dataURL = '';
14692
14695
  var _useState2 = React.useState(false),
14693
14696
  commaInTagName = _useState2[0],
14694
14697
  setCommaInTagName = _useState2[1];
@@ -14720,21 +14723,110 @@ var SaveDesign = function SaveDesign(_ref) {
14720
14723
  return nameValidation;
14721
14724
  } else return true;
14722
14725
  };
14726
+ /*
14727
+ const getBounds = (objects)=> {
14728
+ const minX = Math.min(...objects.map(obj => obj.left));
14729
+ const minY = Math.min(...objects.map(obj => obj.top));
14730
+ const maxX = Math.max(...objects.map(obj => obj.left + obj.width * obj.scaleX));
14731
+ const maxY = Math.max(...objects.map(obj => obj.top + obj.height * obj.scaleY));
14732
+ return {
14733
+ minX: minX,
14734
+ minY: minY,
14735
+ width: maxX - minX,
14736
+ height: maxY - minY
14737
+ };
14738
+ }
14739
+ const cropAndConvert = async()=>{
14740
+ const activeObjects = canvas.getObjects();
14741
+
14742
+ if (activeObjects.length > 0) {
14743
+ // Create a new canvas element to handle the selected objects
14744
+ const croppedCanvas = document.createElement('canvas');
14745
+ const croppedContext = croppedCanvas.getContext('2d');
14746
+ // Calculate the size of the new canvas to fit all selected objects
14747
+ const bounds = getBounds(activeObjects);
14748
+ croppedCanvas.width = bounds.width;
14749
+ croppedCanvas.height = bounds.height;
14750
+ // Draw each object onto the new canvas
14751
+ activeObjects.forEach((obj) => {
14752
+ const left = obj.left - bounds.minX;
14753
+ const top = obj.top - bounds.minY;
14754
+ if (obj.type === 'group') {
14755
+ obj._objects.forEach(element => {
14756
+ const elLeft = element.left + obj.left - bounds.minX;
14757
+ const elTop = element.top + obj.top - bounds.minY;
14758
+ console.log(element.type)
14759
+ if (element.type === 'image') {
14760
+ const imgElement = element._element;
14761
+ croppedContext.drawImage(
14762
+ imgElement,
14763
+ 0, 0, imgElement.width, imgElement.height,
14764
+ elLeft, elTop,
14765
+ element.width * element.scaleX, element.height * element.scaleY
14766
+ );
14767
+ } else if (element.type === 'text') {
14768
+ croppedContext.font = `${element.fontSize}px ${element.fontFamily}`;
14769
+ croppedContext.fillStyle = element.fill;
14770
+ croppedContext.fillText(
14771
+ element.text,
14772
+ elLeft,
14773
+ elTop + element.fontSize
14774
+ );
14775
+ }else if (element.type === 'path') {
14776
+ const path = new Path2D(element.path);
14777
+ croppedContext.fillStyle = element.fill;
14778
+ croppedContext.translate(elLeft, elTop);
14779
+ croppedContext.fill(path);
14780
+ croppedContext.translate(-elLeft, -elTop);
14781
+ }
14782
+ });
14783
+ }
14784
+ if (obj.type === 'image') {
14785
+ const imgElement = obj._element;
14786
+ croppedContext.drawImage(
14787
+ imgElement,
14788
+ 0, 0, imgElement.width, imgElement.height,
14789
+ left, top,
14790
+ obj.width * obj.scaleX, obj.height * obj.scaleY
14791
+ );
14792
+ } else if (obj.type === 'text') {
14793
+ croppedContext.font = `${obj.fontSize}px ${obj.fontFamily}`;
14794
+ croppedContext.fillStyle = obj.fill;
14795
+ croppedContext.fillText(
14796
+ obj.text,
14797
+ left,
14798
+ top + obj.fontSize
14799
+ );
14800
+ }else if (obj.type === 'path') {
14801
+ const path = new Path2D(obj.path);
14802
+ croppedContext.fillStyle = obj.fill;
14803
+ croppedContext.translate(left, top);
14804
+ croppedContext.fill(path);
14805
+ croppedContext.translate(-left, -top);
14806
+ }
14807
+ });
14808
+
14809
+
14810
+ // Convert the new canvas to a data URL in JPG format and download it
14811
+ const dataUrl = croppedCanvas.toDataURL();
14812
+ console.log(dataUrl);
14813
+ }
14814
+ }*/
14723
14815
  var onSaveHandler = /*#__PURE__*/function () {
14724
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
14725
- var canvasGetobject, pathTextsData, dataURL, canvasData, pathTextsKeys, i, pathData, o, obj, dataCh, S3UrlPath, canvasSvg, raw, horizontalLine, verticalLine;
14726
- return _regeneratorRuntime().wrap(function _callee$(_context) {
14727
- while (1) switch (_context.prev = _context.next) {
14816
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
14817
+ var canvasGetobject, pathTextsData, selectAllCanvasObjects, canvasData, pathTextsKeys, i, pathData, o, obj, dataCh, S3UrlPath, canvasSvg, raw, horizontalLine, verticalLine;
14818
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
14819
+ while (1) switch (_context2.prev = _context2.next) {
14728
14820
  case 0:
14729
14821
  if (isSaving) {
14730
- _context.next = 30;
14822
+ _context2.next = 31;
14731
14823
  break;
14732
14824
  }
14733
14825
  if (checkValidations()) {
14734
- _context.next = 3;
14826
+ _context2.next = 3;
14735
14827
  break;
14736
14828
  }
14737
- return _context.abrupt("return");
14829
+ return _context2.abrupt("return");
14738
14830
  case 3:
14739
14831
  setIsSaving(true);
14740
14832
  canvas.backgroundColor = null;
@@ -14776,12 +14868,68 @@ var SaveDesign = function SaveDesign(_ref) {
14776
14868
  };
14777
14869
  }
14778
14870
  });
14779
- dataURL = canvas.toDataURL();
14780
- canvasData = use3dddPlus.getState().applyColorFill(); // const canvasGetobject = canvas.getObjects();
14781
- // canvasData.objects.forEach((element, index) => {
14782
- // element.colorFill = canvasGetobject[index].colorFill;
14783
- // });
14784
- // looping through the text shape objects & taking out text data to inject in the canvas json data & save
14871
+ selectAllCanvasObjects = function selectAllCanvasObjects(padding) {
14872
+ if (padding === void 0) {
14873
+ padding = 20;
14874
+ }
14875
+ //@ts-ignore
14876
+ return new Promise(function (resolve, reject) {
14877
+ var objs = canvas.getObjects().map(function (obj) {
14878
+ return obj.set('active', true);
14879
+ });
14880
+ var group = new fabric.fabric.ActiveSelection(objs, {
14881
+ canvas: canvas,
14882
+ originX: 'center',
14883
+ originY: 'center'
14884
+ });
14885
+ canvas.setActiveObject(group);
14886
+ canvas.requestRenderAll();
14887
+ group.clone(function (clonedGroup) {
14888
+ var boundingRect = clonedGroup.getBoundingRect();
14889
+ var paddedWidth = boundingRect.width + 2 * padding;
14890
+ var paddedHeight = boundingRect.height + 2 * padding;
14891
+ tempCanvas.setWidth(paddedWidth);
14892
+ tempCanvas.setHeight(paddedHeight);
14893
+ clonedGroup.left = padding + boundingRect.left;
14894
+ clonedGroup.top = padding + boundingRect.top;
14895
+ clonedGroup.setCoords();
14896
+ tempCanvas.add(clonedGroup);
14897
+ clonedGroup.center();
14898
+ clonedGroup.setCoords();
14899
+ tempCanvas.requestRenderAll();
14900
+ var dataURL = tempCanvas.toDataURL();
14901
+ tempCanvas.clear();
14902
+ tempCanvas.dispose();
14903
+ clonedGroup = null;
14904
+ canvas.discardActiveObject();
14905
+ resolve(dataURL);
14906
+ });
14907
+ });
14908
+ };
14909
+ _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
14910
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
14911
+ while (1) switch (_context.prev = _context.next) {
14912
+ case 0:
14913
+ _context.prev = 0;
14914
+ _context.next = 3;
14915
+ return selectAllCanvasObjects();
14916
+ case 3:
14917
+ dataURL = _context.sent;
14918
+ console.log(dataURL); // This will log the data URL
14919
+ _context.next = 10;
14920
+ break;
14921
+ case 7:
14922
+ _context.prev = 7;
14923
+ _context.t0 = _context["catch"](0);
14924
+ console.error('Error capturing canvas objects:', _context.t0);
14925
+ case 10:
14926
+ case "end":
14927
+ return _context.stop();
14928
+ }
14929
+ }, _callee, null, [[0, 7]]);
14930
+ }))();
14931
+ // const dataURL = canvas.toDataURL();
14932
+ canvasData = use3dddPlus.getState().applyColorFill(); // looping through the text shape objects & taking out text data to inject in the canvas json data & save
14785
14933
  pathTextsKeys = Object.keys(pathTextsData);
14786
14934
  for (i = 0; i < pathTextsKeys.length; i++) {
14787
14935
  pathData = pathTextsData[pathTextsKeys[i]];
@@ -14793,10 +14941,10 @@ var SaveDesign = function SaveDesign(_ref) {
14793
14941
  }
14794
14942
  }
14795
14943
  dataCh = JSON.stringify(canvasData); // console.log('SAVE-->',dataCh);
14796
- _context.next = 16;
14944
+ _context2.next = 17;
14797
14945
  return uploadLogoConfig(dataCh, use3dddPlus.getState().customerId);
14798
- case 16:
14799
- S3UrlPath = _context.sent;
14946
+ case 17:
14947
+ S3UrlPath = _context2.sent;
14800
14948
  canvasSvg = canvas.toSVG();
14801
14949
  closeModal('', false, '', {});
14802
14950
  raw = JSON.stringify({
@@ -14840,18 +14988,18 @@ var SaveDesign = function SaveDesign(_ref) {
14840
14988
  setIsSaving(false);
14841
14989
  console.log('ERROR in Save Logo:', ex);
14842
14990
  });
14843
- case 30:
14991
+ case 31:
14844
14992
  use3dddPlus.setState({
14845
14993
  logoName: ''
14846
14994
  });
14847
14995
  use3dddPlus.setState({
14848
14996
  logoTag: []
14849
14997
  });
14850
- case 32:
14998
+ case 33:
14851
14999
  case "end":
14852
- return _context.stop();
15000
+ return _context2.stop();
14853
15001
  }
14854
- }, _callee);
15002
+ }, _callee2);
14855
15003
  }));
14856
15004
  return function onSaveHandler() {
14857
15005
  return _ref2.apply(this, arguments);