Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strokeUniform property #5473

Merged
merged 3 commits into from
Feb 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/mixins/object_geometry.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,16 @@
if (skewX === 0 && skewY === 0) {
return { x: dimensions.x * this.scaleX, y: dimensions.y * this.scaleY };
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was supposed to be a shortcut to save on calculations... keeping it now kills the feature. Or maybe we need to reorganize a bit the code. Most of the peple work without the skew values, not point in going for 4 point * matrix + bbox calculations. But i can agree is not probably a bottleneck.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about this? would keep the shortcut somehow.

_getTransformedDimensions: function(skewX, skewY) {
      if (typeof skewX === 'undefined') {
        skewX = this.skewX;
      }
      if (typeof skewY === 'undefined') {
        skewY = this.skewY;
      }
      var dimensions = this._getNonTransformedDimensions(), dimX, dimY,
            noSkew = skewX === 0 && skewY === 0;

      if (this.strokeUniform) {
        dimX = this.width;
        dimY = this.height;
      }
      else {
        dimX = dimensions.x;
        dimY = dimensions.y;
      }
      if (noSkew) {
       return _finalizeDiemensions(dimX, dimY);
      }
      else {
        dimX /= 2;
        dimY /= 2;
      }
      var points = [
            {
              x: -dimX,
              y: -dimY
            },
            {
              x: dimX,
              y: -dimY
            },
            {
              x: -dimX,
              y: dimY
            },
            {
              x: dimX,
              y: dimY
            }],
          i, transformMatrix = this._calcDimensionsTransformMatrix(skewX, skewY, false),
          bbox;
      for (i = 0; i < points.length; i++) {
        points[i] = fabric.util.transformPoint(points[i], transformMatrix);
      }
      bbox = fabric.util.makeBoundingBoxFromPoints(points);
      return this._finalizeDiemensions(bbox.width, bbox.height);
    },

    _finalizeDiemensions(width, height) {
      return this.strokeUniform ?
        { x: width + this.strokeWidth, y: height + this.strokeWidth }
        :
        { x: width, y: height };
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup that makes sense.

var dimX = dimensions.x / 2, dimY = dimensions.y / 2,
points = [
var dimX, dimY;
if (this.strokeUniform) {
dimX = this.width / 2;
dimY = this.height / 2;
}
else {
dimX = dimensions.x / 2;
dimY = dimensions.y / 2;
}
var points = [
{
x: -dimX,
y: -dimY
Expand All @@ -616,7 +624,10 @@
points[i] = fabric.util.transformPoint(points[i], transformMatrix);
}
bbox = fabric.util.makeBoundingBoxFromPoints(points);
return { x: bbox.width, y: bbox.height };
return this.strokeUniform ?
{ x: bbox.width + this.strokeWidth, y: bbox.height + this.strokeWidth }
:
{ x: bbox.width, y: bbox.height };
},

/*
Expand Down
18 changes: 18 additions & 0 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,18 @@
*/
noScaleCache: true,

/**
* When `false`, the stoke width will scale with the object.
* When `true`, the stroke will always match the exact pixel size entered for stroke width.
* default to false
* @since 2.6.0
* @type Boolean
* @default false
* @type Boolean
* @default false
*/
strokeUniform: false,

/**
* When set to `true`, object's cache will be rerendered next render call.
* since 1.7.0
Expand Down Expand Up @@ -1323,6 +1335,9 @@
else {
alternative && alternative(ctx);
}
if (this.strokeUniform) {
ctx.setLineDash(ctx.getLineDash().map(function(value) { return value * ctx.lineWidth; }));
}
},

/**
Expand Down Expand Up @@ -1460,6 +1475,9 @@
}

ctx.save();
if (this.strokeUniform) {
ctx.scale(1 / this.scaleX, 1 / this.scaleY);
}
this._setLineDash(ctx, this.strokeDashArray, this._renderDashedStroke);
this._applyPatternGradientTransform(ctx, this.stroke);
ctx.stroke();
Expand Down