Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Brushes): add beforePathCreated event #6492

Merged
merged 4 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
linrz marked this conversation as resolved.
Show resolved Hide resolved
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