From 4fab4335d9cd22ace20bd6eb167376f1003a3bf5 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sun, 17 Sep 2017 17:50:50 +0200 Subject: [PATCH] Modify pencil brush to fix displacement (#4318) --- src/brushes/pencil_brush.class.js | 47 ++++++++++++++++++------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/brushes/pencil_brush.class.js b/src/brushes/pencil_brush.class.js index 0905163a0bd..d1f19f32d8d 100644 --- a/src/brushes/pencil_brush.class.js +++ b/src/brushes/pencil_brush.class.js @@ -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]; @@ -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); @@ -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; }, @@ -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);