konva/dist/kinetic-core.min.js

30 lines
47 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: May 19 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,tempNodes:[],animations:[],animIdCounter:0,animRunning:!1,dragTimeInterval:0,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])},_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 a.length!==undefined},_isObject:function(a){return a===Object(a)},_getXY:function(a){var b=Kinetic.GlobalObject;if(a===undefined)return{x:0,y:0};if(b._isArray(a)){if(a.length===1){var c=a[0];return b._isArray(c)?{x:c[0],y:c[1]}:c}return{x:a[0],y:a[1]}}return a},_getSize:function(a){var b=Kinetic.GlobalObject;if(a===undefined)return{width:0,height:0};if(b._isArray(a)){if(a.length===1){var c=a[0];return b._isArray(c)?c.length===2?{width:c[0],height:c[1]}:{width:c[2],height:c[3]}:c}return a.length===2?{width:a[0],height:a[1]}:{width:a[2],height:a[3]}}return a},_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}},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.setDefaultAttrs({visible:!0,listening:!0,name:undefined,alpha:1,x:0,y:0,scale:{x:1,y:1},rotation:0,centerOffset:{x:0,y:0},dragConstraint:"none",dragBounds:{},draggable:!1}),this.eventListeners={},this.setAttrs(a)},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.eventListeners[g]&&f.length>1){var h=f[1];for(var i=0;i<this.eventListeners[g].length;i++)if(this.eventListeners[g][i].name===h){this.eventListeners[g].splice(i,1),this.eventListeners[g].length===0&&(this.eventListeners[g]=undefined);break}}else this.eventListeners[g]=undefined}},getAttrs:function(){return this.attrs},setDefaultAttrs:function(a){this.attrs===undefined&&(this.attrs={});if(a)for(var b in a)this.attrs[b]===undefined&&(this.attrs[b]=a[b])},setAttrs:function(a){var b=Kinetic.GlobalObject,c=this;if(a!==undefined){function d(a,e){for(var f in e){var g=e[f];if(b._isObject(g)&&!b._isArray(g)&&!b._isElement(g))a[f]===undefined&&(a[f]={}),d(a[f],g);else switch(f){case"draggable":c.draggable(e[f]);break;case"listening":c.listen(e[f]);break;case"rotationDeg
,h.rect(0,0,a,b),h.closePath(),this.applyStyles(),e!==undefined&&f!==undefined?h.drawImage(this.image,c,d,e,f,0,0,a,b):h.drawImage(this.image,0,0,a,b)}},Kinetic.Shape.apply(this,[a])},Kinetic.Image.prototype={setImage:function(a){this.image=a},getImage:function(){return this.image},setWidth:function(a){this.attrs.width=a},getWidth:function(){return this.attrs.width},setHeight:function(a){this.attrs.height=a},getHeight:function(){return this.attrs.height},setSize:function(a,b){this.attrs.width=a,this.attrs.height=b},getSize:function(){return{width:this.attrs.width,height:this.attrs.height}},getCrop:function(){return this.attrs.crop},setCrop:function(a){var b={};b.crop=a,this.setAttrs(b)}},Kinetic.GlobalObject.extend(Kinetic.Image,Kinetic.Shape),Kinetic.Sprite=function(a){this.setDefaultAttrs({index:0,frameRate:17}),a.drawFunc=function(){if(this.image!==undefined){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(),a.drawImage(this.image,d.x,d.y,d.width,d.height,0,0,d.width,d.height)}},Kinetic.Shape.apply(this,[a])},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(this.interval)},afterFrame:function(a,b){this.afterFrameIndex=a,this.afterFrameFunc=b},setAnimation:function(a){this.attrs.animation=a},setAnimations:function(a){this.attrs.animations=a},getAnimations:function(){return this.attrs.animations},getAnimation:function(){return this.attrs.animation},setIndex:function(a){this.attrs.index=a},_updateIndex:function(){var a=this.attrs.index,b=this.attrs.animation;a<this.attrs.animations[b].length-1?this.attrs.index++:this.attrs.index=0}},Kinetic.GlobalObject.extend(Kinetic.Sprite,Kinetic.Shape),Kinetic.Polygon=function(a){this.setDefaultAttrs({points:[]}),this.shapeType="Polygon",a.drawFunc=function(){var a=this.getContext();a.beginPath(),a.moveTo(this.attrs.points[0].x,this.attrs.points[0].y);for(var b=1;b<this.attrs.points.length;b++)a.lineTo(this.attrs.points[b].x,this.attrs.points[b].y);a.closePath(),this.applyStyles()},Kinetic.Shape.apply(this,[a])},Kinetic.Polygon.prototype={setPoints:function(a){var b={};b.points=a,this.setAttrs(b)},getPoints:function(){return this.attrs.points}},Kinetic.GlobalObject.extend(Kinetic.Polygon,Kinetic.Shape),Kinetic.RegularPolygon=function(a){this.setDefaultAttrs({radius:0,sides:0}),this.shapeType="RegularPolygon",a.drawFunc=function(){var a=this.getContext();a.beginPath(),a.moveTo(0,0-this.attrs.radius);for(var b=1;b<this.attrs.sides;b++){var c=this.attrs.radius*Math.sin(b*2*Math.PI/this.attrs.sides),d=-1*this.attrs.radius*Math.cos(b*2*Math.PI/this.attrs.sides);a.lineTo(c,d)}a.closePath(),this.applyStyles()},Kinetic.Shape.apply(this,[a])},Kinetic.RegularPolygon.prototype={setRadius:function(a){this.attrs.radius=a},getRadius:function(){return this.attrs.radius},setSides:function(a){this.attrs.sides=a},getSides:function(){return this.attrs.sides}},Kinetic.GlobalObject.extend(Kinetic.RegularPolygon,Kinetic.Shape),Kinetic.Star=function(a){this.setDefaultAttrs({numPoints:0,innerRadius:0,outerRadius:0}),this.shapeType="Star",a.drawFunc=function(){var a=this.getContext();a.beginPath(),a.moveTo(0,0-this.attrs.outerRadius);for(var b=1;b<this.attrs.numPoints*2;b++){var c=b%2===0?this.attrs.outerRadius:this.attrs.innerRadius,d=c*Math.sin(b*Math.PI/this.attrs.numPoints),e=-1*c*Math.cos(b*Math.PI/this.attrs.numPoints);a.lineTo(d,e)}a.closePath(),this.applyStyles()},Kinetic.Shape.apply(this,[a])},Kinetic.Star.prototype={setNumPoints:function(a){this.attrs.numPoints=a},getNumPoints:function(){return this.attrs.numPoints},setOuterRadius:function(a){this.attrs.outerRadius=a},getOuterRadius:function(){return this.attrs.outerRadius},setInnerRadius:function(a){this.attrs.innerRadius=a},getInnerRadius:function(){return this.attrs.innerRadius}},Kinetic.GlobalObject.extend(Kinetic.Star,Kinetic.Sha