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

Add enablePointerEvents to Canvas init option #5589

Merged
merged 4 commits into from
May 3, 2019
Merged
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
68 changes: 38 additions & 30 deletions src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,28 @@
},

addOrRemove: function(functor, eventjsFunctor) {
var canvasElement = this.upperCanvasEl,
eventTypePrefix = this.enablePointerEvents ? 'pointer' : 'mouse';
functor(fabric.window, 'resize', this._onResize);
functor(this.upperCanvasEl, 'mousedown', this._onMouseDown);
functor(this.upperCanvasEl, 'mousemove', this._onMouseMove, addEventOptions);
functor(this.upperCanvasEl, 'mouseout', this._onMouseOut);
functor(this.upperCanvasEl, 'mouseenter', this._onMouseEnter);
functor(this.upperCanvasEl, 'wheel', this._onMouseWheel);
functor(this.upperCanvasEl, 'contextmenu', this._onContextMenu);
functor(this.upperCanvasEl, 'dblclick', this._onDoubleClick);
functor(this.upperCanvasEl, 'touchstart', this._onMouseDown, addEventOptions);
functor(this.upperCanvasEl, 'touchmove', this._onMouseMove, addEventOptions);
functor(this.upperCanvasEl, 'dragover', this._onDragOver);
functor(this.upperCanvasEl, 'dragenter', this._onDragEnter);
functor(this.upperCanvasEl, 'dragleave', this._onDragLeave);
functor(this.upperCanvasEl, 'drop', this._onDrop);
functor(canvasElement, eventTypePrefix + 'down', this._onMouseDown);
functor(canvasElement, eventTypePrefix + 'move', this._onMouseMove, addEventOptions);
functor(canvasElement, eventTypePrefix + 'out', this._onMouseOut);
functor(canvasElement, eventTypePrefix + 'enter', this._onMouseEnter);
functor(canvasElement, 'wheel', this._onMouseWheel);
functor(canvasElement, 'contextmenu', this._onContextMenu);
functor(canvasElement, 'dblclick', this._onDoubleClick);
functor(canvasElement, 'touchstart', this._onMouseDown, addEventOptions);
functor(canvasElement, 'touchmove', this._onMouseMove, addEventOptions);
functor(canvasElement, 'dragover', this._onDragOver);
functor(canvasElement, 'dragenter', this._onDragEnter);
functor(canvasElement, 'dragleave', this._onDragLeave);
functor(canvasElement, 'drop', this._onDrop);
if (typeof eventjs !== 'undefined' && eventjsFunctor in eventjs) {
eventjs[eventjsFunctor](this.upperCanvasEl, 'gesture', this._onGesture);
eventjs[eventjsFunctor](this.upperCanvasEl, 'drag', this._onDrag);
eventjs[eventjsFunctor](this.upperCanvasEl, 'orientation', this._onOrientationChange);
eventjs[eventjsFunctor](this.upperCanvasEl, 'shake', this._onShake);
eventjs[eventjsFunctor](this.upperCanvasEl, 'longpress', this._onLongPress);
eventjs[eventjsFunctor](canvasElement, 'gesture', this._onGesture);
eventjs[eventjsFunctor](canvasElement, 'drag', this._onDrag);
eventjs[eventjsFunctor](canvasElement, 'orientation', this._onOrientationChange);
eventjs[eventjsFunctor](canvasElement, 'shake', this._onShake);
eventjs[eventjsFunctor](canvasElement, 'longpress', this._onLongPress);
}
},

Expand All @@ -79,9 +81,10 @@
removeListeners: function() {
this.addOrRemove(removeListener, 'remove');
// if you dispose on a mouseDown, before mouse up, you need to clean document to...
removeListener(fabric.document, 'mouseup', this._onMouseUp);
var eventTypePrefix = this.enablePointerEvents ? 'pointer' : 'mouse';
removeListener(fabric.document, eventTypePrefix + 'up', this._onMouseUp);
removeListener(fabric.document, 'touchend', this._onMouseUp, addEventOptions);
removeListener(fabric.document, 'mousemove', this._onMouseMove, addEventOptions);
removeListener(fabric.document, eventTypePrefix + 'move', this._onMouseMove, addEventOptions);
removeListener(fabric.document, 'touchmove', this._onMouseMove, addEventOptions);
},

Expand Down Expand Up @@ -239,16 +242,18 @@
addListener(fabric.document, 'touchend', this._onMouseUp, addEventOptions);
addListener(fabric.document, 'touchmove', this._onMouseMove, addEventOptions);

removeListener(this.upperCanvasEl, 'mousemove', this._onMouseMove, addEventOptions);
removeListener(this.upperCanvasEl, 'touchmove', this._onMouseMove, addEventOptions);
var canvasElement = this.upperCanvasEl,
eventTypePrefix = this.enablePointerEvents ? 'pointer' : 'mouse';
removeListener(canvasElement, eventTypePrefix + 'move', this._onMouseMove, addEventOptions);
removeListener(canvasElement, 'touchmove', this._onMouseMove, addEventOptions);

if (e.type === 'touchstart') {
// Unbind mousedown to prevent double triggers from touch devices
removeListener(this.upperCanvasEl, 'mousedown', this._onMouseDown);
removeListener(canvasElement, eventTypePrefix + 'down', this._onMouseDown);
}
else {
addListener(fabric.document, 'mouseup', this._onMouseUp);
addListener(fabric.document, 'mousemove', this._onMouseMove, addEventOptions);
addListener(fabric.document, eventTypePrefix + 'up', this._onMouseUp);
addListener(fabric.document, eventTypePrefix + 'move', this._onMouseMove, addEventOptions);
}
},

Expand All @@ -259,21 +264,24 @@
_onMouseUp: function (e) {
this.__onMouseUp(e);
this._resetTransformEventData();
removeListener(fabric.document, 'mouseup', this._onMouseUp);
var canvasElement = this.upperCanvasEl,
eventTypePrefix = this.enablePointerEvents ? 'pointer' : 'mouse';

removeListener(fabric.document, eventTypePrefix + 'up', this._onMouseUp);
removeListener(fabric.document, 'touchend', this._onMouseUp, addEventOptions);

removeListener(fabric.document, 'mousemove', this._onMouseMove, addEventOptions);
removeListener(fabric.document, eventTypePrefix + 'move', this._onMouseMove, addEventOptions);
removeListener(fabric.document, 'touchmove', this._onMouseMove, addEventOptions);

addListener(this.upperCanvasEl, 'mousemove', this._onMouseMove, addEventOptions);
addListener(this.upperCanvasEl, 'touchmove', this._onMouseMove, addEventOptions);
addListener(canvasElement, eventTypePrefix + 'move', this._onMouseMove, addEventOptions);
addListener(canvasElement, 'touchmove', this._onMouseMove, addEventOptions);

if (e.type === 'touchend') {
// Wait 400ms before rebinding mousedown to prevent double triggers
// from touch devices
var _this = this;
setTimeout(function() {
addListener(_this.upperCanvasEl, 'mousedown', _this._onMouseDown);
addListener(_this.upperCanvasEl, eventTypePrefix + 'down', _this._onMouseDown);
}, 400);
}
},
Expand Down