improved OO design for remove() and add()

This commit is contained in:
ericdrowell
2012-09-26 19:26:53 -07:00
parent 0a8f0ddb74
commit f235ea032d
7 changed files with 23 additions and 31 deletions

22
dist/kinetic-core.js vendored
View File

@@ -1411,11 +1411,6 @@ Kinetic.Node.prototype = {
while(this.children && this.children.length > 0) { while(this.children && this.children.length > 0) {
this.children[0].remove(); this.children[0].remove();
} }
// do extra stuff if needed
if(this._remove !== undefined) {
this._remove();
}
} }
}, },
/** /**
@@ -2510,11 +2505,6 @@ Kinetic.Container.prototype = {
go._pullNodes(stage); go._pullNodes(stage);
} }
// do extra stuff if needed
if(this._add !== undefined) {
this._add(child);
}
// chainable // chainable
return this; return this;
}, },
@@ -3063,13 +3053,17 @@ Kinetic.Stage.prototype = {
* add layer to stage * add layer to stage
* @param {Layer} layer * @param {Layer} layer
*/ */
_add: function(layer) { add: function(layer) {
Kinetic.Container.prototype.add.call(this, layer);
layer.canvas.setSize(this.attrs.width, this.attrs.height); layer.canvas.setSize(this.attrs.width, this.attrs.height);
layer.bufferCanvas.setSize(this.attrs.width, this.attrs.height); layer.bufferCanvas.setSize(this.attrs.width, this.attrs.height);
// draw layer and append canvas to container // draw layer and append canvas to container
layer.draw(); layer.draw();
this.content.appendChild(layer.canvas.element); this.content.appendChild(layer.canvas.element);
// chainable
return this;
}, },
_setUserPosition: function(evt) { _setUserPosition: function(evt) {
if(!evt) { if(!evt) {
@@ -3690,7 +3684,8 @@ Kinetic.Layer.prototype = {
/** /**
* remove layer from stage * remove layer from stage
*/ */
_remove: function() { remove: function() {
Kinetic.Node.prototype.remove.call(this);
/* /*
* remove canvas DOM from the document if * remove canvas DOM from the document if
* it exists * it exists
@@ -4196,7 +4191,8 @@ Kinetic.Shape.prototype = {
var p = bufferCanvas.context.getImageData(Math.round(pos.x), Math.round(pos.y), 1, 1).data; var p = bufferCanvas.context.getImageData(Math.round(pos.x), Math.round(pos.y), 1, 1).data;
return p[3] > 0; return p[3] > 0;
}, },
_remove: function() { remove: function() {
Kinetic.Node.prototype.remove.call(this);
delete Kinetic.Global.shapes[this.colorKey]; delete Kinetic.Global.shapes[this.colorKey];
}, },
__draw: function(canvas) { __draw: function(canvas) {

File diff suppressed because one or more lines are too long

View File

@@ -86,11 +86,6 @@ Kinetic.Container.prototype = {
go._pullNodes(stage); go._pullNodes(stage);
} }
// do extra stuff if needed
if(this._add !== undefined) {
this._add(child);
}
// chainable // chainable
return this; return this;
}, },

View File

@@ -229,7 +229,8 @@ Kinetic.Layer.prototype = {
/** /**
* remove layer from stage * remove layer from stage
*/ */
_remove: function() { remove: function() {
Kinetic.Node.prototype.remove.call(this);
/* /*
* remove canvas DOM from the document if * remove canvas DOM from the document if
* it exists * it exists

View File

@@ -188,11 +188,6 @@ Kinetic.Node.prototype = {
while(this.children && this.children.length > 0) { while(this.children && this.children.length > 0) {
this.children[0].remove(); this.children[0].remove();
} }
// do extra stuff if needed
if(this._remove !== undefined) {
this._remove();
}
} }
}, },
/** /**

View File

@@ -422,7 +422,8 @@ Kinetic.Shape.prototype = {
var p = bufferCanvas.context.getImageData(Math.round(pos.x), Math.round(pos.y), 1, 1).data; var p = bufferCanvas.context.getImageData(Math.round(pos.x), Math.round(pos.y), 1, 1).data;
return p[3] > 0; return p[3] > 0;
}, },
_remove: function() { remove: function() {
Kinetic.Node.prototype.remove.call(this);
delete Kinetic.Global.shapes[this.colorKey]; delete Kinetic.Global.shapes[this.colorKey];
}, },
__draw: function(canvas) { __draw: function(canvas) {

View File

@@ -418,13 +418,17 @@ Kinetic.Stage.prototype = {
* add layer to stage * add layer to stage
* @param {Layer} layer * @param {Layer} layer
*/ */
_add: function(layer) { add: function(layer) {
Kinetic.Container.prototype.add.call(this, layer);
layer.canvas.setSize(this.attrs.width, this.attrs.height); layer.canvas.setSize(this.attrs.width, this.attrs.height);
layer.bufferCanvas.setSize(this.attrs.width, this.attrs.height); layer.bufferCanvas.setSize(this.attrs.width, this.attrs.height);
// draw layer and append canvas to container // draw layer and append canvas to container
layer.draw(); layer.draw();
this.content.appendChild(layer.canvas.element); this.content.appendChild(layer.canvas.element);
// chainable
return this;
}, },
_setUserPosition: function(evt) { _setUserPosition: function(evt) {
if(!evt) { if(!evt) {