Skip to content

Commit

Permalink
now removing color key from shapes hash when a shape is removed from …
Browse files Browse the repository at this point in the history
…the stage
  • Loading branch information
ericdrowell committed Aug 27, 2012
1 parent 89611ae commit c01c08d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 48 deletions.
41 changes: 24 additions & 17 deletions dist/kinetic-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2538,8 +2538,8 @@ Kinetic.Container.prototype = {
}

// do extra stuff if needed
if(this._remove !== undefined) {
this._remove(child);
if(child._remove !== undefined) {
child._remove();
}
}

Expand Down Expand Up @@ -3061,21 +3061,6 @@ Kinetic.Stage.prototype = {
layer.draw();
}
},
/**
* remove layer from stage
* @param {Layer} layer
*/
_remove: function(layer) {
/*
* remove canvas DOM from the document if
* it exists
*/
try {
this.content.removeChild(layer.canvas.element);
}
catch(e) {
}
},
/**
* add layer to stage
* @param {Layer} layer
Expand Down Expand Up @@ -3469,6 +3454,10 @@ Kinetic.Stage.prototype = {
this.touchPos = undefined;
this.tapStart = false;

/*
* ids and names hash needs to be stored at the stage level to prevent
* id and name collisions between multiple stages in the document
*/
this.ids = {};
this.names = {};
this.dragAnim = new Kinetic.Animation();
Expand Down Expand Up @@ -3676,6 +3665,21 @@ Kinetic.Layer.prototype = {
}
return canvas.toDataURL(mimeType, quality);
},
/**
* remove layer from stage
*/
_remove: function() {
/*
* remove canvas DOM from the document if
* it exists
*/
try {
this.getStage().content.removeChild(this.canvas.element);
}
catch(e) {
Kinetic.Global.warn('unable to remove layer scene canvas element from the document');
}
},
__draw: function(canvas) {
if(this.attrs.clearBeforeDraw) {
canvas.clear();
Expand Down Expand Up @@ -4084,6 +4088,9 @@ Kinetic.Shape.prototype = {
var p = bufferCanvas.context.getImageData(Math.round(pos.x), Math.round(pos.y), 1, 1).data;
return p[3] > 0;
},
_remove: function() {
delete Kinetic.Global.shapes[this.colorKey];
},
__draw: function(canvas) {
if(this.attrs.drawFunc) {
var stage = this.getStage();
Expand Down
4 changes: 2 additions & 2 deletions dist/kinetic-core.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ Kinetic.Container.prototype = {
}

// do extra stuff if needed
if(this._remove !== undefined) {
this._remove(child);
if(child._remove !== undefined) {
child._remove();
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ Kinetic.Layer.prototype = {
}
return canvas.toDataURL(mimeType, quality);
},
/**
* remove layer from stage
*/
_remove: function() {
/*
* remove canvas DOM from the document if
* it exists
*/
try {
this.getStage().content.removeChild(this.canvas.element);
}
catch(e) {
Kinetic.Global.warn('unable to remove layer scene canvas element from the document');
}
},
__draw: function(canvas) {
if(this.attrs.clearBeforeDraw) {
canvas.clear();
Expand Down
3 changes: 3 additions & 0 deletions src/Shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ Kinetic.Shape.prototype = {
var p = bufferCanvas.context.getImageData(Math.round(pos.x), Math.round(pos.y), 1, 1).data;
return p[3] > 0;
},
_remove: function() {
delete Kinetic.Global.shapes[this.colorKey];
},
__draw: function(canvas) {
if(this.attrs.drawFunc) {
var stage = this.getStage();
Expand Down
19 changes: 4 additions & 15 deletions src/Stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,21 +402,6 @@ Kinetic.Stage.prototype = {
layer.draw();
}
},
/**
* remove layer from stage
* @param {Layer} layer
*/
_remove: function(layer) {
/*
* remove canvas DOM from the document if
* it exists
*/
try {
this.content.removeChild(layer.canvas.element);
}
catch(e) {
}
},
/**
* add layer to stage
* @param {Layer} layer
Expand Down Expand Up @@ -810,6 +795,10 @@ Kinetic.Stage.prototype = {
this.touchPos = undefined;
this.tapStart = false;

/*
* ids and names hash needs to be stored at the stage level to prevent
* id and name collisions between multiple stages in the document
*/
this.ids = {};
this.names = {};
this.dragAnim = new Kinetic.Animation();
Expand Down
26 changes: 14 additions & 12 deletions tests/js/unitTests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Test.prototype.tests = {
////////////////////////////////////////////////////////////////////////
// STAGE tests
// STAGE testsf
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -593,30 +593,32 @@ Test.prototype.tests = {
strokeWidth: 4,
name: 'myRect'
});


var circleColorKey = circle.colorKey;
var rectColorKey = rect.colorKey;

layer.add(circle);
layer.add(rect);
stage.add(layer);

var node = stage.get('#myCircle')[0];
var nodes = stage.get('.myRect');

test(stage.ids.myCircle._id === circle._id, 'circle not in ids hash');
test(stage.names.myRect[0]._id === rect._id, 'rect not in names hash');

var node = stage.get('#myCircle')[0];
var parent = node.getParent();

parent.remove(node);
test(Kinetic.Global.shapes[circleColorKey]._id === circle._id, 'circle color key should be in shapes hash');
test(Kinetic.Global.shapes[rectColorKey]._id === rect._id, 'rect color key should be in shapes hash');

layer.remove(circle);

test(stage.ids.myCircle === undefined, 'circle still in hash');
test(stage.names.myRect[0]._id === rect._id, 'rect not in names hash');
test(Kinetic.Global.shapes[circleColorKey] === undefined, 'circle color key should not be in shapes hash');
test(Kinetic.Global.shapes[rectColorKey]._id === rect._id, 'rect color key should be in shapes hash');

var parent = nodes[0].getParent();
parent.remove(nodes[0]);
layer.remove(rect);

test(stage.ids.myCircle === undefined, 'circle still in hash');
test(stage.names.myRect === undefined, 'rect still in hash');
test(Kinetic.Global.shapes[circleColorKey] === undefined, 'circle color key should not be in shapes hash');
test(Kinetic.Global.shapes[rectColorKey] === undefined, 'rect color key should not be in shapes hash');
},
'STAGE - test ids and names hashes': function(containerId) {
var stage = new Kinetic.Stage({
Expand Down

0 comments on commit c01c08d

Please sign in to comment.