Faster removeChildren and destroyChildren methods

This commit is contained in:
Лаврёнов Антон 2014-03-02 08:21:11 +08:00
parent 74060ce935
commit 3a5b6eb766
3 changed files with 20 additions and 69 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ node_modules
bower_components
phantomjs.exe
documentation
test/sandbox.html
# Numerous always-ignore extensions
*.diff

View File

@ -27,17 +27,20 @@
* @memberof Kinetic.Container.prototype
*/
removeChildren: function() {
var children = this.children,
child;
while(children.length > 0) {
child = children[0];
var children = Kinetic.Collection.toCollection(this.children);
var child;
for (var i = 0; i < children.length; i++) {
child = children[i];
// reset parent to prevent many _setChildrenIndices calls
delete child.parent;
child.index = 0;
if (child.hasChildren()) {
child.removeChildren();
}
child.remove();
}
children = null;
this.children = new Kinetic.Collection();
return this;
},
/**
@ -46,10 +49,17 @@
* @memberof Kinetic.Container.prototype
*/
destroyChildren: function() {
var children = this.children;
while(children.length > 0) {
children[0].destroy();
var children = Kinetic.Collection.toCollection(this.children);
var child;
for (var i = 0; i < children.length; i++) {
child = children[i];
// reset parent to prevent many _setChildrenIndices calls
delete child.parent;
child.index = 0;
child.destroy();
}
children = null;
this.children = new Kinetic.Collection();
return this;
},
/**

View File

@ -1,60 +0,0 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>KineticJS Mocha Tests</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
<style>
#mocha .test {
overflow: auto;
}
</style>
</head>
<body>
<div id="mocha"></div>
<!-- used for KineticJS container -->
<div id="container"></div>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../dist/kinetic-dev.js"></script>
<script>
Kinetic.enableTrace = true;
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var text = new Kinetic.Text({
x: 10,
y: 10,
//stroke: '#555',
//strokeWidth: 5,
text: 'HEADING\n\nAll the world\'s a stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts.',
//text: 'HEADING\n\nThis is a really cool paragraph. \n And this is a footer.',
fontSize: 16,
fontFamily: 'Calibri',
fontStyle: 'normal',
fill: '#555',
//width: 20,
width: 380,
//width: 200,
padding: 20,
align: 'center',
shadowColor: 'red',
shadowBlur: 1,
shadowOffset: [10, 10],
shadowOpacity: 0.5,
draggable: true
});
layer.add(text);
stage.add(layer);
console.log(layer.getContext().getTrace());
</script>
</body>
</html>