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

RenderAll() changes #2545

Merged
merged 2 commits into from
Oct 12, 2015
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
15 changes: 6 additions & 9 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@

target.hasBorders = target.transparentCorners = false;

this._draw(this.contextCache, target);
target.render(this.contextCache);
target._renderControls(this.contextCache);

target.hasBorders = hasBorders;
target.transparentCorners = transparentCorners;
Expand Down Expand Up @@ -1247,20 +1248,16 @@
* @param {CanvasRenderingContext2D} ctx Context to render controls on
*/
drawControls: function(ctx) {
ctx.save();
ctx.setTransform.apply(ctx, [1, 0, 0, 1, 0, 0]);
var activeGroup = this.getActiveGroup();
if (activeGroup) {
this._drawGroupControls(ctx, activeGroup);
activeGroup._renderControls(ctx);
}
else {
this._drawObjectsControls(ctx);
}
},

/**
* @private
*/
_drawGroupControls: function(ctx, activeGroup) {
activeGroup._renderControls(ctx);
ctx.restore();
},

/**
Expand Down
152 changes: 59 additions & 93 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -869,9 +865,16 @@
fabric.util.clipContext(this, canvasToDrawOn);
}

objsToRender = this._chooseObjectsToRender();
Copy link
Member Author

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.

Copy link
Member Author

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.


//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();
Expand All @@ -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'];
Copy link
Member Author

Choose a reason for hiding this comment

The 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);
}
},

Expand All @@ -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');
},

/**
Expand Down