2012-12-02 04:04:10 +08:00
|
|
|
(function() {
|
2013-03-25 04:12:33 +08:00
|
|
|
// CONSTANTS
|
|
|
|
var CANVAS = 'canvas',
|
|
|
|
CONTEXT_2D = '2d',
|
|
|
|
OBJECT_ARRAY = '[object Array]',
|
|
|
|
OBJECT_NUMBER = '[object Number]',
|
|
|
|
OBJECT_STRING = '[object String]',
|
|
|
|
PI_OVER_DEG180 = Math.PI / 180,
|
2013-04-30 14:12:28 +08:00
|
|
|
DEG180_OVER_PI = 180 / Math.PI,
|
|
|
|
HASH = '#',
|
|
|
|
RGB_PAREN = 'rgb(',
|
|
|
|
COLORS = {
|
|
|
|
aqua: [0,255,255],
|
|
|
|
lime: [0,255,0],
|
|
|
|
silver: [192,192,192],
|
|
|
|
black: [0,0,0],
|
|
|
|
maroon: [128,0,0],
|
|
|
|
teal: [0,128,128],
|
|
|
|
blue: [0,0,255],
|
|
|
|
navy: [0,0,128],
|
|
|
|
white: [255,255,255],
|
|
|
|
fuchsia: [255,0,255],
|
|
|
|
olive:[128,128,0],
|
|
|
|
yellow: [255,255,0],
|
|
|
|
orange: [255,165,0],
|
|
|
|
gray: [128,128,128],
|
|
|
|
purple: [128,0,128],
|
|
|
|
green: [0,128,0],
|
|
|
|
red: [255,0,0],
|
|
|
|
pink: [255,192,203],
|
|
|
|
cyan: [0,255,255],
|
|
|
|
transparent: [255,255,255,0]
|
|
|
|
},
|
|
|
|
|
|
|
|
RGB_REGEX = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/;
|
|
|
|
|
2012-04-29 08:45:13 +08:00
|
|
|
/*
|
2012-12-02 04:04:10 +08:00
|
|
|
* utilities that handle data type detection, conversion, and manipulation
|
2012-04-29 08:45:13 +08:00
|
|
|
*/
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Type = {
|
|
|
|
/*
|
|
|
|
* cherry-picked utilities from underscore.js
|
|
|
|
*/
|
|
|
|
_isElement: function(obj) {
|
|
|
|
return !!(obj && obj.nodeType == 1);
|
|
|
|
},
|
|
|
|
_isFunction: function(obj) {
|
|
|
|
return !!(obj && obj.constructor && obj.call && obj.apply);
|
|
|
|
},
|
|
|
|
_isObject: function(obj) {
|
|
|
|
return (!!obj && obj.constructor == Object);
|
|
|
|
},
|
|
|
|
_isArray: function(obj) {
|
2013-03-25 04:12:33 +08:00
|
|
|
return Object.prototype.toString.call(obj) == OBJECT_ARRAY;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
_isNumber: function(obj) {
|
2013-03-25 04:12:33 +08:00
|
|
|
return Object.prototype.toString.call(obj) == OBJECT_NUMBER;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
_isString: function(obj) {
|
2013-03-25 04:12:33 +08:00
|
|
|
return Object.prototype.toString.call(obj) == OBJECT_STRING;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* other utils
|
|
|
|
*/
|
|
|
|
_hasMethods: function(obj) {
|
2013-03-25 04:12:33 +08:00
|
|
|
var names = [],
|
|
|
|
key;
|
|
|
|
|
|
|
|
for(key in obj) {
|
|
|
|
if(this._isFunction(obj[key])) {
|
2012-12-02 04:04:10 +08:00
|
|
|
names.push(key);
|
2013-03-25 04:12:33 +08:00
|
|
|
}
|
2012-05-13 09:37:07 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
return names.length > 0;
|
|
|
|
},
|
2013-01-13 14:01:12 +08:00
|
|
|
_isInDocument: function(el) {
|
2013-03-25 04:12:33 +08:00
|
|
|
while(el = el.parentNode) {
|
2013-01-13 14:01:12 +08:00
|
|
|
if(el == document) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2012-12-02 04:04:10 +08:00
|
|
|
/*
|
|
|
|
* The argument can be:
|
|
|
|
* - an integer (will be applied to both x and y)
|
|
|
|
* - an array of one integer (will be applied to both x and y)
|
|
|
|
* - an array of two integers (contains x and y)
|
|
|
|
* - an array of four integers (contains x, y, width, and height)
|
|
|
|
* - an object with x and y properties
|
|
|
|
* - an array of one element which is an array of integers
|
|
|
|
* - an array of one element of an object
|
|
|
|
*/
|
|
|
|
_getXY: function(arg) {
|
|
|
|
if(this._isNumber(arg)) {
|
2012-05-20 12:14:04 +08:00
|
|
|
return {
|
2012-12-02 04:04:10 +08:00
|
|
|
x: arg,
|
|
|
|
y: arg
|
2012-05-20 12:14:04 +08:00
|
|
|
};
|
2012-05-13 09:37:07 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
else if(this._isArray(arg)) {
|
|
|
|
// if arg is an array of one element
|
|
|
|
if(arg.length === 1) {
|
|
|
|
var val = arg[0];
|
|
|
|
// if arg is an array of one element which is a number
|
|
|
|
if(this._isNumber(val)) {
|
2012-05-20 12:14:04 +08:00
|
|
|
return {
|
2012-12-02 04:04:10 +08:00
|
|
|
x: val,
|
|
|
|
y: val
|
2012-05-20 12:14:04 +08:00
|
|
|
};
|
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
// if arg is an array of one element which is an array
|
|
|
|
else if(this._isArray(val)) {
|
2012-05-20 12:14:04 +08:00
|
|
|
return {
|
2012-12-02 04:04:10 +08:00
|
|
|
x: val[0],
|
|
|
|
y: val[1]
|
2012-05-20 12:14:04 +08:00
|
|
|
};
|
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
// if arg is an array of one element which is an object
|
|
|
|
else if(this._isObject(val)) {
|
|
|
|
return val;
|
|
|
|
}
|
2012-05-20 12:14:04 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
// if arg is an array of two or more elements
|
|
|
|
else if(arg.length >= 2) {
|
|
|
|
return {
|
|
|
|
x: arg[0],
|
|
|
|
y: arg[1]
|
|
|
|
};
|
2012-05-20 12:14:04 +08:00
|
|
|
}
|
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
// if arg is an object return the object
|
|
|
|
else if(this._isObject(arg)) {
|
|
|
|
return arg;
|
2012-05-13 09:37:07 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
// default
|
2013-01-02 15:54:02 +08:00
|
|
|
return null;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* The argument can be:
|
|
|
|
* - an integer (will be applied to both width and height)
|
|
|
|
* - an array of one integer (will be applied to both width and height)
|
|
|
|
* - an array of two integers (contains width and height)
|
|
|
|
* - an array of four integers (contains x, y, width, and height)
|
|
|
|
* - an object with width and height properties
|
|
|
|
* - an array of one element which is an array of integers
|
|
|
|
* - an array of one element of an object
|
|
|
|
*/
|
|
|
|
_getSize: function(arg) {
|
|
|
|
if(this._isNumber(arg)) {
|
2012-05-20 12:14:04 +08:00
|
|
|
return {
|
2012-12-02 04:04:10 +08:00
|
|
|
width: arg,
|
|
|
|
height: arg
|
2012-05-20 12:14:04 +08:00
|
|
|
};
|
2012-05-13 09:37:07 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
else if(this._isArray(arg)) {
|
|
|
|
// if arg is an array of one element
|
|
|
|
if(arg.length === 1) {
|
|
|
|
var val = arg[0];
|
|
|
|
// if arg is an array of one element which is a number
|
|
|
|
if(this._isNumber(val)) {
|
|
|
|
return {
|
|
|
|
width: val,
|
|
|
|
height: val
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// if arg is an array of one element which is an array
|
|
|
|
else if(this._isArray(val)) {
|
|
|
|
/*
|
|
|
|
* if arg is an array of one element which is an
|
|
|
|
* array of four elements
|
|
|
|
*/
|
|
|
|
if(val.length >= 4) {
|
|
|
|
return {
|
|
|
|
width: val[2],
|
|
|
|
height: val[3]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* if arg is an array of one element which is an
|
|
|
|
* array of two elements
|
|
|
|
*/
|
|
|
|
else if(val.length >= 2) {
|
|
|
|
return {
|
|
|
|
width: val[0],
|
|
|
|
height: val[1]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if arg is an array of one element which is an object
|
|
|
|
else if(this._isObject(val)) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if arg is an array of four elements
|
|
|
|
else if(arg.length >= 4) {
|
|
|
|
return {
|
|
|
|
width: arg[2],
|
|
|
|
height: arg[3]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// if arg is an array of two elements
|
|
|
|
else if(arg.length >= 2) {
|
|
|
|
return {
|
|
|
|
width: arg[0],
|
|
|
|
height: arg[1]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if arg is an object return the object
|
|
|
|
else if(this._isObject(arg)) {
|
|
|
|
return arg;
|
|
|
|
}
|
2012-05-20 12:14:04 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
// default
|
2013-01-02 15:54:02 +08:00
|
|
|
return null;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* arg will be an array of numbers or
|
2013-01-09 16:21:47 +08:00
|
|
|
* an array of point arrays or
|
2012-12-02 04:04:10 +08:00
|
|
|
* an array of point objects
|
|
|
|
*/
|
|
|
|
_getPoints: function(arg) {
|
|
|
|
if(arg === undefined) {
|
|
|
|
return [];
|
2012-05-14 02:58:40 +08:00
|
|
|
}
|
2012-05-20 12:14:04 +08:00
|
|
|
|
2013-01-09 16:21:47 +08:00
|
|
|
// an array of arrays
|
|
|
|
if(this._isArray(arg[0])) {
|
|
|
|
/*
|
|
|
|
* convert array of arrays into an array
|
|
|
|
* of objects containing x, y
|
|
|
|
*/
|
|
|
|
var arr = [];
|
|
|
|
for(var n = 0; n < arg.length; n++) {
|
|
|
|
arr.push({
|
|
|
|
x: arg[n][0],
|
|
|
|
y: arg[n][1]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
// an array of objects
|
|
|
|
if(this._isObject(arg[0])) {
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
// an array of integers
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* convert array of numbers into an array
|
|
|
|
* of objects containing x, y
|
|
|
|
*/
|
|
|
|
var arr = [];
|
|
|
|
for(var n = 0; n < arg.length; n += 2) {
|
|
|
|
arr.push({
|
|
|
|
x: arg[n],
|
|
|
|
y: arg[n + 1]
|
|
|
|
});
|
|
|
|
}
|
2012-07-16 11:12:18 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* arg can be an image object or image data
|
|
|
|
*/
|
|
|
|
_getImage: function(arg, callback) {
|
2013-03-25 04:12:33 +08:00
|
|
|
var imageObj, canvas, context, dataUrl;
|
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
// if arg is null or undefined
|
|
|
|
if(!arg) {
|
|
|
|
callback(null);
|
|
|
|
}
|
2012-07-15 07:25:56 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
// if arg is already an image object
|
|
|
|
else if(this._isElement(arg)) {
|
|
|
|
callback(arg);
|
2012-07-16 11:12:18 +08:00
|
|
|
}
|
2012-07-15 09:32:00 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
// if arg is a string, then it's a data url
|
|
|
|
else if(this._isString(arg)) {
|
2013-03-25 04:12:33 +08:00
|
|
|
imageObj = new Image();
|
2012-12-02 04:04:10 +08:00
|
|
|
imageObj.onload = function() {
|
|
|
|
callback(imageObj);
|
|
|
|
}
|
|
|
|
imageObj.src = arg;
|
2012-07-16 11:12:18 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
//if arg is an object that contains the data property, it's an image object
|
|
|
|
else if(arg.data) {
|
2013-03-25 04:12:33 +08:00
|
|
|
canvas = document.createElement(CANVAS);
|
2012-12-02 04:04:10 +08:00
|
|
|
canvas.width = arg.width;
|
|
|
|
canvas.height = arg.height;
|
2013-03-25 04:12:33 +08:00
|
|
|
context = canvas.getContext(CONTEXT_2D);
|
2012-12-02 04:04:10 +08:00
|
|
|
context.putImageData(arg, 0, 0);
|
2013-03-25 04:12:33 +08:00
|
|
|
dataUrl = canvas.toDataURL();
|
|
|
|
imageObj = new Image();
|
2012-12-02 04:04:10 +08:00
|
|
|
imageObj.onload = function() {
|
|
|
|
callback(imageObj);
|
|
|
|
}
|
|
|
|
imageObj.src = dataUrl;
|
2012-09-25 11:34:23 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-12-02 04:04:10 +08:00
|
|
|
callback(null);
|
2012-09-25 11:34:23 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
_rgbToHex: function(r, g, b) {
|
|
|
|
return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
|
|
|
|
},
|
2013-04-14 22:43:02 +08:00
|
|
|
_hexToRgb: function(hex) {
|
|
|
|
var bigint = parseInt(hex, 16);
|
|
|
|
return {
|
|
|
|
r: (bigint >> 16) & 255,
|
|
|
|
g: (bigint >> 8) & 255,
|
|
|
|
b: bigint & 255
|
|
|
|
};
|
|
|
|
},
|
2012-12-02 04:04:10 +08:00
|
|
|
_getRandomColorKey: function() {
|
2013-04-12 14:09:41 +08:00
|
|
|
var randColor = (Math.random() * 0xFFFFFF << 0).toString(16);
|
|
|
|
while (randColor.length < 6) {
|
|
|
|
randColor = '0' + randColor;
|
|
|
|
}
|
|
|
|
return randColor;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
2013-04-30 14:12:28 +08:00
|
|
|
getRGB: function(color) {
|
|
|
|
var rgb;
|
|
|
|
// color string
|
|
|
|
if (color in COLORS) {
|
|
|
|
rgb = COLORS[color];
|
|
|
|
return {
|
|
|
|
r: rgb[0],
|
|
|
|
g: rgb[1],
|
|
|
|
b: rgb[2]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// hex
|
|
|
|
else if (color[0] === HASH) {
|
|
|
|
return this._hexToRgb(color.substring(1));
|
|
|
|
}
|
|
|
|
// rgb string
|
|
|
|
else if (color.substr(0, 4) === RGB_PAREN) {
|
|
|
|
rgb = RGB_REGEX.exec(color.replace(/ /g,''));
|
|
|
|
return {
|
|
|
|
r: parseInt(rgb[1]),
|
|
|
|
g: parseInt(rgb[2]),
|
|
|
|
b: parseInt(rgb[3])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// default
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
r: 0,
|
|
|
|
g: 0,
|
|
|
|
b: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2012-12-02 04:04:10 +08:00
|
|
|
// o1 takes precedence over o2
|
|
|
|
_merge: function(o1, o2) {
|
|
|
|
var retObj = this._clone(o2);
|
|
|
|
for(var key in o1) {
|
|
|
|
if(this._isObject(o1[key])) {
|
|
|
|
retObj[key] = this._merge(o1[key], retObj[key]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
retObj[key] = o1[key];
|
|
|
|
}
|
2012-09-25 11:34:23 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
return retObj;
|
|
|
|
},
|
|
|
|
// deep clone
|
|
|
|
_clone: function(obj) {
|
|
|
|
var retObj = {};
|
|
|
|
for(var key in obj) {
|
|
|
|
if(this._isObject(obj[key])) {
|
|
|
|
retObj[key] = this._clone(obj[key]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
retObj[key] = obj[key];
|
|
|
|
}
|
2012-09-25 11:34:23 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
return retObj;
|
|
|
|
},
|
|
|
|
_degToRad: function(deg) {
|
2013-03-25 04:12:33 +08:00
|
|
|
return deg * PI_OVER_DEG180;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
_radToDeg: function(rad) {
|
2013-03-25 04:12:33 +08:00
|
|
|
return rad * DEG180_OVER_PI;
|
2013-03-22 11:24:03 +08:00
|
|
|
},
|
|
|
|
_capitalize: function(str) {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
2012-09-25 11:34:23 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
};
|
|
|
|
})();
|