Skip to content

Commit

Permalink
Merge pull request #2141 from asturur/fixThis.Canvas
Browse files Browse the repository at this point in the history
Make .canvas property always present
  • Loading branch information
kangax committed Apr 22, 2015
2 parents 54211dd + bd2f99b commit 3586cb2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/shapes/group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
if (object) {
this._objects.push(object);
object.group = this;
object._set('canvas', this.canvas);
}
// since _restoreObjectsState set objects inactive
this.forEachObject(this._setObjectActive, this);
Expand Down Expand Up @@ -163,6 +164,7 @@
*/
_onObjectAdded: function(object) {
object.group = this;
object._set('canvas', this.canvas);
},

/**
Expand Down Expand Up @@ -194,7 +196,7 @@
* @private
*/
_set: function(key, value) {
if (key in this.delegatedProperties) {
if (key in this.delegatedProperties || key === 'canvas') {
var i = this._objects.length;
while (i--) {
this._objects[i].set(key, value);
Expand Down
2 changes: 1 addition & 1 deletion src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@
*/
_onObjectAdded: function(obj) {
this.stateful && obj.setupState();
obj.canvas = this;
obj._set('canvas', this);
obj.setCoords();
this.fire('object:added', { target: obj });
obj.fire('added');
Expand Down
19 changes: 19 additions & 0 deletions test/unit/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,25 @@ test('toObject without default values', function() {
equal(group.insertAt(rect1, 2), group, 'should be chainable');
});

test('canvas property propagation', function() {
var g1 = makeGroupWith4Objects(),
g2 = makeGroupWith4Objects(),
rect1 = new fabric.Rect(),
rect2 = new fabric.Rect(),
group1 = new fabric.Group([g1]);

group1.add(g2);
group1.insertAt(rect1, 0);
g2.insertAt(rect2, 0);

canvas.add(group1);
equal(g2.canvas, canvas);
equal(g2._objects[3].canvas, canvas);
equal(g1.canvas, canvas);
equal(g1._objects[3].canvas, canvas);
equal(rect2.canvas, canvas);
equal(rect1.canvas, canvas);
});
// asyncTest('cloning group with image', function() {
// var rect = new fabric.Rect({ top: 100, left: 100, width: 30, height: 10 }),
// img = new fabric.Image(_createImageElement()),
Expand Down

0 comments on commit 3586cb2

Please sign in to comment.