Skip to content

Commit

Permalink
feat(Brushes): add beforePathCreated event (#6492)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrz authored Aug 7, 2020
1 parent d3af410 commit bacad22
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/brushes/circle_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ fabric.CircleBrush = fabric.util.createClass(fabric.BaseBrush, /** @lends fabric
var group = new fabric.Group(circles);
group.canvas = this.canvas;

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

Expand Down
1 change: 1 addition & 0 deletions src/brushes/pencil_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@

var path = this.createPath(pathData);
this.canvas.clearContext(this.canvas.contextTop);
this.canvas.fire('before:path:created', { path: path });
this.canvas.add(path);
this.canvas.requestRenderAll();
path.setCoords();
Expand Down
1 change: 1 addition & 0 deletions src/brushes/spray_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric

var group = new fabric.Group(rects);
this.shadow && group.set('shadow', new fabric.Shadow(this.shadow));
this.canvas.fire('before:path:created', { path: group });
this.canvas.add(group);
this.canvas.fire('path:created', { path: group });

Expand Down
11 changes: 8 additions & 3 deletions test/unit/brushes.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@
assert.equal(brush._points.length, 6, 'concident points are discarded');
});
QUnit.test('fabric pencil brush multiple points not discarded', function(assert) {
var fired = false;
var fireBeforePathCreatedEvent = false;
var firePathCreatedEvent = false;
var added = null;
canvas.on('before:path:created', function() {
fireBeforePathCreatedEvent = true;
});
canvas.on('path:created', function(opt) {
fired = true;
firePathCreatedEvent = true;
added = opt.path;
});
var brush = new fabric.PencilBrush(canvas);
Expand All @@ -76,7 +80,8 @@
brush.onMouseMove(pointer2, { e: {} });
brush.onMouseMove(pointer3, { e: {} });
brush.onMouseUp({ e: {} });
assert.equal(fired, true, 'event is fired');
assert.equal(fireBeforePathCreatedEvent, true, 'before:path:created event is fired');
assert.equal(firePathCreatedEvent, true, 'path:created event is fired');
assert.ok(added instanceof fabric.Path, 'a path is added');
assert.ok(added.path.length, 6, 'path has 6 steps');
canvas.off();
Expand Down

0 comments on commit bacad22

Please sign in to comment.