introduced new setDefaultAttrs() method which greatly simplifies the logic required inside node constructors. This will also make plugin creation much easier

This commit is contained in:
Eric Rowell
2012-04-28 12:55:18 -07:00
parent c661cff85a
commit 8dce92c2fd
13 changed files with 194 additions and 202 deletions

176
dist/kinetic-core.js vendored
View File

@@ -178,31 +178,28 @@ window.requestAnimFrame = (function(callback) {
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Node = function(config) { Kinetic.Node = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { visible: true,
this.attrs = {}; listening: true,
} name: undefined,
this.attrs.visible = true; alpha: 1,
this.attrs.listening = true; x: 0,
this.attrs.name = undefined; y: 0,
this.attrs.alpha = 1; scale: {
this.attrs.x = 0;
this.attrs.y = 0;
this.attrs.scale = {
x: 1, x: 1,
y: 1 y: 1
}; },
this.attrs.rotation = 0; rotation: 0,
this.attrs.centerOffset = { centerOffset: {
x: 0, x: 0,
y: 0 y: 0
}; },
this.attrs.dragConstraint = 'none'; dragConstraint: 'none',
this.attrs.dragBounds = {}; dragBounds: {},
this.attrs.draggable = false; draggable: false
this.eventListeners = {}; });
// set attrs this.eventListeners = {};
this.setAttrs(config); this.setAttrs(config);
}; };
/* /*
@@ -284,6 +281,23 @@ Kinetic.Node.prototype = {
getAttrs: function() { getAttrs: function() {
return this.attrs; return this.attrs;
}, },
/**
* set default attrs
* @param {Object} confic
*/
setDefaultAttrs: function(config) {
// create attrs object if undefined
if(this.attrs === undefined) {
this.attrs = {};
}
if(config) {
for(var key in config) {
var val = config[key];
this.attrs[key] = config[key];
}
}
},
/** /**
* set attrs * set attrs
* @param {Object} config * @param {Object} config
@@ -1213,12 +1227,11 @@ Kinetic.Container.prototype = {
* @param {int} height * @param {int} height
*/ */
Kinetic.Stage = function(config) { Kinetic.Stage = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { width: 400,
this.attrs = {}; height: 200
} });
this.attrs.width = 400;
this.attrs.height = 200;
this.nodeType = 'Stage'; this.nodeType = 'Stage';
this.ids = {}; this.ids = {};
this.names = {}; this.names = {};
@@ -2089,11 +2102,9 @@ Kinetic.GlobalObject.extend(Kinetic.Stage, Kinetic.Node);
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Layer = function(config) { Kinetic.Layer = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { throttle: 12
this.attrs = {}; });
}
this.attrs.throttle = 12;
this.nodeType = 'Layer'; this.nodeType = 'Layer';
this.lastDrawTime = 0; this.lastDrawTime = 0;
@@ -2258,15 +2269,13 @@ Kinetic.GlobalObject.extend(Kinetic.Group, Kinetic.Node);
* The default is "path" because it performs better * The default is "path" because it performs better
*/ */
Kinetic.Shape = function(config) { Kinetic.Shape = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { fill: undefined,
this.attrs = {}; stroke: undefined,
} strokeWidth: undefined,
this.attrs.fill = undefined; lineJoin: undefined,
this.attrs.stroke = undefined; detectionType: 'path'
this.attrs.strokeWidth = undefined; });
this.attrs.lineJoin = undefined;
this.attrs.detectionType = 'path';
this.data = []; this.data = [];
this.nodeType = 'Shape'; this.nodeType = 'Shape';
@@ -2488,13 +2497,11 @@ Kinetic.GlobalObject.extend(Kinetic.Shape, Kinetic.Node);
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Rect = function(config) { Kinetic.Rect = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { width: 0,
this.attrs = {}; height: 0,
} cornerRadius: 0
this.attrs.width = 0; });
this.attrs.height = 0;
this.attrs.cornerRadius = 0;
this.shapeType = "Rect"; this.shapeType = "Rect";
@@ -2600,11 +2607,10 @@ Kinetic.GlobalObject.extend(Kinetic.Rect, Kinetic.Shape);
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Circle = function(config) { Kinetic.Circle = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { radius: 0
this.attrs = {}; });
}
this.attrs.radius = 0;
this.shapeType = "Circle"; this.shapeType = "Circle";
config.drawFunc = function() { config.drawFunc = function() {
@@ -2651,16 +2657,14 @@ Kinetic.GlobalObject.extend(Kinetic.Circle, Kinetic.Shape);
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Image = function(config) { Kinetic.Image = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { crop: {
this.attrs = {};
}
this.attrs.crop = {
x: 0, x: 0,
y: 0, y: 0,
width: undefined, width: undefined,
height: undefined height: undefined
}; }
});
this.shapeType = "Image"; this.shapeType = "Image";
config.drawFunc = function() { config.drawFunc = function() {
@@ -2787,11 +2791,9 @@ Kinetic.GlobalObject.extend(Kinetic.Image, Kinetic.Shape);
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Polygon = function(config) { Kinetic.Polygon = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { points: {}
this.attrs = {}; });
}
this.attrs.points = {};
this.shapeType = "Polygon"; this.shapeType = "Polygon";
config.drawFunc = function() { config.drawFunc = function() {
@@ -2840,12 +2842,10 @@ Kinetic.GlobalObject.extend(Kinetic.Polygon, Kinetic.Shape);
* @param {Object} config * @param {Object} config
*/ */
Kinetic.RegularPolygon = function(config) { Kinetic.RegularPolygon = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { radius: 0,
this.attrs = {}; sides: 0
} });
this.attrs.radius = 0;
this.attrs.sides = 0;
this.shapeType = "RegularPolygon"; this.shapeType = "RegularPolygon";
config.drawFunc = function() { config.drawFunc = function() {
@@ -2910,13 +2910,11 @@ Kinetic.GlobalObject.extend(Kinetic.RegularPolygon, Kinetic.Shape);
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Star = function(config) { Kinetic.Star = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { points: [],
this.attrs = {}; innerRadius: 0,
} outerRadius: 0
this.attrs.points = []; });
this.attrs.innerRadius = 0;
this.attrs.outerRadius = 0;
this.shapeType = "Star"; this.shapeType = "Star";
config.drawFunc = function() { config.drawFunc = function() {
@@ -2994,20 +2992,18 @@ Kinetic.GlobalObject.extend(Kinetic.Star, Kinetic.Shape);
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Text = function(config) { Kinetic.Text = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { fontFamily: 'Calibri',
this.attrs = {}; text: '',
} fontSize: 12,
this.attrs.fontFamily = ''; fill: undefined,
this.attrs.text = ''; textStroke: undefined,
this.attrs.fontSize = 12; textStrokeWidth: undefined,
this.attrs.fill = undefined; align: 'left',
this.attrs.textStroke = undefined; verticalAlign: 'top',
this.attrs.textStrokeWidth = undefined; padding: 0,
this.attrs.align = 'left'; fontStyle: 'normal'
this.attrs.verticalAlign = 'top'; });
this.attrs.padding = 0;
this.attrs.fontStyle = 'normal';
this.shapeType = "Text"; this.shapeType = "Text";

File diff suppressed because one or more lines are too long

View File

@@ -10,11 +10,9 @@
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Layer = function(config) { Kinetic.Layer = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { throttle: 12
this.attrs = {}; });
}
this.attrs.throttle = 12;
this.nodeType = 'Layer'; this.nodeType = 'Layer';
this.lastDrawTime = 0; this.lastDrawTime = 0;

View File

@@ -9,31 +9,28 @@
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Node = function(config) { Kinetic.Node = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { visible: true,
this.attrs = {}; listening: true,
} name: undefined,
this.attrs.visible = true; alpha: 1,
this.attrs.listening = true; x: 0,
this.attrs.name = undefined; y: 0,
this.attrs.alpha = 1; scale: {
this.attrs.x = 0;
this.attrs.y = 0;
this.attrs.scale = {
x: 1, x: 1,
y: 1 y: 1
}; },
this.attrs.rotation = 0; rotation: 0,
this.attrs.centerOffset = { centerOffset: {
x: 0, x: 0,
y: 0 y: 0
}; },
this.attrs.dragConstraint = 'none'; dragConstraint: 'none',
this.attrs.dragBounds = {}; dragBounds: {},
this.attrs.draggable = false; draggable: false
this.eventListeners = {}; });
// set attrs this.eventListeners = {};
this.setAttrs(config); this.setAttrs(config);
}; };
/* /*
@@ -115,6 +112,23 @@ Kinetic.Node.prototype = {
getAttrs: function() { getAttrs: function() {
return this.attrs; return this.attrs;
}, },
/**
* set default attrs
* @param {Object} confic
*/
setDefaultAttrs: function(config) {
// create attrs object if undefined
if(this.attrs === undefined) {
this.attrs = {};
}
if(config) {
for(var key in config) {
var val = config[key];
this.attrs[key] = config[key];
}
}
},
/** /**
* set attrs * set attrs
* @param {Object} config * @param {Object} config

View File

@@ -16,15 +16,13 @@
* The default is "path" because it performs better * The default is "path" because it performs better
*/ */
Kinetic.Shape = function(config) { Kinetic.Shape = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { fill: undefined,
this.attrs = {}; stroke: undefined,
} strokeWidth: undefined,
this.attrs.fill = undefined; lineJoin: undefined,
this.attrs.stroke = undefined; detectionType: 'path'
this.attrs.strokeWidth = undefined; });
this.attrs.lineJoin = undefined;
this.attrs.detectionType = 'path';
this.data = []; this.data = [];
this.nodeType = 'Shape'; this.nodeType = 'Shape';

