@nasser-sw/fabric 7.0.1-beta6 → 7.0.1-beta8

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.js CHANGED
@@ -360,7 +360,7 @@
360
360
  }
361
361
  const cache = new Cache();
362
362
 
363
- var version = "7.0.1-beta5";
363
+ var version = "7.0.1-beta7";
364
364
 
365
365
  // use this syntax so babel plugin see this import here
366
366
  const VERSION = version;
@@ -17586,6 +17586,7 @@
17586
17586
  _defineProperty(this, "hitStrokeWidth", 'auto');
17587
17587
  _defineProperty(this, "_updatingEndpoints", false);
17588
17588
  _defineProperty(this, "_useEndpointCoords", true);
17589
+ _defineProperty(this, "_exportingSVG", false);
17589
17590
  this.setOptions(options);
17590
17591
  this.x1 = x1;
17591
17592
  this.x2 = x2;
@@ -17703,16 +17704,18 @@
17703
17704
  }
17704
17705
  setCoords() {
17705
17706
  if (this._useEndpointCoords) {
17706
- // Set the object's center to the geometric center of the line
17707
- const center = this._findCenterFromElement();
17708
- this.left = center.x;
17709
- this.top = center.y;
17710
-
17711
17707
  // Set width and height for hit detection and bounding box
17712
17708
  const effectiveStrokeWidth = this.hitStrokeWidth === 'auto' ? this.strokeWidth : this.hitStrokeWidth;
17713
17709
  const hitPadding = Math.max(effectiveStrokeWidth / 2 + 5, 10);
17714
17710
  this.width = Math.abs(this.x2 - this.x1) + hitPadding * 2;
17715
17711
  this.height = Math.abs(this.y2 - this.y1) + hitPadding * 2;
17712
+
17713
+ // Only update left/top if they haven't been explicitly set (e.g., during loading)
17714
+ if (this.left === 0 && this.top === 0) {
17715
+ const center = this._findCenterFromElement();
17716
+ this.left = center.x;
17717
+ this.top = center.y;
17718
+ }
17716
17719
  }
17717
17720
  super.setCoords();
17718
17721
  }
@@ -17795,6 +17798,14 @@
17795
17798
  this.x2 = newX;
17796
17799
  this.y2 = newY;
17797
17800
  }
17801
+
17802
+ // Update gradient coordinates if stroke is a gradient (but not during SVG export)
17803
+ if (this.stroke instanceof Gradient && !this._exportingSVG) {
17804
+ this.stroke.coords.x1 = this.x1;
17805
+ this.stroke.coords.y1 = this.y1;
17806
+ this.stroke.coords.x2 = this.x2;
17807
+ this.stroke.coords.y2 = this.y2;
17808
+ }
17798
17809
  this.dirty = true;
17799
17810
  this.setCoords();
17800
17811
  (_this$canvas3 = this.canvas) === null || _this$canvas3 === void 0 || _this$canvas3.requestRenderAll();
@@ -17865,6 +17876,14 @@
17865
17876
  if (coordProps.includes(key)) {
17866
17877
  this._setWidthHeight();
17867
17878
  this.dirty = true;
17879
+
17880
+ // Update gradient coordinates if stroke is a gradient (but not during SVG export)
17881
+ if (this.stroke instanceof Gradient && !this._exportingSVG) {
17882
+ this.stroke.coords.x1 = this.x1;
17883
+ this.stroke.coords.y1 = this.y1;
17884
+ this.stroke.coords.x2 = this.x2;
17885
+ this.stroke.coords.y2 = this.y2;
17886
+ }
17868
17887
  }
17869
17888
  if ((key === 'left' || key === 'top') && this.canvas && !this._updatingEndpoints) {
17870
17889
  const deltaX = this.left - oldLeft;
@@ -17875,6 +17894,14 @@
17875
17894
  this.y1 += deltaY;
17876
17895
  this.x2 += deltaX;
17877
17896
  this.y2 += deltaY;
17897
+
17898
+ // Update gradient coordinates if stroke is a gradient
17899
+ if (this.stroke instanceof Gradient) {
17900
+ this.stroke.coords.x1 = this.x1;
17901
+ this.stroke.coords.y1 = this.y1;
17902
+ this.stroke.coords.x2 = this.x2;
17903
+ this.stroke.coords.y2 = this.y2;
17904
+ }
17878
17905
  this._updatingEndpoints = false;
17879
17906
  }
17880
17907
  }
@@ -17888,17 +17915,23 @@
17888
17915
  super.render(ctx);
17889
17916
  }
