konva/dist/kinetic-core.min.js

30 lines
55 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.
2012-07-15 15:06:35 +08:00
* Date: Jul 15 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.Filters={},Kinetic.Global={BUBBLE_WHITELIST:["mousedown","mousemove","mouseup","mouseover","mouseout","click","dblclick","touchstart","touchmove","touchend","tap","dbltap","dragstart","dragmove","dragend"],stages:[],idCounter:0,tempNodes:[],maxDragTimeInterval:20,drag:{moving:!1,offset:{x:0,y:0},lastDrawTime:0},_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)}}},Kinetic.Type={_isElement:function(a){return!!a&&a.nodeType==1},_isFunction:function(a){return!!(a&&a.constructor&&a.call&&a.apply)},_isObject:function(a){return!!a&&a.constructor==Object},_isArray:function(a){return Object.prototype.toString.call(a)=="[object Array]"},_isNumber:function(a){return Object.prototype.toString.call(a)=="[object Number]"},_isString:function(a){return Object.prototype.toString.call(a)=="[object String]"},_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},_getImage:function(a){if(this._isElement(a))return a;if(this._isString(a)){var b=new Image;return b.src=a,b}if(a.data){var c=document.createElement("canvas");c.width=a.width,c.height=a.height;var d=c.getContext("2d");d.putImageData(a,0,0);var e=c.toDataURL(),b=new Image;return b.src=e,b}return null}},function(){var a=!1;Kinetic.Class=function(){},Kinetic.Class.extend=function(b){function f(){!a&&this.init&&this.init.apply(this,arguments)}var c=this.prototype;a=!0;var d=new this;a=!1;for(var e in b)d[e]=typeof b[e]=="function"&&typeof c[e]=="function"?function(a,b){return function(){var d=this._super;this._super=c[a];var e=b.apply(this,arguments);return this._super=d,e}}(e,b[e]):b[e];return f.prototype=d,f.prototype.constructor=f,f.extend=arguments.callee,f}}(),Kinetic.Animation={animations:[],animIdCounter:0,animRunning:!1,frame:{time:0,timeDiff:0,lastTime:0},_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&&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())}},requestAnimFrame=function(a){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(a,1e3/60)}}(),Kinetic.Node=Kinetic
,c.strokeStyle=this.attrs.textStroke,c.strokeText(a,0,0);c.restore(),b&&this.strokeText(a,0,0)},drawImage:function(){var a=!1,b=this.getContext();b.save();var c=Array.prototype.slice.call(arguments);if(c.length===5||c.length===9){this.attrs.shadow&&!this.appliedShadow&&(a=this._applyShadow());switch(c.length){case 5:b.drawImage(c[0],c[1],c[2],c[3],c[4]);break;case 9:b.drawImage(c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7],c[8])}}b.restore(),a&&this.drawImage.apply(this,c)},applyLineJoin:function(){var a=this.getContext();this.attrs.lineJoin&&(a.lineJoin=this.attrs.lineJoin)},_applyShadow:function(){var a=this.getContext(),b=this.attrs.shadow;if(b){var c=this.getAbsoluteAlpha(),d=b.color?b.color:"black",e=b.blur?b.blur:5,f=b.offset?b.offset:{x:0,y:0};return b.alpha&&(a.globalAlpha=b.alpha*c),a.shadowColor=d,a.shadowBlur=e,a.shadowOffsetX=f.x,a.shadowOffsetY=f.y,this.appliedShadow=!0,!0}return!1},intersects:function(){var a=Kinetic.Type._getXY(Array.prototype.slice.call(arguments)),b=this.getStage();if(this.attrs.detectionType==="path"){var c=b.pathLayer,d=c.getContext();return this._draw(c),d.isPointInPath(a.x,a.y)}if(this.imageData){var e=b.attrs.width,f=this.imageData.data[(e*a.y+a.x)*4+3];return f}return!1},_draw:function(a){if(a&&this.attrs.drawFunc){var b=a.getStage(),c=a.getContext(),d=[],e=this.parent;d.unshift(this);while(e)d.unshift(e),e=e.parent;c.save();for(var f=0;f<d.length;f++){var g=d[f],h=g.getTransform(),i=h.getMatrix();c.transform(i[0],i[1],i[2],i[3],i[4],i[5])}this.tempLayer=a;var j=this.getAbsoluteAlpha();j!==1&&(c.globalAlpha=j),this.applyLineJoin(),this.appliedShadow=!1,this.attrs.drawFunc.call(this),c.restore()}}}),Kinetic.Node.addGettersSetters(Kinetic.Shape,["fill","stroke","lineJoin","strokeWidth","shadow","drawFunc","filter"]),Kinetic.Rect=Kinetic.Shape.extend({init: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()},this._super(a)},setSize:function(){var a=Kinetic.Type._getSize(Array.prototype.slice.call(arguments));this.setAttrs(a)},getSize:function(){return{width:this.attrs.width,height:this.attrs.height}}}),Kinetic.Node.addGettersSetters(Kinetic.Rect,["width","height","cornerRadius"]),Kinetic.Ellipse=Kinetic.Shape.extend({init: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()},this._super(a),this._convertRadius();var b=this;this.on("radiusChange.kinetic",function(){b._convertRadius()})},_convertRadius:function(){var a=Kinetic.Type,b=this.getRadius();if(a._isObject(b))return!1;this.attrs.radius=a._getXY(b)}}),Kinetic.Circle=Kinetic.Ellipse,Kinetic.Node.addGettersSetters(Kinetic.Ellipse,["radius"]),Kinetic.Image=Kinetic.Shape.extend({init: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