Skip to content

Commit

Permalink
Enable request animationFrame for mouse interaction (#3997)
Browse files Browse the repository at this point in the history
* switch to request animation frame

* added request animation frame

* fix
  • Loading branch information
asturur authored Jun 11, 2017
1 parent 145bd3b commit 5f57659
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/brushes/circle_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fabric.CircleBrush = fabric.util.createClass(fabric.BaseBrush, /** @lends fabric
this.canvas.clearContext(this.canvas.contextTop);
this._resetShadow();
this.canvas.renderOnAddRemove = originalRenderOnAddRemove;
this.canvas.renderAll();
this.canvas.requestRenderAll();
},

/**
Expand Down
4 changes: 2 additions & 2 deletions src/brushes/pencil_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
// rendered inconsistently across browsers
// Firefox 4, for example, renders a dot,
// whereas Chrome 10 renders nothing
this.canvas.renderAll();
this.canvas.requestRenderAll();
return;
}

Expand All @@ -208,7 +208,7 @@

this.canvas.clearContext(this.canvas.contextTop);
this._resetShadow();
this.canvas.renderAll();
this.canvas.requestRenderAll();

// fire event 'path' created
this.canvas.fire('path:created', { path: path });
Expand Down
2 changes: 1 addition & 1 deletion src/brushes/spray_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
this.canvas.clearContext(this.canvas.contextTop);
this._resetShadow();
this.canvas.renderOnAddRemove = originalRenderOnAddRemove;
this.canvas.renderAll();
this.canvas.requestRenderAll();
},

/**
Expand Down
4 changes: 2 additions & 2 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*/
initialize: function(el, options) {
options || (options = { });

this.renderAndResetBound = this.renderAndReset.bind(this);
this._initStatic(el, options);
this._initInteractive();
this._createCacheCanvas();
Expand Down Expand Up @@ -1396,7 +1396,7 @@
this._setActiveObject(object);
this.fire('object:selected', { target: object, e: e });
object.fire('selected', { e: e });
this.renderAll();
this.requestRenderAll();
return this;
},

Expand Down
6 changes: 3 additions & 3 deletions src/mixins/animation.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati
duration: this.FX_DURATION,
onChange: function(value) {
object.set('left', value);
_this.renderAll();
_this.requestRenderAll();
onChange();
},
onComplete: function() {
Expand Down Expand Up @@ -65,7 +65,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati
duration: this.FX_DURATION,
onChange: function(value) {
object.set('top', value);
_this.renderAll();
_this.requestRenderAll();
onChange();
},
onComplete: function() {
Expand Down Expand Up @@ -103,7 +103,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati
},
onChange: function(value) {
object.set('opacity', value);
_this.renderAll();
_this.requestRenderAll();
onChange();
},
onComplete: function () {
Expand Down
8 changes: 4 additions & 4 deletions src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
this._setCursorFromEvent(e, target);
this._handleEvent(e, 'up', target ? target : null, LEFT_CLICK, isClick);
target && (target.__corner = 0);
shouldRender && this.renderAll();
shouldRender && this.requestRenderAll();
},

/**
Expand Down Expand Up @@ -447,7 +447,7 @@
*/
_onMouseDownInDrawingMode: function(e) {
this._isCurrentlyDrawing = true;
this.discardActiveObject(e).renderAll();
this.discardActiveObject(e).requestRenderAll();
if (this.clipTo) {
fabric.util.clipContext(this, this.contextTop);
}
Expand Down Expand Up @@ -559,7 +559,7 @@
}
this._handleEvent(e, 'down', target ? target : null);
// we must renderAll so that we update the visuals
shouldRender && this.renderAll();
shouldRender && this.requestRenderAll();
},

/**
Expand Down Expand Up @@ -681,7 +681,7 @@
this._beforeScaleTransform(e, transform);
this._performTransformAction(e, transform, pointer);

transform.actionPerformed && this.renderAll();
transform.actionPerformed && this.requestRenderAll();
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/mixins/canvas_gestures.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

this._setCenterToOrigin(t.target);

this.renderAll();
this.requestRenderAll();
t.action = 'drag';
},

Expand Down
2 changes: 1 addition & 1 deletion src/mixins/canvas_grouping.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
group.addWithUpdate();
this.setActiveGroup(group, e);
this.fire('selection:created', { target: group, e: e });
this.renderAll();
this.requestRenderAll();
}
},

Expand Down
2 changes: 1 addition & 1 deletion src/mixins/itext_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@
}
this.canvas.fire('text:editing:entered', { target: this });
this.initMouseMoveHandler();
this.canvas.renderAll();
this.canvas.requestRenderAll();
return this;
},

Expand Down
10 changes: 5 additions & 5 deletions src/mixins/itext_key_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
this.renderCursorOrSelection();
}
else {
this.canvas && this.canvas.renderAll();
this.canvas && this.canvas.requestRenderAll();
}
},

Expand All @@ -116,7 +116,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
}
e.stopImmediatePropagation();
e.preventDefault();
this.canvas && this.canvas.renderAll();
this.canvas && this.canvas.requestRenderAll();
},

/**
Expand All @@ -143,7 +143,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
this.fire('changed');
if (this.canvas) {
this.canvas.fire('text:changed', { target: this });
this.canvas.renderAll();
this.canvas.requestRenderAll();
}
}

Expand Down Expand Up @@ -184,7 +184,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
this.fire('changed');
if (this.canvas) {
this.canvas.fire('text:changed', { target: this });
this.canvas.renderAll();
this.canvas.requestRenderAll();
}
},
/**
Expand Down Expand Up @@ -592,7 +592,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot

this._removeExtraneousStyles();

this.canvas && this.canvas.renderAll();
this.canvas && this.canvas.requestRenderAll();

this.setCoords();
this.fire('changed');
Expand Down
33 changes: 30 additions & 3 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
initialize: function(el, options) {
options || (options = { });

this.renderAndResetBound = this.renderAndReset.bind(this);
this._initStatic(el, options);
},

Expand Down Expand Up @@ -818,6 +818,31 @@
return this;
},

/**
* Function created to be instance bound at initialization
* used in requestAnimationFrame rendering
* @return {fabric.Canvas} instance
* @chainable
*/
renderAndReset: function() {
this.renderAll();
this.isRendering = false;
},

/**
* Append a renderAll request to next animation frame.
* a boolean flag will avoid appending more.
* @return {fabric.Canvas} instance
* @chainable
*/
requestRenderAll: function () {
if (!this.isRendering) {
this.isRendering = true;
fabric.util.requestAnimFrame(this.renderAndResetBound);
}
return this;
},

/**
* Calculate the position of the 4 corner of canvas with current viewportTransform.
* helps to determinate when an object is in the current rendering viewport using
Expand Down Expand Up @@ -1424,7 +1449,8 @@
removeFromArray(this._objects, object);
this._objects.unshift(object);
}
return this.renderAll && this.renderAll();
this.renderAll && this.renderAll();
return this;
},

/**
Expand Down Expand Up @@ -1452,7 +1478,8 @@
removeFromArray(this._objects, object);
this._objects.push(object);
}
return this.renderAll && this.renderAll();
this.renderAll && this.renderAll();
return this;
},

/**
Expand Down

0 comments on commit 5f57659

Please sign in to comment.