@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.mjs CHANGED
@@ -354,7 +354,7 @@ class Cache {
354
354
  }
355
355
  const cache = new Cache();
356
356
 
357
- var version = "7.0.1-beta5";
357
+ var version = "7.0.1-beta7";
358
358
 
359
359
  // use this syntax so babel plugin see this import here
360
360
  const VERSION = version;
@@ -17580,6 +17580,7 @@ class Line extends FabricObject {
17580
17580
  _defineProperty(this, "hitStrokeWidth", 'auto');
17581
17581
  _defineProperty(this, "_updatingEndpoints", false);
17582
17582
  _defineProperty(this, "_useEndpointCoords", true);
17583
+ _defineProperty(this, "_exportingSVG", false);
17583
17584
  this.setOptions(options);
17584
17585
  this.x1 = x1;
17585
17586
  this.x2 = x2;
@@ -17697,16 +17698,18 @@ class Line extends FabricObject {
17697
17698
  }
17698
17699
  setCoords() {
17699
17700
  if (this._useEndpointCoords) {
17700
- // Set the object's center to the geometric center of the line
17701
- const center = this._findCenterFromElement();
17702
- this.left = center.x;
17703
- this.top = center.y;
17704
-
17705
17701
  // Set width and height for hit detection and bounding box
17706
17702
  const effectiveStrokeWidth = this.hitStrokeWidth === 'auto' ? this.strokeWidth : this.hitStrokeWidth;
17707
17703
  const hitPadding = Math.max(effectiveStrokeWidth / 2 + 5, 10);
17708
17704
  this.width = Math.abs(this.x2 - this.x1) + hitPadding * 2;
17709
17705
  this.height = Math.abs(this.y2 - this.y1) + hitPadding * 2;
17706
+
17707
+ // Only update left/top if they haven't been explicitly set (e.g., during loading)
17708
+ if (this.left === 0 && this.top === 0) {
17709
+ const center = this._findCenterFromElement();
17710
+ this.left = center.x;
17711
+ this.top = center.y;
17712
+ }
17710
17713
  }
17711
17714
  super.setCoords();
17712
17715
  }
@@ -17789,6 +17792,14 @@ class Line extends FabricObject {
17789
17792
  this.x2 = newX;
17790
17793
  this.y2 = newY;
17791
17794
  }
17795
+
17796
+ // Update gradient coordinates if stroke is a gradient (but not during SVG export)
17797
+ if (this.stroke instanceof Gradient && !this._exportingSVG) {
17798
+ this.stroke.coords.x1 = this.x1;
17799
+ this.stroke.coords.y1 = this.y1;
17800
+ this.stroke.coords.x2 = this.x2;
17801
+ this.stroke.coords.y2 = this.y2;
17802
+ }
17792
17803
  this.dirty = true;
17793
17804
  this.setCoords();
17794
17805
  (_this$canvas3 = this.canvas) === null || _this$canvas3 === void 0 || _this$canvas3.requestRenderAll();
@@ -17859,6 +17870,14 @@ class Line extends FabricObject {
17859
17870
  if (coordProps.includes(key)) {
17860
17871
  this._setWidthHeight();
17861
17872
  this.dirty = true;
17873
+
17874
+ // Update gradient coordinates if stroke is a gradient (but not during SVG export)
17875
+ if (this.stroke instanceof Gradient && !this._exportingSVG) {
17876
+ this.stroke.coords.x1 = this.x1;
17877
+ this.stroke.coords.y1 = this.y1;
17878
+ this.stroke.coords.x2 = this.x2;
17879
+ this.stroke.coords.y2 = this.y2;
17880
+ }
17862
17881
  }
17863
17882
  if ((key === 'left' || key === 'top') && this.canvas && !this._updatingEndpoints) {
17864
17883
  const deltaX = this.left - oldLeft;
@@ -17869,6 +17888,14 @@ class Line extends FabricObject {
17869
17888
  this.y1 += deltaY;
17870
17889
  this.x2 += deltaX;
17871
17890
  this.y2 += deltaY;
17891
+
17892
+ // Update gradient coordinates if stroke is a gradient
17893
+ if (this.stroke instanceof Gradient) {
17894
+ this.stroke.coords.x1 = this.x1;
17895
+ this.stroke.coords.y1 = this.y1;
17896
+ this.stroke.coords.x2 = this.x2;
17897
+ this.stroke.coords.y2 = this.y2;
17898
+ }
17872
17899
  this._updatingEndpoints = false;
17873
17900
  }
17874
17901
  }
@@ -17882,17 +17909,23 @@ class Line extends FabricObject {
17882
17909
  super.render(ctx);
17883
17910
  }
17884
17911
  _renderDirectly(ctx) {
17885
- var _this$stroke;
17886
17912
  if (!this.visible) return;
17887
17913
  ctx.save();
17888
17914
  ctx.globalAlpha = this.opacity;
17889
- ctx.strokeStyle = ((_this$stroke = this.stroke) === null || _this$stroke === void 0 ? void 0 : _this$stroke.toString()) || '#000';
17890
17915
  ctx.lineWidth = this.strokeWidth;
17891
17916
  ctx.lineCap = this.strokeLineCap || 'butt';
17892
17917
  ctx.beginPath();
17893
17918
  ctx.moveTo(this.x1, this.y1);
17894
17919
  ctx.lineTo(this.x2, this.y2);
17920
+ const origStrokeStyle = ctx.strokeStyle;
17921
+ if (isFiller(this.stroke)) {
17922
+ ctx.strokeStyle = this.stroke.toLive(ctx);
17923
+ } else {
17924
+ var _this$stroke;
17925
+ ctx.strokeStyle = ((_this$stroke = this.stroke) === null || _this$stroke === void 0 ? void 0 : _this$stroke.toString()) || '#000';
17926
+ }
17895
17927
  ctx.stroke();
17928
+ ctx.strokeStyle = origStrokeStyle;
17896
17929
  ctx.restore();
17897
17930
  }
17898
17931
  _render(ctx) {
@@ -17914,6 +17947,15 @@ class Line extends FabricObject {
17914
17947
  }
17915
17948
  toObject() {
17916
17949
  let propertiesToInclude = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
17950
+ if (this._useEndpointCoords) {
17951
+ return {
17952
+ ...super.toObject(propertiesToInclude),
17953
+ x1: this.x1,
17954
+ y1: this.y1,
17955
+ x2: this.x2,
17956
+ y2: this.y2
17957
+ };
17958
+ }
17917
17959
  return {
17918
17960
  ...super.toObject(propertiesToInclude),
17919
17961
  ...this.calcLinePoints()
@@ -17958,7 +18000,15 @@ class Line extends FabricObject {
17958
18000
  _toSVG() {
17959
18001
  if (this._useEndpointCoords) {
17960
18002
  // Use absolute coordinates to bypass all Fabric.js transforms
17961
- 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`];
18003
+ // Handle gradients manually for proper SVG export
18004
+ let strokeAttr = '';
18005
+ if (this.stroke instanceof Gradient) {
18006
+ // Let Fabric.js handle gradient definition, but we'll use the reference
18007
+ strokeAttr = `stroke="url(#${this.stroke.id})"`;
18008
+ } else {
18009
+ strokeAttr = `stroke="${this.stroke || 'none'}"`;
18010
+ }
18011
+ 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`];
17962
18012
  } else {
17963
18013
  // Use standard calcLinePoints for legacy mode
17964
18014
  const {
@@ -17972,9 +18022,26 @@ class Line extends FabricObject {
17972
18022
  }
17973
18023
  toSVG(reviver) {
17974
18024
  if (this._useEndpointCoords) {
17975
- // Override toSVG to prevent Fabric.js from adding transform wrapper
17976
- const markup = this._toSVG().join('');
17977
- return reviver ? reviver(markup) : markup;
18025
+ // For endpoint coords, we need to bypass transforms but still allow gradients
18026
+ // Let's temporarily disable transforms during SVG generation
18027
+ const originalLeft = this.left;
18028
+ const originalTop = this.top;
18029
+
18030
+ // Set position to center of line for gradient calculation
18031
+ this.left = (this.x1 + this.x2) / 2;
18032
+ this.top = (this.y1 + this.y2) / 2;
18033
+
18034
+ // Get the SVG with standard system (for gradient handling)
18035
+ const standardSVG = super.toSVG(reviver);
18036
+
18037
+ // Restore original position
18038
+ this.left = originalLeft;
18039
+ this.top = originalTop;
18040
+
18041
+ // Extract gradient definition and clean up the line element
18042
+ // Remove the transform wrapper and update coordinates
18043
+ 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}"`);
18044
+ return cleanSVG;
17978
18045
  }
17979
18046
  // Use default behavior for legacy mode
17980
18047
  return super.toSVG(reviver);