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

Reduce event emission on canvas #6055

Merged
merged 2 commits into from
Dec 28, 2019
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
50 changes: 26 additions & 24 deletions src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,23 +827,24 @@
* @private
*/
_fireOverOutEvents: function(target, e) {
var _this = this, _hoveredTarget = this._hoveredTarget,
var _hoveredTarget = this._hoveredTarget,
_hoveredTargets = this._hoveredTargets, targets = this.targets,
diff = _hoveredTargets.length - targets.length,
diffArrayLength = diff > 0 ? diff : 0,
diffArray = [];
for (var i = 0; i < diffArrayLength; i++){
diffArray.push(null);
}
[target].concat(targets, diffArray).forEach(function(_target, index) {
_this.fireSyntheticInOutEvents(_target, e, {
oldTarget: index === 0 ? _hoveredTarget : _hoveredTargets[index - 1],
canvasEvtOut: 'mouse:out',
length = Math.max(_hoveredTargets.length, targets.length);

this.fireSyntheticInOutEvents(target, e, {
oldTarget: _hoveredTarget,
evtOut: 'mouseout',
canvasEvtOut: 'mouse:out',
evtIn: 'mouseover',
canvasEvtIn: 'mouse:over',
});
for (var i = 0; i < length; i++){
this.fireSyntheticInOutEvents(targets[i], e, {
oldTarget: _hoveredTargets[i],
evtOut: 'mouseout',
canvasEvtIn: 'mouse:over',
evtIn: 'mouseover',
});
});
}
this._hoveredTarget = target;
this._hoveredTargets = this.targets.concat();
},
Expand All @@ -855,21 +856,22 @@
* @private
*/
_fireEnterLeaveEvents: function(target, e) {
var _this = this, _draggedoverTarget = this._draggedoverTarget,
var _draggedoverTarget = this._draggedoverTarget,
_hoveredTargets = this._hoveredTargets, targets = this.targets,
diff = _hoveredTargets.length - targets.length,
diffArrayLength = diff > 0 ? diff : 0,
diffArray = [];
for (var i = 0; i < diffArrayLength; i++){
diffArray.push(null);
}
[target].concat(targets, diffArray).forEach(function(_target, index) {
_this.fireSyntheticInOutEvents(_target, e, {
oldTarget: index === 0 ? _draggedoverTarget : _hoveredTargets[index - 1],
length = Math.max(_hoveredTargets.length, targets.length);

this.fireSyntheticInOutEvents(target, e, {
oldTarget: _draggedoverTarget,
evtOut: 'dragleave',
evtIn: 'dragenter',
});
for (var i = 0; i < length; i++) {
this.fireSyntheticInOutEvents(targets[i], e, {
oldTarget: _hoveredTargets[i],
evtOut: 'dragleave',
evtIn: 'dragenter',
});
});
}
this._draggedoverTarget = target;
},

Expand Down
12 changes: 7 additions & 5 deletions test/unit/canvas_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,13 @@
var c = new fabric.Canvas();
var targetArray = [];
var targetOutArray = [];
c.on('mouse:over', function(opt) {
targetArray.push(opt.target);
});
c.on('mouse:out', function(opt) {
targetOutArray.push(opt.target);
[rect1, rect2, rect3, rect4, rect5, group1, group2, group].forEach(function(t) {
t.on('mouseover', function(opt) {
targetArray.push(opt.target);
});
t.on('mouseout', function(opt) {
targetOutArray.push(opt.target);
});
});
c.add(group, rect5);
simulateEvent(c.upperCanvasEl, 'mousemove', {
Expand Down