Skip to content

Commit

Permalink
Add a new event and fixed the free drawing point displacement (#4311)
Browse files Browse the repository at this point in the history
* things

* fixed drawing paths

* added jsdocs
  • Loading branch information
asturur authored Sep 16, 2017
1 parent 124da9c commit 87f4fc9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
36 changes: 21 additions & 15 deletions src/brushes/pencil_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@
//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);

Expand All @@ -137,23 +140,26 @@
* @return {String} SVG path
*/
convertPointsToSVGPath: function(points) {
var path = [], i, len,
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 (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 Down
2 changes: 2 additions & 0 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*
* @fires before:selection:cleared
* @fires selection:cleared
* @fires selection:updated
* @fires selection:created
*
* @fires path:created
* @fires mouse:down
Expand Down
4 changes: 3 additions & 1 deletion src/mixins/canvas_grouping.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
var activeSelection = this._activeObject;
if (activeSelection.contains(target)) {
activeSelection.removeWithUpdate(target);
target.fire('deselected', { e: e });
this._hoveredTarget = target;
if (activeSelection.size() === 1) {
// activate last remaining object
Expand All @@ -59,9 +60,10 @@
}
else {
activeSelection.addWithUpdate(target);
target.fire('selected', { e: e });
this._hoveredTarget = activeSelection;
}
this.fire('selection:created', { target: activeSelection, e: e });
this.fire('selection:updated', { target: activeSelection, e: e, updated: target });
},

/**
Expand Down

0 comments on commit 87f4fc9

Please sign in to comment.