View File

@@ -12,12 +12,11 @@
* @param {int} height * @param {int} height
*/ */
Kinetic.Stage = function(config) { Kinetic.Stage = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { width: 400,
this.attrs = {}; height: 200
} });
this.attrs.width = 400;
this.attrs.height = 200;
this.nodeType = 'Stage'; this.nodeType = 'Stage';
this.ids = {}; this.ids = {};
this.names = {}; this.names = {};

View File

@@ -8,11 +8,10 @@
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Circle = function(config) { Kinetic.Circle = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { radius: 0
this.attrs = {}; });
}
this.attrs.radius = 0;
this.shapeType = "Circle"; this.shapeType = "Circle";
config.drawFunc = function() { config.drawFunc = function() {

View File

@@ -8,16 +8,14 @@
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Image = function(config) { Kinetic.Image = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { crop: {
this.attrs = {};
}
this.attrs.crop = {
x: 0, x: 0,
y: 0, y: 0,
width: undefined, width: undefined,
height: undefined height: undefined
}; }
});
this.shapeType = "Image"; this.shapeType = "Image";
config.drawFunc = function() { config.drawFunc = function() {

View File

@@ -8,11 +8,9 @@
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Polygon = function(config) { Kinetic.Polygon = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { points: {}
this.attrs = {}; });
}
this.attrs.points = {};
this.shapeType = "Polygon"; this.shapeType = "Polygon";
config.drawFunc = function() { config.drawFunc = function() {

View File

@@ -8,13 +8,11 @@
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Rect = function(config) { Kinetic.Rect = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { width: 0,
this.attrs = {}; height: 0,
} cornerRadius: 0
this.attrs.width = 0; });
this.attrs.height = 0;
this.attrs.cornerRadius = 0;
this.shapeType = "Rect"; this.shapeType = "Rect";

View File

@@ -8,12 +8,10 @@
* @param {Object} config * @param {Object} config
*/ */
Kinetic.RegularPolygon = function(config) { Kinetic.RegularPolygon = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { radius: 0,
this.attrs = {}; sides: 0
} });
this.attrs.radius = 0;
this.attrs.sides = 0;
this.shapeType = "RegularPolygon"; this.shapeType = "RegularPolygon";
config.drawFunc = function() { config.drawFunc = function() {

View File

@@ -8,13 +8,11 @@
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Star = function(config) { Kinetic.Star = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { points: [],
this.attrs = {}; innerRadius: 0,
} outerRadius: 0
this.attrs.points = []; });
this.attrs.innerRadius = 0;
this.attrs.outerRadius = 0;
this.shapeType = "Star"; this.shapeType = "Star";
config.drawFunc = function() { config.drawFunc = function() {

View File

@@ -8,20 +8,18 @@
* @param {Object} config * @param {Object} config
*/ */
Kinetic.Text = function(config) { Kinetic.Text = function(config) {
// default attrs this.setDefaultAttrs({
if(this.attrs === undefined) { fontFamily: 'Calibri',
this.attrs = {}; text: '',
} fontSize: 12,
this.attrs.fontFamily = ''; fill: undefined,
this.attrs.text = ''; textStroke: undefined,
this.attrs.fontSize = 12; textStrokeWidth: undefined,
this.attrs.fill = undefined; align: 'left',
this.attrs.textStroke = undefined; verticalAlign: 'top',
this.attrs.textStrokeWidth = undefined; padding: 0,
this.attrs.align = 'left'; fontStyle: 'normal'
this.attrs.verticalAlign = 'top'; });
this.attrs.padding = 0;
this.attrs.fontStyle = 'normal';
this.shapeType = "Text"; this.shapeType = "Text";