17890
17917
  _renderDirectly(ctx) {
17891
- var _this$stroke;
17892
17918
  if (!this.visible) return;
17893
17919
  ctx.save();
17894
17920
  ctx.globalAlpha = this.opacity;
17895
- ctx.strokeStyle = ((_this$stroke = this.stroke) === null || _this$stroke === void 0 ? void 0 : _this$stroke.toString()) || '#000';
17896
17921
  ctx.lineWidth = this.strokeWidth;
17897
17922
  ctx.lineCap = this.strokeLineCap || 'butt';
17898
17923
  ctx.beginPath();
17899
17924
  ctx.moveTo(this.x1, this.y1);
17900
17925
  ctx.lineTo(this.x2, this.y2);
17926
+ const origStrokeStyle = ctx.strokeStyle;
17927
+ if (isFiller(this.stroke)) {
17928
+ ctx.strokeStyle = this.stroke.toLive(ctx);
17929
+ } else {
17930
+ var _this$stroke;
17931
+ ctx.strokeStyle = ((_this$stroke = this.stroke) === null || _this$stroke === void 0 ? void 0 : _this$stroke.toString()) || '#000';
17932
+ }
17901
17933
  ctx.stroke();
17934
+ ctx.strokeStyle = origStrokeStyle;
17902
17935
  ctx.restore();
17903
17936
  }
17904
17937
  _render(ctx) {
@@ -17920,6 +17953,15 @@
17920
17953
  }
17921
17954
  toObject() {
17922
17955
  let propertiesToInclude = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
17956
+ if (this._useEndpointCoords) {
17957
+ return {
17958
+ ...super.toObject(propertiesToInclude),
17959
+ x1: this.x1,
17960
+ y1: this.y1,
17961
+ x2: this.x2,
17962
+ y2: this.y2
17963
+ };
17964
+ }
17923
17965
  return {
17924
17966
  ...super.toObject(propertiesToInclude),
17925
17967
  ...this.calcLinePoints()
@@ -17964,7 +18006,15 @@
17964
18006
  _toSVG() {
17965
18007
  if (this._useEndpointCoords) {
17966
18008
  // Use absolute coordinates to bypass all Fabric.js transforms
17967
- return [`<line stroke="${this.stroke}" stroke-width="${this.strokeWidth}" stroke-linecap="${this.strokeLineCap}" `, `x1="${this.x1}" y1="${this.y1}" x2="${this.x2}" y2="${this.y2}" />\n`];
18009
+ // Handle gradients manually for proper SVG export
18010
+ let strokeAttr = '';
18011
+ if (this.stroke instanceof Gradient) {
18012
+ // Let Fabric.js handle gradient definition, but we'll use the reference
18013
+ strokeAttr = `stroke="url(#${this.stroke.id})"`;
18014
+ } else {
18015
+ strokeAttr = `stroke="${this.stroke || 'none'}"`;
18016
+ }
18017
+ return [`<line ${strokeAttr} stroke-width="${this.strokeWidth}" stroke-linecap="${this.strokeLineCap}" `, `stroke-dasharray="${this.strokeDashArray ? this.strokeDashArray.join(' ') : 'none'}" `, `stroke-dashoffset="${this.strokeDashOffset}" stroke-linejoin="${this.strokeLineJoin}" `, `stroke-miterlimit="${this.strokeMiterLimit}" fill="${this.fill || 'none'}" `, `fill-rule="${this.fillRule}" opacity="${this.opacity}" `, `x1="${this.x1}" y1="${this.y1}" x2="${this.x2}" y2="${this.y2}" />\n`];
17968
18018
  } else {
17969
18019
  // Use standard calcLinePoints for legacy mode
17970
18020
  const {
@@ -17978,9 +18028,26 @@
17978
18028
  }
17979
18029
  toSVG(reviver) {
17980
18030
  if (this._useEndpointCoords) {
17981
- // Override toSVG to prevent Fabric.js from adding transform wrapper
17982
- const markup = this._toSVG().join('');
17983
- return reviver ? reviver(markup) : markup;
18031
+ // For endpoint coords, we need to bypass transforms but still allow gradients
18032
+ // Let's temporarily disable transforms during SVG generation
18033
+ const originalLeft = this.left;
18034
+ const originalTop = this.top;
18035
+
18036
+ // Set position to center of line for gradient calculation
18037
+ this.left = (this.x1 + this.x2) / 2;
18038
+ this.top = (this.y1 + this.y2) / 2;
18039
+
18040
+ // Get the SVG with standard system (for gradient handling)
18041
+ const standardSVG = super.toSVG(reviver);
18042
+
18043
+ // Restore original position
18044
+ this.left = originalLeft;
18045
+ this.top = originalTop;
18046
+
18047
+ // Extract gradient definition and clean up the line element
18048
+ // Remove the transform wrapper and update coordinates
18049
+ const cleanSVG = standardSVG.replace(/<g transform="[^"]*"[^>]*>/g, '').replace(/<\/g>/g, '').replace(/x1="[^"]*"/g, `x1="${this.x1}"`).replace(/y1="[^"]*"/g, `y1="${this.y1}"`).replace(/x2="[^"]*"/g, `x2="${this.x2}"`).replace(/y2="[^"]*"/g, `y2="${this.y2}"`);
18050
+ return cleanSVG;
17984
18051
  }
17985
18052
  // Use default behavior for legacy mode
17986
18053
  return super.toSVG(reviver);