konva/dist/kinetic-core.min.js

29 lines
28 KiB
JavaScript
Raw Normal View History

/**
* KineticJS JavaScript Library core
* http://www.kineticjs.com/
* Copyright 2012, Eric Rowell
* Licensed under the MIT or GPL Version 2 licenses.
* Date: Mar 24 2012
*
* 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.
*/
var Kinetic={};Kinetic.GlobalObject={stages:[],idCounter:0,frame:{time:0,timeDiff:0,lastTime:0},drag:{moving:!1,node:undefined,offset:{x:0,y: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])},_isaCanvasAnimating:function(){for(var a=0;a<this.stages.length;a++){var b=this.stages[a];if(b.isAnimating)return!0;for(var c=0;c<b.children.length;c++){var d=b.children[c];if(d.transitions.length>0)return!0}}return this.frame.lastTime=0,!1},_endTransition:function(){var a=this.config;for(var b in a)if(a.hasOwnProperty(b))if(a[b].x!==undefined||a[b].y!==undefined){var c=["x","y"];for(var d=0;d<c.length;d++){var e=c[d];a[b][e]!==undefined&&(this.node[b][e]=a[b][e])}}else this.node[b]=a[b]},_transitionPow:function(a,b,c,d){var e=d(),f=a.config,g=this.frame.timeDiff;if(c!==undefined){var h=a.starts[b][c],i=f[b][c]-h,j=i/Math.pow(f.duration*1e3,e);a.node[b][c]=j*Math.pow(a.time,e)+h}else{var h=a.starts[b],i=f[b]-h,j=i/Math.pow(f.duration*1e3,e);a.node[b]=j*Math.pow(a.time,e)+h}},_chooseTransition:function(a,b,c){var d=a.config,e=this;switch(d.easing){case"ease-in":this._transitionPow(a,b,c,function(){return 2.5});break;case"ease-out":this._transitionPow(a,b,c,function(){return.4});break;case"ease-in-out":this._transitionPow(a,b,c,function(){var b=-2.1,c=b/(d.duration*1e3);return 2.5+c*a.time});break;default:this._transitionPow(a,b,c,function(){return 1})}},_runTransition:function(a){var b=a.config;for(var c in b)if(b.hasOwnProperty(c)&&c!=="duration"&&c!=="easing"&&c!=="callback")if(b[c].x!==undefined||b[c].y!==undefined){var d=["x","y"];for(var e=0;e<d.length;e++){var f=d[e];b[c][f]!==undefined&&this._chooseTransition(a,c,f)}}else this._chooseTransition(a,c)},_clearTransition:function(a){var b=a.getLayer();for(var c=0;c<b.transitions.length;c++)b.transitions[c].node.id===a.id&&b.transitions.splice(c,1)},_runFrames:function(){for(var a=0;a<this.stages.length;a++){var b=this.stages[a];b.isAnimating&&b.onFrameFunc!==undefined&&b.onFrameFunc(this.frame);var c=b.getChildren();for(var d=0;d<c.length;d++){var e=c[d],f=!1;for(var g=0;g<e.transitions.length;g++){f=!0;var h=e.transitions[g];h.time+=this.frame.timeDiff,h.time>=h.config.duration*1e3?(this._endTransition.apply(h),this._clearTransition(h.node),h.config.callback!==undefined&&h.config.callback()):this._runTransition(h)}f&&e.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._isaCanvasAnimating()){this._updateFrameObject(),this._runFrames();var a=this;requestAnimFrame(function(){a._animationLoop()})}},_handleAnimation:function(){var a=this;this._isaCanvasAnimating()&&a._animationLoop()}},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.visible=!0,this.isListening=!0,this.name=undefined,this.alpha=1,this.x=0,this.y=0,this.scale={x:1,y:1},this.rotation=0,this.centerOffset={x:0,y:0},this.eventListeners={},this.dragConstraint="none",this.dragBounds={},this._draggable=!1;if(a)for(var b in a)switch(b){case"draggable":this.draggable(a[b]);break;case"listen":this.listen(a[b]);break;case"rotationDeg":this.rotation=a[b]*Math.PI/180;break;default:this[b]=a[b]}this.centerOffset.x===undefined&&(this.centerOffset.x=0),this.centerOffset.y===undefined&&(this.centerOffset.y=0)},Kinetic.Node.prototype={on:function(a,b){var c=a.split(" ");for(var d=0;d<c.length;d++){var e=c[d],f=e.indexOf("touch")===-1?"on"+e:e,g=f.split("."),h=g[0],i=g.length>1?g[1]:"";this.eventListeners[h]||(this.eventListeners[h]=[]),this.eventListeners[h].push({name:i,handler:b})}},off:function(a){var b=a.split(" ");for(var c=0;c<b.length;c++){var d=b[c],e=d.indexOf("touch")===-1?"on"+d:d,f=e.split("."),g=f[0];if(this.