Skip to content

Commit

Permalink
Modify pencil brush to fix displacement (#4318)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Sep 17, 2017
1 parent c78c249 commit 4fab433
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/brushes/pencil_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
* @private
*/
_render: function() {
var ctx = this.canvas.contextTop,
var ctx = this.canvas.contextTop, i, len,
v = this.canvas.viewportTransform,
p1 = this._points[0],
p2 = this._points[1];
Expand All @@ -109,12 +109,15 @@
//then we should be drawing a dot. A path isn't drawn between two identical dots
//that's why we set them apart a bit
if (this._points.length === 2 && p1.x === p2.x && p1.y === p2.y) {
p1.x -= 0.5;
p2.x += 0.5;
var width = this.width / 1000;
p1 = new fabric.Point(p1.x, p1.y);
p2 = new fabric.Point(p2.x, p2.y);
p1.x -= width;
p2.x += width;
}
ctx.moveTo(p1.x, p1.y);

for (var i = 1, len = this._points.length; i < len; i++) {
for (i = 1, len = this._points.length; i < len; i++) {
// we pick the point between pi + 1 & pi + 2 as the
// end point and p1 as our control point.
var midPoint = p1.midPointFrom(p2);
Expand All @@ -137,23 +140,26 @@
* @return {String} SVG path
*/
convertPointsToSVGPath: function(points) {
var path = [],
var path = [], i, width = this.width / 1000,
p1 = new fabric.Point(points[0].x, points[0].y),
p2 = new fabric.Point(points[1].x, points[1].y);

path.push('M ', points[0].x, ' ', points[0].y, ' ');
for (var i = 1, len = points.length; i < len; i++) {
var midPoint = p1.midPointFrom(p2);
// p1 is our bezier control point
// midpoint is our endpoint
// start point is p(i-1) value.
path.push('Q ', p1.x, ' ', p1.y, ' ', midPoint.x, ' ', midPoint.y, ' ');
p1 = new fabric.Point(points[i].x, points[i].y);
p2 = new fabric.Point(points[1].x, points[1].y),
len = points.length;

path.push('M ', p1.x - width, ' ', p1.y, ' ');
for (i = 1; i < len; i++) {
if (!p1.eq(p2)) {
var midPoint = p1.midPointFrom(p2);
// p1 is our bezier control point
// midpoint is our endpoint
// start point is p(i-1) value.
path.push('Q ', p1.x, ' ', p1.y, ' ', midPoint.x, ' ', midPoint.y, ' ');
}
p1 = points[i];
if ((i + 1) < points.length) {
p2 = new fabric.Point(points[i + 1].x, points[i + 1].y);
p2 = points[i + 1];
}
}
path.push('L ', p1.x, ' ', p1.y, ' ');
path.push('L ', p1.x + width, ' ', p1.y, ' ');
return path;
},

Expand All @@ -170,10 +176,11 @@
strokeLineCap: this.strokeLineCap,
strokeLineJoin: this.strokeLineJoin,
strokeDashArray: this.strokeDashArray,
originX: 'center',
originY: 'center'
});

var position = new fabric.Point(path.left + path.width / 2, path.top + path.height / 2);
position = path.translateToGivenOrigin(position, 'center', 'center', path.originX, path.originY);
path.top = position.y;
path.left = position.x;
if (this.shadow) {
this.shadow.affectStroke = true;
path.setShadow(this.shadow);
Expand Down

0 comments on commit 4fab433

Please sign in to comment.