greatly improved the OO design for draw logic. removed unecessary _draw() and __draw() methods

This commit is contained in:
ericdrowell
2012-10-07 19:14:14 -07:00
parent f1bc2fe4e8
commit 432533ae4a
8 changed files with 203 additions and 170 deletions

100
dist/kinetic-core.js vendored
View File

@@ -3,7 +3,7 @@
* http://www.kineticjs.com/
* Copyright 2012, Eric Rowell
* Licensed under the MIT or GPL Version 2 licenses.
* Date: Oct 06 2012
* Date: Oct 07 2012
*
* Copyright (C) 2011 - 2012 by Eric Rowell
*
@@ -1742,7 +1742,7 @@ Kinetic.Node.prototype = {
*/
getAbsoluteOpacity: function() {
var absOpacity = this.getOpacity();
if (this.getParent()) {
if(this.getParent()) {
absOpacity *= this.getParent().getAbsoluteOpacity();
}
return absOpacity;
@@ -1989,16 +1989,17 @@ Kinetic.Node.prototype = {
var mimeType = config && config.mimeType ? config.mimeType : null;
var quality = config && config.quality ? config.quality : null;
var canvas;
//if width and height are defined, create new canvas to draw on, else reuse stage buffer canvas
if(config && config.width && config.height) {
canvas = new Kinetic.Canvas(config.width, config.height);
}
else {
canvas = this.getStage().bufferCanvas;
canvas.clear();
}
var context = canvas.getContext();
canvas.clear();
this._draw(canvas);
this.draw(canvas);
return canvas.toDataURL(mimeType, quality);
},
/**
@@ -2225,25 +2226,8 @@ Kinetic.Node.prototype = {
}
}
},
_draw: function(canvas) {
if(this.isVisible() && (!canvas || canvas.name !== 'buffer' || this.getListening())) {
if(this.__draw) {
this.__draw(canvas);
}
var children = this.children;
if(children) {
for(var n = 0; n < children.length; n++) {
var child = children[n];
if(child.draw) {
child.draw(canvas);
}
else {
child._draw(canvas);
}
}
}
}
_shouldDraw: function(canvas) {
return (this.isVisible() && (!canvas || canvas.name !== 'buffer' || this.getListening()));
}
};
@@ -2309,12 +2293,12 @@ Kinetic.Node._createNode = function(obj, container) {
else {
type = obj.nodeType;
}
// if container was passed in, add it to attrs
if (container) {
obj.attrs.container = container;
if(container) {
obj.attrs.container = container;
}
var no = new Kinetic[type](obj.attrs);
if(obj.children) {
for(var n = 0; n < obj.children.length; n++) {
@@ -2491,7 +2475,6 @@ Kinetic.Node.prototype.isDraggable = Kinetic.Node.prototype.getDraggable;
* @name getListening
* @methodOf Kinetic.Node.prototype
*/
///////////////////////////////////////////////////////////////////////
// Container
///////////////////////////////////////////////////////////////////////
@@ -2597,13 +2580,14 @@ Kinetic.Container.prototype = {
var collection = new Kinetic.Collection();
// ID selector
if(selector.charAt(0) === '#') {
var node = this._getNodeById(selector.slice(1));
if (node) collection.push(node);
var node = this._getNodeById(selector.slice(1));
if(node)
collection.push(node);
}
// name selector
else if(selector.charAt(0) === '.') {
var nodeList = this._getNodesByName(selector.slice(1));
Kinetic.Collection.apply(collection, nodeList);
var nodeList = this._getNodesByName(selector.slice(1));
Kinetic.Collection.apply(collection, nodeList);
}
// unrecognized selector, pass to children
else {
@@ -2625,7 +2609,7 @@ Kinetic.Container.prototype = {
},
_getNodesByName: function(key) {
var arr = this.getStage().names[key] || [];
return this._getDescendants(arr);
return this._getDescendants(arr);
},
_get: function(selector) {
var retArr = Kinetic.Node.prototype._get.call(this, selector);
@@ -2650,7 +2634,7 @@ Kinetic.Container.prototype = {
return obj;
},
_getDescendants: function(arr) {
var retArr = [];
var retArr = [];
for(var n = 0; n < arr.length; n++) {
var node = arr[n];
if(this.isAncestorOf(node)) {
@@ -2721,6 +2705,15 @@ Kinetic.Container.prototype = {
for(var n = 0; n < this.children.length; n++) {
this.children[n].index = n;
}
},
draw: function(canvas) {
if(Kinetic.Node.prototype._shouldDraw.call(this, canvas)) {
var children = this.children;
var length = children.length;
for(var n = 0; n < length; n++) {
children[n].draw(canvas);
}
}
}
};
Kinetic.Global.extend(Kinetic.Container, Kinetic.Node);
@@ -2790,13 +2783,11 @@ Kinetic.Stage.prototype = {
this.setAttr('container', container);
},
/**
* draw children
* draw layers
* @name draw
* @methodOf Kinetic.Stage.prototype
*/
draw: function() {
this._draw();
},
/**
* set stage size
* @name setSize
@@ -3522,12 +3513,24 @@ Kinetic.Layer.prototype = {
this.beforeDrawFunc.call(this);
}
var canvases = [];
if(canvas) {
this._draw(canvas);
canvases.push(canvas);
}
else {
this._draw(this.getCanvas());
this._draw(this.bufferCanvas);
canvases.push(this.getCanvas());
canvases.push(this.bufferCanvas);
}
var length = canvases.length;
for(var n = 0; n < length; n++) {
var canvas = canvases[n];
if(Kinetic.Node.prototype._shouldDraw.call(this, canvas)) {
if(this.attrs.clearBeforeDraw) {
canvas.clear();
}
Kinetic.Container.prototype.draw.call(this, canvas);
}
}
// after draw handler
@@ -3672,7 +3675,7 @@ Kinetic.Layer.prototype = {
* specified, then "image/png" will result. For "image/jpeg", specify a quality
* level as quality (range 0.0 - 1.0). Note that this method works
* differently from toDataURL() for other nodes because it generates an absolute dataURL
* based on what's draw on the layer, rather than drawing
* based on what's currently drawn on the layer, rather than drawing
* the current state of each child node
* @name toDataURL
* @methodOf Kinetic.Layer.prototype
@@ -3712,11 +3715,6 @@ Kinetic.Layer.prototype = {
} catch(e) {
Kinetic.Global.warn('unable to remove layer scene canvas element from the document');
}
},
__draw: function(canvas) {
if(this.attrs.clearBeforeDraw) {
canvas.clear();
}
}
};
Kinetic.Global.extend(Kinetic.Layer, Kinetic.Container);
@@ -4218,7 +4216,7 @@ Kinetic.Shape.prototype = {
var stage = this.getStage();
var bufferCanvas = stage.bufferCanvas;
bufferCanvas.clear();
this._draw(bufferCanvas);
this.draw(bufferCanvas);
var p = bufferCanvas.context.getImageData(Math.round(pos.x), Math.round(pos.y), 1, 1).data;
return p[3] > 0;
},
@@ -4226,8 +4224,8 @@ Kinetic.Shape.prototype = {
Kinetic.Node.prototype.remove.call(this);
delete Kinetic.Global.shapes[this.colorKey];
},
__draw: function(canvas) {
if(this.attrs.drawFunc) {
draw: function(canvas) {
if(this.attrs.drawFunc && Kinetic.Node.prototype._shouldDraw.call(this, canvas)) {
var stage = this.getStage();
var context = canvas.getContext();
var family = [];

File diff suppressed because one or more lines are too long