Skip to content

Commit

Permalink
GIving brushes a _render method (#4613)
Browse files Browse the repository at this point in the history
* solves error

* rerender just if there is an activeObject
  • Loading branch information
asturur committed Jan 11, 2018
1 parent 015421b commit 0a7aba7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 23 deletions.
11 changes: 11 additions & 0 deletions src/brushes/base_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ fabric.BaseBrush = fabric.util.createClass(/** @lends fabric.BaseBrush.prototype
}
},

/**
* Sets the transformation on given context
* @param {RenderingContext2d} ctx context to render on
* @private
*/
_saveAndTransform: function(ctx) {
var v = this.canvas.viewportTransform;
ctx.save();
ctx.transform(v[0], v[1], v[2], v[3], v[4], v[5]);
},

/**
* Sets brush shadow styles
* @private
Expand Down
26 changes: 21 additions & 5 deletions src/brushes/circle_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ fabric.CircleBrush = fabric.util.createClass(fabric.BaseBrush, /** @lends fabric
*/
drawDot: function(pointer) {
var point = this.addPoint(pointer),
ctx = this.canvas.contextTop,
v = this.canvas.viewportTransform;
ctx.save();
ctx.transform(v[0], v[1], v[2], v[3], v[4], v[5]);

ctx = this.canvas.contextTop;
this._saveAndTransform(ctx);
ctx.fillStyle = point.fill;
ctx.beginPath();
ctx.arc(point.x, point.y, point.radius, 0, Math.PI * 2, false);
Expand All @@ -51,6 +48,25 @@ fabric.CircleBrush = fabric.util.createClass(fabric.BaseBrush, /** @lends fabric
this.drawDot(pointer);
},

/**
* Render the full state of the brush
* @private
*/
_render: function() {
var ctx = this.canvas.contextTop, i, len,
points = this.points, point;
this._saveAndTransform(ctx);
for (i = 0, len = points.length; i < len; i++) {
point = points[i];
ctx.fillStyle = point.fill;
ctx.beginPath();
ctx.arc(point.x, point.y, point.radius, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
}
ctx.restore();
},

/**
* Invoked on mouse move
* @param {Object} pointer
Expand Down
4 changes: 1 addition & 3 deletions src/brushes/pencil_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@
*/
_render: function() {
var ctx = this.canvas.contextTop, i, len,
v = this.canvas.viewportTransform,
p1 = this._points[0],
p2 = this._points[1];

ctx.save();
ctx.transform(v[0], v[1], v[2], v[3], v[4], v[5]);
this._saveAndTransform(ctx);
ctx.beginPath();
//if we only have 2 points in the path and they are the same
//it means that the user only clicked the canvas without moving the mouse
Expand Down
38 changes: 24 additions & 14 deletions src/brushes/spray_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
this._setShadow();

this.addSprayChunk(pointer);
this.render();
this.render(this.sprayChunkPoints);
},

/**
Expand All @@ -75,7 +75,7 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
*/
onMouseMove: function(pointer) {
this.addSprayChunk(pointer);
this.render();
this.render(this.sprayChunkPoints);
},

/**
Expand All @@ -101,8 +101,6 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
originY: 'center',
fill: this.color
});

this.shadow && rect.setShadow(this.shadow);
rects.push(rect);
}
}
Expand All @@ -112,8 +110,7 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
}

var group = new fabric.Group(rects, { originX: 'center', originY: 'center' });
group.canvas = this.canvas;

this.shadow && group.setShadow(this.shadow);
this.canvas.add(group);
this.canvas.fire('path:created', { path: group });

Expand Down Expand Up @@ -147,18 +144,16 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
},

/**
* Renders brush
* Render new chunk of spray brush
*/
render: function() {
var ctx = this.canvas.contextTop;
render: function(sprayChunk) {
var ctx = this.canvas.contextTop, i, len;
ctx.fillStyle = this.color;

var v = this.canvas.viewportTransform, i, len;
ctx.save();
ctx.transform(v[0], v[1], v[2], v[3], v[4], v[5]);
this._saveAndTransform(ctx);

for (i = 0, len = this.sprayChunkPoints.length; i < len; i++) {
var point = this.sprayChunkPoints[i];
for (i = 0, len = sprayChunk.length; i < len; i++) {
var point = sprayChunk[i];
if (typeof point.opacity !== 'undefined') {
ctx.globalAlpha = point.opacity;
}
Expand All @@ -167,6 +162,21 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
ctx.restore();
},

/**
* Render all spray chunks
*/
_render: function() {
var ctx = this.canvas.contextTop, i, ilen;
ctx.fillStyle = this.color;

this._saveAndTransform(ctx);

for (i = 0, ilen = this.sprayChunks.length; i < ilen; i++) {
this.render(this.sprayChunks[i]);
}
ctx.restore();
},

/**
* @param {Object} pointer
*/
Expand Down
4 changes: 3 additions & 1 deletion src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,9 @@
*/
_onMouseDownInDrawingMode: function(e) {
this._isCurrentlyDrawing = true;
this.discardActiveObject(e).requestRenderAll();
if (this.getActiveObject()) {
this.discardActiveObject(e).requestRenderAll();
}
if (this.clipTo) {
fabric.util.clipContext(this, this.contextTop);
}
Expand Down

0 comments on commit 0a7aba7

Please sign in to comment.