Files
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.
* Date: Jul 22 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},warn:function(a){console&&console.warn&&console.warn("Kinetic warning: "+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)}}},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,b){if(!a)b(null);else if(this._isElement(a))b(a);else if(this._isString(a)){var c=new Image;c.onload=function(){b(c)},c.src=a}else if(a.data){var d=document.createElement("canvas");d.width=a.width,d.height=a.height;var e=d.getContext("2d");e.putImageData(a,0,0);var f=d.toDataURL(),c=new Image;c.onload=function(){b(c)},c.src=f}else b(null)}},Kinetic.Canvas=function(a,b){this.element=document.createElement("canvas"),this.context=this.element.getContext("2d"),this.element.width=a,this.element.height=b},Kinetic.Canvas.prototype={clear:function(){var a=this.getContext(),b=this.getElement();a.clearRect(0,0,b.width,b.height)},getElement:function(){return this.element},getContext:function(){return this.context},setWidth:function(a){this.element.width=a},setHeight:function(a){this.element.height=a},getWidth:function(){return this.element.width},getHeight:function(){return this.element.height},setSize:function(a,b){this.setWidth(a),this.setHeight(b)},strip:function(){var a=this.context;a.stroke=function(){},a.fill=function(){},a.fillRect=function(b,c,d,e){a.rect(b,c,d,e)},a.strokeRect=function(b,c,d,e){a.rect(b,c,d,e)},a.drawImage=function(){},a.fillText=function(){},a.strokeText=function(){}},toDataURL:function(a,b){try{return this.element.toDataURL(a,b)}catch(c){return this.element.toDataURL()}}},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,
f,a.fill(a)}a.restore()}b&&this.fill(a)},fillText:function(a,b){var c=!1;this.attrs.textFill&&(a.save(),this.attrs.shadow&&!this.appliedShadow&&(c=this._applyShadow(a)),a.fillStyle=this.attrs.textFill,a.fillText(b,0,0),a.restore()),c&&this.fillText(contex,b,0,0)},strokeText:function(a,b){var c=!1;if(this.attrs.textStroke||this.attrs.textStrokeWidth){a.save(),this.attrs.shadow&&!this.appliedShadow&&(c=this._applyShadow(a));var d=this.attrs.textStroke?this.attrs.textStroke:"black",e=this.attrs.textStrokeWidth?this.attrs.textStrokeWidth:2;a.lineWidth=e,a.strokeStyle=d,a.strokeText(b,0,0),a.restore()}c&&this.strokeText(a,b,0,0)},drawImage:function(){var a=!1,b=arguments[0];b.save();var c=Array.prototype.slice.call(arguments);if(c.length===6||c.length===10)this.attrs.shadow&&!this.appliedShadow&&(a=this._applyShadow(b)),c.length===6?b.drawImage(c[1],c[2],c[3],c[4],c[5]):b.drawImage(c[1],c[2],c[3],c[4],c[5],c[6],c[7],c[8],c[9]);b.restore(),a&&this.drawImage.apply(this,c)},applyLineJoin:function(a){this.attrs.lineJoin&&(a.lineJoin=this.attrs.lineJoin)},_applyShadow:function(a){var 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.pathCanvas,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(this.attrs.drawFunc){var b=this.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])}var j=this.getAbsoluteAlpha();j!==1&&(c.globalAlpha=j),this.applyLineJoin(c),this.appliedShadow=!1,this.attrs.drawFunc.call(this,a.getContext()),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(a){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(a),this.stroke(a)},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(a){var b=this.getRadius();a.beginPath(),a.save(),b.x!==b.y&&a.scale(1,b.y/b.x),a.arc(0,0,b.x,0,Math.PI*2,!0),a.restore(),a.closePath(),this.fill(a),this.stroke(a)},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