now removing color key from shapes hash when a shape is removed from the stage

This commit is contained in:
Eric Rowell 2012-08-26 18:25:51 -07:00
parent 89611aed5f
commit c01c08d557
7 changed files with 64 additions and 48 deletions

41
dist/kinetic-core.js vendored
View File

@ -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();
}
}
@ -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
@ -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();
@ -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();
@ -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();

File diff suppressed because one or more lines are too long

View File

@ -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();
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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
@ -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();

View File

@ -1,6 +1,6 @@
Test.prototype.tests = {
////////////////////////////////////////////////////////////////////////
// STAGE tests
// STAGE testsf
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
@ -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({