2012-06-02 14:56:01 +08:00
|
|
|
/**
|
|
|
|
* KineticJS JavaScript Library core
|
|
|
|
* http://www.kineticjs.com/
|
|
|
|
* Copyright 2012, Eric Rowell
|
|
|
|
* Licensed under the MIT or GPL Version 2 licenses.
|
2012-07-01 15:19:56 +08:00
|
|
|
* Date: Jul 01 2012
|
2012-06-02 14:56:01 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 - 2012 by Eric Rowell
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2012-07-01 15:19:56 +08:00
|
|
|
var Kinetic={};Kinetic.GlobalObject={stages:[],idCounter:0,tempNodes:[],animations:[],animIdCounter:0,animRunning:!1,maxDragTimeInterval:20,frame:{time:0,timeDiff:0,lastTime:0},drag:{moving:!1,node:undefined,offset:{x:0,y:0},lastDrawTime:0},extend:function(a,b){for(var c in b.prototype)b.prototype.hasOwnProperty(c)&&a.prototype[c]===undefined&&(a.prototype[c]=b.prototype[c])},addSetters:function(constructor,a){for(var b=0;b<a.length;b++){var c=a[b];this._addSetter(constructor,c)}},addGetters:function(constructor,a){for(var b=0;b<a.length;b++){var c=a[b];this._addGetter(constructor,c)}},addSettersGetters:function(constructor,a){this.addSetters(constructor,a),this.addGetters(constructor,a)},_pullNodes:function(a){var b=this.tempNodes;for(var c=0;c<b.length;c++){var d=b[c];d.getStage()!==undefined&&d.getStage()._id===a._id&&(a._addId(d),a._addName(d),this.tempNodes.splice(c,1),c-=1)}},_addAnimation:function(a){a.id=this.animIdCounter++,this.animations.push(a)},_removeAnimation:function(a){var b=a.id,c=this.animations;for(var d=0;d<c.length;d++)if(c[d].id===b)return this.animations.splice(d,1),!1},_runFrames:function(){var a={};for(var b=0;b<this.animations.length;b++){var c=this.animations[b];c.node&&c.node._id!==undefined&&(a[c.node._id]=c.node),c.func(this.frame)}for(var d in a)a[d].draw()},_updateFrameObject:function(){var a=new Date,b=a.getTime();this.frame.lastTime===0?this.frame.lastTime=b:(this.frame.timeDiff=b-this.frame.lastTime,this.frame.lastTime=b,this.frame.time+=this.frame.timeDiff)},_animationLoop:function(){if(this.animations.length>0){this._updateFrameObject(),this._runFrames();var a=this;requestAnimFrame(function(){a._animationLoop()})}else this.animRunning=!1,this.frame.lastTime=0},_handleAnimation:function(){var a=this;this.animRunning?this.frame.lastTime=0:(this.animRunning=!0,a._animationLoop())},_isElement:function(a){return!!a&&a.nodeType==1},_isFunction:function(a){return!!(a&&a.constructor&&a.call&&a.apply)},_isArray:function(a){return Object.prototype.toString.call(a)=="[object Array]"},_isObject:function(a){return!!a&&a.constructor==Object},_isNumber:function(a){return Object.prototype.toString.call(a)=="[object Number]"},_hasMethods:function(a){var b=[];for(var c in a)this._isFunction(a[c])&&b.push(c);return b.length>0},_getXY:function(a){if(this._isNumber(a))return{x:a,y:a};if(this._isArray(a)){if(a.length===1){var b=a[0];if(this._isNumber(b))return{x:b,y:b};if(this._isArray(b))return{x:b[0],y:b[1]};if(this._isObject(b))return b}else if(a.length>=2)return{x:a[0],y:a[1]}}else if(this._isObject(a))return a;return{x:0,y:0}},_getSize:function(a){if(this._isNumber(a))return{width:a,height:a};if(this._isArray(a))if(a.length===1){var b=a[0];if(this._isNumber(b))return{width:b,height:b};if(this._isArray(b)){if(b.length>=4)return{width:b[2],height:b[3]};if(b.length>=2)return{width:b[0],height:b[1]}}else if(this._isObject(b))return b}else{if(a.length>=4)return{width:a[2],height:a[3]};if(a.length>=2)return{width:a[0],height:a[1]}}else if(this._isObject(a))return a;return{width:0,height:0}},_getPoints:function(a){if(a===undefined)return[];if(this._isObject(a[0]))return a;var b=[];for(var c=0;c<a.length;c+=2)b.push({x:a[c],y:a[c+1]});return b},_addSetter:function(constructor,a){var b=this,c="set"+a.charAt(0).toUpperCase()+a.slice(1);constructor.prototype[c]=function(){var b;arguments.length==1?b=arguments[0]:b=Array.prototype.slice.call(arguments);var c={};c[a]=b,this.setAttrs(c)}},_addGetter:function(constructor,a){var b=this,c="get"+a.charAt(0).toUpperCase()+a.slice(1);constructor.prototype[c]=function(b){return this.attrs[a]}}},window.requestAnimFrame=function(a){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(a,1e3/60)}}(),Kinetic.Node=function(a){this.defaultNodeAttrs={visible:!0,listening:!0,name:undefined,alpha:1,x:0,y:0,scale:{x:1,y:1},rotation:0,offset:{x:0,y:0},dragConstraint:"none",dragBounds:{},draggable:!1},this.setDefaultAttrs(this.defaultNodeA
|
2012-07-02 08:08:59 +08:00
|
|
|
(),this.appliedShadow=!1,this.attrs.drawFunc.call(this),c.restore()}}},Kinetic.GlobalObject.extend(Kinetic.Shape,Kinetic.Node),Kinetic.GlobalObject.addSettersGetters(Kinetic.Shape,["fill","stroke","lineJoin","strokeWidth","shadow","drawFunc"]),Kinetic.Rect=function(a){this.setDefaultAttrs({width:0,height:0,cornerRadius:0}),this.shapeType="Rect",a.drawFunc=function(){var a=this.getContext();a.beginPath(),this.attrs.cornerRadius===0?a.rect(0,0,this.attrs.width,this.attrs.height):(a.moveTo(this.attrs.cornerRadius,0),a.lineTo(this.attrs.width-this.attrs.cornerRadius,0),a.arc(this.attrs.width-this.attrs.cornerRadius,this.attrs.cornerRadius,this.attrs.cornerRadius,Math.PI*3/2,0,!1),a.lineTo(this.attrs.width,this.attrs.height-this.attrs.cornerRadius),a.arc(this.attrs.width-this.attrs.cornerRadius,this.attrs.height-this.attrs.cornerRadius,this.attrs.cornerRadius,0,Math.PI/2,!1),a.lineTo(this.attrs.cornerRadius,this.attrs.height),a.arc(this.attrs.cornerRadius,this.attrs.height-this.attrs.cornerRadius,this.attrs.cornerRadius,Math.PI/2,Math.PI,!1),a.lineTo(0,this.attrs.cornerRadius),a.arc(this.attrs.cornerRadius,this.attrs.cornerRadius,this.attrs.cornerRadius,Math.PI,Math.PI*3/2,!1)),a.closePath(),this.fill(),this.stroke()},Kinetic.Shape.apply(this,[a])},Kinetic.Rect.prototype={setSize:function(){var a=Kinetic.GlobalObject._getSize(Array.prototype.slice.call(arguments));this.setAttrs(a)},getSize:function(){return{width:this.attrs.width,height:this.attrs.height}}},Kinetic.GlobalObject.extend(Kinetic.Rect,Kinetic.Shape),Kinetic.GlobalObject.addSettersGetters(Kinetic.Rect,["width","height","cornerRadius"]),Kinetic.Ellipse=function(a){this.setDefaultAttrs({radius:{x:0,y:0}}),this.shapeType="Ellipse",a.drawFunc=function(){var a=this.getCanvas(),b=this.getContext(),c=this.getRadius();b.beginPath(),b.save(),c.x!==c.y&&b.scale(1,c.y/c.x),b.arc(0,0,c.x,0,Math.PI*2,!0),b.restore(),b.closePath(),this.fill(),this.stroke()},Kinetic.Shape.apply(this,[a]),this._convertRadius();var b=this;this.on("radiusChange",function(){b._convertRadius()})},Kinetic.Circle=Kinetic.Ellipse,Kinetic.Ellipse.prototype={_convertRadius:function(){var a=Kinetic.GlobalObject,b=this.getRadius();if(a._isObject(b))return!1;this.attrs.radius=a._getXY(b)}},Kinetic.GlobalObject.extend(Kinetic.Ellipse,Kinetic.Shape),Kinetic.GlobalObject.addSettersGetters(Kinetic.Ellipse,["radius"]),Kinetic.Image=function(a){this.shapeType="Image",a.drawFunc=function(){if(!!this.attrs.image){var a=this.attrs.width?this.attrs.width:this.attrs.image.width,b=this.attrs.height?this.attrs.height:this.attrs.image.height,c=this.getCanvas(),d=this.getContext();d.beginPath(),d.rect(0,0,a,b),d.closePath(),this.fill(),this.stroke();if(this.attrs.crop&&this.attrs.crop.width&&this.attrs.crop.height){var e=this.attrs.crop.x?this.attrs.crop.x:0,f=this.attrs.crop.y?this.attrs.crop.y:0,g=this.attrs.crop.width,h=this.attrs.crop.height;this.drawImage(this.attrs.image,e,f,g,h,0,0,a,b)}else this.drawImage(this.attrs.image,0,0,a,b)}},Kinetic.Shape.apply(this,[a])},Kinetic.Image.prototype={setSize:function(){var a=Kinetic.GlobalObject._getSize(Array.prototype.slice.call(arguments));this.setAttrs(a)},getSize:function(){return{width:this.attrs.width,height:this.attrs.height}}},Kinetic.GlobalObject.extend(Kinetic.Image,Kinetic.Shape),Kinetic.GlobalObject.addSettersGetters(Kinetic.Image,["height","width","image","crop"]),Kinetic.Sprite=function(a){this.setDefaultAttrs({index:0,frameRate:17}),a.drawFunc=function(){if(!!this.attrs.image){var a=this.getContext(),b=this.attrs.animation,c=this.attrs.index,d=this.attrs.animations[b][c];a.beginPath(),a.rect(0,0,d.width,d.height),a.closePath(),this.drawImage(this.attrs.image,d.x,d.y,d.width,d.height,0,0,d.width,d.height)}},Kinetic.Shape.apply(this,[a]);var b=this;this.on("animationChange",function(){b.setIndex(0)})},Kinetic.Sprite.prototype={start:function(){var a=this,b=this.getLayer();this.interval=setInterval(function(){a._updateIndex(),b.draw(),a.afterFrameFunc&&a.attrs.index===a.afterFrameIndex&&a.afterFrameFunc()},1e3/this.attrs.frameRate)},stop:function(){clearInterval(t
|