-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
RenderAll() changes #2545
Merged
Merged
RenderAll() changes #2545
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -748,36 +748,6 @@ | |
return null; | ||
}, | ||
|
||
/** | ||
* Given a context, renders an object on that context | ||
* @param {CanvasRenderingContext2D} ctx Context to render object on | ||
* @param {fabric.Object} object Object to render | ||
* @private | ||
*/ | ||
_draw: function (ctx, object) { | ||
if (!object) { | ||
return; | ||
} | ||
|
||
ctx.save(); | ||
var v = this.viewportTransform; | ||
ctx.transform(v[0], v[1], v[2], v[3], v[4], v[5]); | ||
if (this._shouldRenderObject(object)) { | ||
object.render(ctx); | ||
} | ||
ctx.restore(); | ||
if (!this.controlsAboveOverlay) { | ||
object._renderControls(ctx); | ||
} | ||
}, | ||
|
||
_shouldRenderObject: function(object) { | ||
if (!object) { | ||
return false; | ||
} | ||
return (object !== this.getActiveGroup() || !this.preserveObjectStacking); | ||
}, | ||
|
||
/** | ||
* @private | ||
* @param {fabric.Object} obj Object that was added | ||
|
@@ -847,15 +817,41 @@ | |
return this; | ||
}, | ||
|
||
/** | ||
* Divides objects in two groups, one to render immediately | ||
* and one to render as activeGroup. | ||
* return objects to render immediately and pushes the other in the activeGroup. | ||
*/ | ||
_chooseObjectsToRender: function() { | ||
var activeGroup = this.getActiveGroup(), | ||
object, objsToRender = [ ], activeGroupObjects = [ ]; | ||
|
||
if (activeGroup && !this.preserveObjectStacking) { | ||
for (var i = 0, length = this._objects.length; i < length; i++) { | ||
object = this._objects[i]; | ||
if (!activeGroup.contains(object)) { | ||
objsToRender.push(object); | ||
} | ||
else { | ||
activeGroupObjects.push(object); | ||
} | ||
} | ||
activeGroup._set('_objects', activeGroupObjects); | ||
} | ||
else { | ||
objsToRender = this._objects; | ||
} | ||
return objsToRender; | ||
}, | ||
|
||
/** | ||
* Renders both the top canvas and the secondary container canvas. | ||
* @param {Boolean} [allOnTop] Whether we want to force all images to be rendered on the top canvas | ||
* @return {fabric.Canvas} instance | ||
* @chainable | ||
*/ | ||
renderAll: function () { | ||
var canvasToDrawOn = this.contextContainer, | ||
activeGroup = this.getActiveGroup(); | ||
var canvasToDrawOn = this.contextContainer, objsToRender; | ||
|
||
if (this.contextTop && this.selection && !this._groupSelector) { | ||
this.clearContext(this.contextTop); | ||
|
@@ -869,9 +865,16 @@ | |
fabric.util.clipContext(this, canvasToDrawOn); | ||
} | ||
|
||
objsToRender = this._chooseObjectsToRender(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following your advice i grouped the big block of code and gave it a clear ( i hope ) name. |
||
|
||
//apply viewport transform once for all rendering process | ||
canvasToDrawOn.setTransform.apply(canvasToDrawOn, this.viewportTransform); | ||
this._renderBackground(canvasToDrawOn); | ||
this._renderObjects(canvasToDrawOn, activeGroup); | ||
this._renderActiveGroup(canvasToDrawOn, activeGroup); | ||
this._renderObjects(canvasToDrawOn, objsToRender); | ||
this.preserveObjectStacking || this._renderObjects(canvasToDrawOn, [this.getActiveGroup()]); | ||
if (!this.controlsAboveOverlay && this.interactive) { | ||
this.drawControls(canvasToDrawOn); | ||
} | ||
|
||
if (this.clipTo) { | ||
canvasToDrawOn.restore(); | ||
|
@@ -891,46 +894,35 @@ | |
/** | ||
* @private | ||
* @param {CanvasRenderingContext2D} ctx Context to render on | ||
* @param {fabric.Group} activeGroup | ||
* @param {Array} objects to render | ||
*/ | ||
_renderObjects: function(ctx, activeGroup) { | ||
var i, length; | ||
|
||
// fast path | ||
if (!activeGroup || this.preserveObjectStacking) { | ||
for (i = 0, length = this._objects.length; i < length; ++i) { | ||
this._draw(ctx, this._objects[i]); | ||
} | ||
} | ||
else { | ||
for (i = 0, length = this._objects.length; i < length; ++i) { | ||
if (this._objects[i] && !activeGroup.contains(this._objects[i])) { | ||
this._draw(ctx, this._objects[i]); | ||
} | ||
} | ||
_renderObjects: function(ctx, objects) { | ||
for (var i = 0, length = objects.length; i < length; ++i) { | ||
objects[i] && objects[i].render(ctx); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
* @param {CanvasRenderingContext2D} ctx Context to render on | ||
* @param {fabric.Group} activeGroup | ||
* @param {string} property 'background' or 'overlay' | ||
*/ | ||
_renderActiveGroup: function(ctx, activeGroup) { | ||
_renderBackgroundOrOverlay: function(ctx, property) { | ||
var object = this[property + 'Color']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a temp variable do reduce accessing object properties. |
||
if (object) { | ||
ctx.fillStyle = object.toLive | ||
? object.toLive(ctx) | ||
: object; | ||
|
||
// delegate rendering to group selection (if one exists) | ||
if (activeGroup) { | ||
|
||
//Store objects in group preserving order, then replace | ||
var sortedObjects = []; | ||
this.forEachObject(function (object) { | ||
if (activeGroup.contains(object)) { | ||
sortedObjects.push(object); | ||
} | ||
}); | ||
// forEachObject reverses the object, so we reverse again | ||
activeGroup._set('_objects', sortedObjects.reverse()); | ||
this._draw(ctx, activeGroup); | ||
ctx.fillRect( | ||
object.offsetX || 0, | ||
object.offsetY || 0, | ||
this.width, | ||
this.height); | ||
} | ||
object = this[property + 'Image']; | ||
if (object) { | ||
object.render(ctx); | ||
} | ||
}, | ||
|
||
|
@@ -939,41 +931,15 @@ | |
* @param {CanvasRenderingContext2D} ctx Context to render on | ||
*/ | ||
_renderBackground: function(ctx) { | ||
if (this.backgroundColor) { | ||
ctx.fillStyle = this.backgroundColor.toLive | ||
? this.backgroundColor.toLive(ctx) | ||
: this.backgroundColor; | ||
|
||
ctx.fillRect( | ||
this.backgroundColor.offsetX || 0, | ||
this.backgroundColor.offsetY || 0, | ||
this.width, | ||
this.height); | ||
} | ||
if (this.backgroundImage) { | ||
this._draw(ctx, this.backgroundImage); | ||
} | ||
this._renderBackgroundOrOverlay(ctx, 'background'); | ||
}, | ||
|
||
/** | ||
* @private | ||
* @param {CanvasRenderingContext2D} ctx Context to render on | ||
*/ | ||
_renderOverlay: function(ctx) { | ||
if (this.overlayColor) { | ||
ctx.fillStyle = this.overlayColor.toLive | ||
? this.overlayColor.toLive(ctx) | ||
: this.overlayColor; | ||
|
||
ctx.fillRect( | ||
this.overlayColor.offsetX || 0, | ||
this.overlayColor.offsetY || 0, | ||
this.width, | ||
this.height); | ||
} | ||
if (this.overlayImage) { | ||
this._draw(ctx, this.overlayImage); | ||
} | ||
this._renderBackgroundOrOverlay(ctx, 'overlay'); | ||
}, | ||
|
||
/** | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following your advice i grouped the big block of code and gave it a clear ( i hope ) name.