Skip to content

Commit

Permalink
add drawDot func to pencilbrush (fabricjs#4743)
Browse files Browse the repository at this point in the history
  • Loading branch information
boomyao authored and asturur committed Feb 21, 2018
1 parent d1e5337 commit f855bbc
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/brushes/pencil_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
this._points = [];
},

/**
* Invoked inside on mouse down and mouse move
* @param {Object} pointer
*/
_drawSegment: function (ctx, p1, p2) {
var midPoint = p1.midPointFrom(p2);
ctx.quadraticCurveTo(p1.x, p1.y, midPoint.x, midPoint.y);
return midPoint;
},

/**
* Inovoked on mouse down
* @param {Object} pointer
Expand All @@ -34,17 +44,25 @@
* @param {Object} pointer
*/
onMouseMove: function(pointer) {
this._captureDrawingPath(pointer);
// redraw curve
// clear top canvas
this.canvas.clearContext(this.canvas.contextTop);
this._render();
if (this._captureDrawingPath(pointer) && this._points.length > 1) {
var points = this._points, length = points.length, ctx = this.canvas.contextTop;
// draw the curve update
this._saveAndTransform(ctx);
if (this.oldEnd) {
ctx.beginPath();
ctx.moveTo(this.oldEnd.x, this.oldEnd.y);
}
this.oldEnd = this._drawSegment(ctx, points[length - 2], points[length - 1], true);
ctx.stroke();
ctx.restore();
}
},

/**
* Invoked on mouse up
*/
onMouseUp: function() {
this.oldEnd = undefined;
this._finalizeAndAddPath();
},

Expand All @@ -58,7 +76,6 @@

this._reset();
this._addPoint(p);

this.canvas.contextTop.moveTo(p.x, p.y);
},

Expand All @@ -68,9 +85,10 @@
*/
_addPoint: function(point) {
if (this._points.length > 1 && point.eq(this._points[this._points.length - 1])) {
return;
return false;
}
this._points.push(point);
return true;
},

/**
Expand All @@ -79,7 +97,6 @@
*/
_reset: function() {
this._points.length = 0;

this._setBrushStyles();
this._setShadow();
},
Expand All @@ -90,7 +107,7 @@
*/
_captureDrawingPath: function(pointer) {
var pointerPoint = new fabric.Point(pointer.x, pointer.y);
this._addPoint(pointerPoint);
return this._addPoint(pointerPoint);
},

/**
Expand Down Expand Up @@ -120,9 +137,7 @@
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);
ctx.quadraticCurveTo(p1.x, p1.y, midPoint.x, midPoint.y);

this._drawSegment(ctx, p1, p2);
p1 = this._points[i];
p2 = this._points[i + 1];
}
Expand Down

0 comments on commit f855bbc

Please sign in to comment.