konva/dist/kinetic-core.min.js

30 lines
42 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: Apr 28 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,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])},_addAnimation:function(a){a.id=this.animIdCounter++,this.animations.push(a)},_removeAnimation:function(a){var b=this.animations;for(var c=0;c<b.length;c++)if(b[c].id===a)return this.animations.splice(c,1),!1},_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)}},_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.frame.lastTime=0},_handleAnimation:function(){var a=this;this.animations.length>0?a._animationLoop():this.frame.lastTime=0},_isElement:function(a){return!!a&&a.nodeType==1},_isFunction:function(a){return!!(a&&a.constructor&&a.call&&a.apply)},_getPoint:function(a){return a.length===1?a[0]:{x:a[0],y:a[1]}}},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){var c=a[b];this.attrs[b]=a[b]}},setAttrs:function(a){var b=Kinetic.GlobalObject;if(a)for(var c in a){var d=a[c];if(b._isFunction(d)||b._isElement(d))this[c]=d;else switch(c){case"draggable":this.draggable(a[c]);break;case"listening":this.listen(a[c]);break;case"rotationDeg":this.attrs.rotation=a[c]*Math.PI/180;break;case"centerOffset":d.x!==undefined&&(this.attrs[c].x=d.x),d.y!==undefined&&(this.attrs[c].y=d.y);break;case"scale":d.x!==undefined&&(this.attrs[c].x=d.x),d.y!==undefined&&(this.attrs[c].y=d.y);break;case"crop":d.x!==undefined&&(this.attrs[c].x=d.x),d.y!==undefined&&(this.attrs[c].y=d.y),d.width!==undefined&&(this.attrs[c].width=d.width),d.height!==undefined&&(this.attrs[c].height=d.height);break;default:this.attrs[c]=a[c]}}},isVisible:function(){return this.attrs.visible},show:function(){this.attrs.visible=!0},hide:function(){this.attrs.visible=!1},getZIndex:function(){return this.index},getAbsoluteZIndex:function(){function e(b){var f=[];for(var g=0;g<b.length;g++){var h=b[g];d++,h.nodeType!=="Shape"&&(f=f.concat(h.getChildren())),h._id===c._id&&(g=b.length)}f.length>0&&f
},setInnerRadius:function(a){this.attrs.innerRadius=a},getInnerRadius:function(){return this.attrs.innerRadius}},Kinetic.GlobalObject.extend(Kinetic.Star,Kinetic.Shape),Kinetic.Text=function(a){this.setDefaultAttrs({fontFamily:"Calibri",text:"",fontSize:12,fill:undefined,textStroke:undefined,textStrokeWidth:undefined,align:"left",verticalAlign:"top",padding:0,fontStyle:"normal"}),this.shapeType="Text",a.drawFunc=function(){var a=this.getContext();a.font=this.attrs.fontStyle+" "+this.attrs.fontSize+"pt "+this.attrs.fontFamily,a.textBaseline="middle";var b=this.getTextHeight(),c=this.getTextWidth(),d=this.attrs.padding,e=0,f=0;switch(this.attrs.align){case"center":e=c/-2-d;break;case"right":e=-1*c-d}switch(this.attrs.verticalAlign){case"middle":f=b/-2-d;break;case"bottom":f=-1*b-d}a.save(),a.beginPath(),this.applyLineJoin(),a.rect(e,f,c+d*2,b+d*2),a.closePath(),this.fillStroke(),a.restore();var g=d+e,h=b/2+d+f;this.attrs.textFill!==undefined&&(a.fillStyle=this.attrs.textFill,a.fillText(this.attrs.text,g,h));if(this.attrs.textStroke!==undefined||this.attrs.textStrokeWidth!==undefined)this.attrs.textStroke===undefined?this.attrs.textStroke="black":this.attrs.textStrokeWidth===undefined&&(this.attrs.textStrokeWidth=2),a.lineWidth=this.attrs.textStrokeWidth,a.strokeStyle=this.attrs.textStroke,a.strokeText(this.attrs.text,g,h)},Kinetic.Shape.apply(this,[a])},Kinetic.Text.prototype={setFontFamily:function(a){this.attrs.fontFamily=a},getFontFamily:function(){return this.attrs.fontFamily},setFontSize:function(a){this.attrs.fontSize=a},getFontSize:function(){return this.attrs.fontSize},setFontStyle:function(a){this.attrs.fontStyle=a},getFontStyle:function(){return this.attrs.fontStyle},setTextFill:function(a){this.attrs.textFill=a},getTextFill:function(){return this.attrs.textFill},setTextStroke:function(a){this.attrs.textStroke=a},getTextStroke:function(){return this.attrs.textStroke},setTextStrokeWidth:function(a){this.attrs.textStrokeWidth=a},getTextStrokeWidth:function(){return this.attrs.textStrokeWidth},setPadding:function(a){this.attrs.padding=a},getPadding:function(){return this.attrs.padding},setAlign:function(a){this.attrs.align=a},getAlign:function(){return this.attrs.align},setVerticalAlign:function(a){this.attrs.verticalAlign=a},getVerticalAlign:function(){return this.attrs.verticalAlign},setText:function(a){this.attrs.text=a},getText:function(){return this.attrs.text},getTextWidth:function(){return this.getTextSize().width},getTextHeight:function(){return this.getTextSize().height},getTextSize:function(){var a=this.getContext();a.save(),a.font=this.attrs.fontStyle+" "+this.attrs.fontSize+"pt "+this.attrs.fontFamily;var b=a.measureText(this.attrs.text);return a.restore(),{width:b.width,height:parseInt(this.attrs.fontSize,10)}}},Kinetic.GlobalObject.extend(Kinetic.Text,Kinetic.Shape),Kinetic.Transform=function(){this.m=[1,0,0,1,0,0]},Kinetic.Transform.prototype={translate:function(a,b){this.m[4]+=this.m[0]*a+this.m[2]*b,this.m[5]+=this.m[1]*a+this.m[3]*b},scale:function(a,b){this.m[0]*=a,this.m[1]*=a,this.m[2]*=b,this.m[3]*=b},rotate:function(a){var b=Math.cos(a),c=Math.sin(a),d=this.m[0]*b+this.m[2]*c,e=this.m[1]*b+this.m[3]*c,f=this.m[0]*-c+this.m[2]*b,g=this.m[1]*-c+this.m[3]*b;this.m[0]=d,this.m[1]=e,this.m[2]=f,this.m[3]=g},getTranslation:function(){return{x:this.m[4],y:this.m[5]}},multiply:function(a){var b=this.m[0]*a.m[0]+this.m[2]*a.m[1],c=this.m[1]*a.m[0]+this.m[3]*a.m[1],d=this.m[0]*a.m[2]+this.m[2]*a.m[3],e=this.m[1]*a.m[2]+this.m[3]*a.m[3],f=this.m[0]*a.m[4]+this.m[2]*a.m[5]+this.m[4],g=this.m[1]*a.m[4]+this.m[3]*a.m[5]+this.m[5];this.m[0]=b,this.m[1]=c,this.m[2]=d,this.m[3]=e,this.m[4]=f,this.m[5]=g},invert:function(){var a=1/(this.m[0]*this.m[3]-this.m[1]*this.m[2]),b=this.m[3]*a,c=-this.m[1]*a,d=-this.m[2]*a,e=this.m[0]*a,f=a*(this.m[2]*this.m[5]-this.m[3]*this.m[4]),g=a*(this.m[1]*this.m[4]-this.m[0]*this.m[5]);this.m[0]=b,this.m[1]=c,this.m[2]=d,this.m[3]=e,this.m[4]=f,this.m[5]=g},getMatrix:function(){return this.m}},Kinetic.Transition=function(a,b){this.node=a,this.config=b,this.tweens=[];for(var c