konva/konva.min.js

43 lines
138 KiB
JavaScript
Raw Normal View History

2015-04-07 16:03:08 +08:00
/*
2017-08-19 15:01:43 +08:00
* Konva JavaScript Framework v1.6.8
2015-04-07 16:03:08 +08:00
* http://konvajs.github.io/
* Licensed under the MIT or GPL Version 2 licenses.
2017-09-05 19:30:08 +08:00
* Date: Tue Sep 05 2017
2015-04-07 16:03:08 +08:00
*
* Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
2017-03-20 19:53:11 +08:00
* Modified work Copyright (C) 2014 - 2017 by Anton Lavrenov (Konva)
2015-04-07 16:03:08 +08:00
*
* @license
* 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.
*/
2017-09-05 19:30:08 +08:00
!function(t){"use strict";var e=Math.PI/180,n={version:"1.6.8",stages:[],idCounter:0,ids:{},names:{},shapes:{},listenClickTap:!1,inDblClickWindow:!1,enableTrace:!1,traceArrMax:100,dblClickWindow:400,pixelRatio:void 0,dragDistance:3,angleDeg:!0,showWarnings:!0,Filters:{},isDragging:function(){var t=n.DD;return!!t&&t.isDragging},isDragReady:function(){var t=n.DD;return!!t&&!!t.node},_addId:function(t,e){void 0!==e&&(this.ids[e]=t)},_removeId:function(t){void 0!==t&&delete this.ids[t]},_addName:function(t,e){e&&(this.names[e]||(this.names[e]=[]),this.names[e].push(t))},_removeName:function(t,e){if(t){var n=this.names[t];if(n){for(var i=0;i<n.length;i++)n[i]._id===e&&n.splice(i,1);0===n.length&&delete this.names[t]}}},getAngle:function(t){return this.angleDeg?t*e:t},_detectIE:function(t){var e=t.indexOf("msie ");if(e>0)return parseInt(t.substring(e+5,t.indexOf(".",e)),10);if(t.indexOf("trident/")>0){var n=t.indexOf("rv:");return parseInt(t.substring(n+3,t.indexOf(".",n)),10)}var i=t.indexOf("edge/");return i>0&&parseInt(t.substring(i+5,t.indexOf(".",i)),10)},_parseUA:function(t){var e=t.toLowerCase(),i=/(chrome)[ /]([\w.]+)/.exec(e)||/(webkit)[ /]([\w.]+)/.exec(e)||/(opera)(?:.*version|)[ /]([\w.]+)/.exec(e)||/(msie) ([\w.]+)/.exec(e)||e.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+)|)/.exec(e)||[],a=!!t.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i),r=!!t.match(/IEMobile/i);return{browser:i[1]||"",version:i[2]||"0",isIE:n._detectIE(e),mobile:a,ieMobile:r}},UA:void 0},i=void 0!==t?t:"undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope?self:{};if(n.UA=n._parseUA(i.navigator&&i.navigator.userAgent||""),i.Konva&&console.error("Konva instance is already exist in current eviroment. Please use only one instance."),i.Konva=n,n.global=i,"object"!=typeof exports)"function"==typeof define&&define.amd&&define(function(){return n}),n.document=document,n.window=window;else{if(i.window&&i.window.document)n.document=i.window.document,n.window=i.window;else{var a=require("canvas"),r=require("jsdom").JSDOM;n.window=new r("<!DOCTYPE html><html><head></head><body></body></html>").window,n.document=n.window.document,n.window.Image=a.Image,n._nodeCanvas=a,n.isNode=!0}module.exports=n}}("undefined"!=typeof global?global:window),function(){"use strict";Konva.Collection=function(){var t=[].slice.call(arguments),e=t.length,n=0;for(this.length=e;n<e;n++)this[n]=t[n];return this},Konva.Collection.prototype=[],Konva.Collection.prototype.each=function(t){for(var e=0;e<this.length;e++)t(this[e],e)},Konva.Collection.prototype.toArray=function(){var t,e=[],n=this.length;for(t=0;t<n;t++)e.push(this[t]);return e},Konva.Collection.toCollection=function(t){var e,n=new Konva.Collection,i=t.length;for(e=0;e<i;e++)n.push(t[e]);return n},Konva.Collection._mapMethod=function(t){Konva.Collection.prototype[t]=function(){var e,n=this.length,i=[].slice.call(arguments);for(e=0;e<n;e++)this[e][t].apply(this[e],i);return this}},Konva.Collection.mapMethods=function(t){var e=t.prototype;for(var n in e)Konva.Collection._mapMethod(n)},Konva.Transform=function(t){this.m=t&&t.slice()||[1,0,0,1,0,0]},Konva.Transform.prototype={copy:function(){return new Konva.Transform(this.m)},point:function(t){var e=this.m;return{x:e[0]*t.x+e[2]*t.y+e[4],y:e[1]*t.x+e[3]*t.y+e[5]}},translate:function(t,e){return this.m[4]+=this.m[0]*t+this.m[2]*e,this.m[5]+=this.m[1]*t+this.m[3]*e,this},scale:function(t,e){return this.m[0]*=t,this.m[1]*=t,this.m[2]*=e,this.m[3]*=e,this},rotate:function(t){var e=Math.cos(t),n=Math.sin(t),i=this.m[0]*e+this.m[2]*n,a=this.m[1]*e+this.m[3]*n,r=this.m[0]*-n+this.m[2]*e,o=this.m[1]*-n+this.m[3]*e;return this.m[0]=i,this.m[1]=a,this.m[2]=r,this.m[3]=o,this},getTranslation:function(){return{x:this.m[4],y:this.m[5]}},skew:function(t,e){var n=this.m[0]+this.m[2]*e,i=this.m[1]+this.m[3]*e,a=this.m[2]+this.m[0]*t,r=this.m[3]+this.m[1]*t;return this.m[0]=n,this.m[1]=i,this.m[2]=a,this.m[3]=r,this},multiply:function(t){var e=this.m[0]*t.m[0]+this.m[2]*t.m[1],n=this.m[1]*t.m[0]+this.m[3]*t.m[1],i=this.m[0]*t.m[2]+this.m[2]*t.m[3],
2015-04-07 16:03:08 +08:00
* Sepia Filter
* Based on: Pixastic Lib - Sepia filter - v0.1.0
* Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
* @function
* @name Sepia
* @memberof Konva.Filters
* @param {Object} imageData
* @author Jacob Seidelin <jseidelin@nihilogic.dk>
* @license MPL v1.1 [http://www.pixastic.com/lib/license.txt]
* @example
* node.cache();
* node.filters([Konva.Filters.Sepia]);
*/
2017-08-26 19:12:01 +08:00
Konva.Filters.Sepia=function(t){var e,n,i,a,r,o,s,h,c,l=t.data,d=t.width,u=t.height,f=4*d;do{e=(u-1)*f,n=d;do{s=.393*(a=l[i=e+4*(n-1)])+.769*(r=l[i+1])+.189*(o=l[i+2]),h=.349*a+.686*r+.168*o,c=.272*a+.534*r+.131*o,l[i]=s>255?255:s,l[i+1]=h>255?255:h,l[i+2]=c>255?255:c,l[i+3]=l[i+3]}while(--n)}while(--u)}}(),function(){"use strict";Konva.Filters.Solarize=function(t){var e=t.data,n=t.width,i=4*n,a=t.height;do{var r=(a-1)*i,o=n;do{var s=r+4*(o-1),h=e[s],c=e[s+1],l=e[s+2];h>127&&(h=255-h),c>127&&(c=255-c),l>127&&(l=255-l),e[s]=h,e[s+1]=c,e[s+2]=l}while(--o)}while(--a)}}(),function(){"use strict";var t=function(t,e,n){var i,a,r,o,s=t.data,h=e.data,c=t.width,l=t.height,d=n.polarCenterX||c/2,u=n.polarCenterY||l/2,f=0,v=0,g=0,p=0,m=Math.sqrt(d*d+u*u);a=c-d,r=l-u,m=(o=Math.sqrt(a*a+r*r))>m?o:m;var _,y,S,K,C=l,x=c,w=360/x*Math.PI/180;for(y=0;y<x;y+=1)for(S=Math.sin(y*w),K=Math.cos(y*w),_=0;_<C;_+=1)a=Math.floor(d+m*_/C*K),f=s[(i=4*((r=Math.floor(u+m*_/C*S))*c+a))+0],v=s[i+1],g=s[i+2],p=s[i+3],h[(i=4*(y+_*c))+0]=f,h[i+1]=v,h[i+2]=g,h[i+3]=p},e=function(t,e,n){var i,a,r,o,s,h,c=t.data,l=e.data,d=t.width,u=t.height,f=n.polarCenterX||d/2,v=n.polarCenterY||u/2,g=0,p=0,m=0,_=0,y=Math.sqrt(f*f+v*v);a=d-f,r=u-v,y=(h=Math.sqrt(a*a+r*r))>y?h:y;var S,K,C,x=u,w=d,b=n.polarRotation||0;for(a=0;a<d;a+=1)for(r=0;r<u;r+=1)o=a-f,s=r-v,S=Math.sqrt(o*o+s*s)*x/y,K=(K=(180*Math.atan2(s,o)/Math.PI+360+b)%360)*w/360,C=Math.floor(K),g=c[(i=4*(Math.floor(S)*d+C))+0],p=c[i+1],m=c[i+2],_=c[i+3],l[(i=4*(r*d+a))+0]=g,l[i+1]=p,l[i+2]=m,l[i+3]=_},n=Konva.Util.createCanvasElement();Konva.Filters.Kaleidoscope=function(i){var a,r,o,s,h,c,l,d,u,f=i.width,v=i.height,g=Math.round(this.kaleidoscopePower()),p=Math.round(this.kaleidoscopeAngle()),m=Math.floor(f*(p%360)/360);if(!(g<1)){n.width=f,n.height=v;var _=n.getContext("2d").getImageData(0,0,f,v);t(i,_,{polarCenterX:f/2,polarCenterY:v/2});for(var y=f/Math.pow(2,g);y<=8;)y*=2,g-=1;var S=y=Math.ceil(y),K=0,C=S,x=1;for(m+y>f&&(K=S,C=0,x=-1),r=0;r<v;r+=1)for(a=K;a!==C;a+=x)d=4*(f*r+Math.round(a+m)%f),s=_.data[d+0],h=_.data[d+1],c=_.data[d+2],l=_.data[d+3],u=4*(f*r+a),_.data[u+0]=s,_.data[u+1]=h,_.data[u+2]=c,_.data[u+3]=l;for(r=0;r<v;r+=1)for(S=Math.floor(y),o=0;o<g;o+=1){for(a=0;a<S+1;a+=1)d=4*(f*r+a),s=_.data[d+0],h=_.data[d+1],c=_.data[d+2],l=_.data[d+3],u=4*(f*r+2*S-a-1),_.data[u+0]=s,_.data[u+1]=h,_.data[u+2]=c,_.data[u+3]=l;S*=2}e(_,i,{polarRotation:0})}},Konva.Factory.addGetterSetter(Konva.Node,"kaleidoscopePower",2,null,Konva.Factory.afterSetFilter),Konva.Factory.addGetterSetter(Konva.Node,"kaleidoscopeAngle",0,null,Konva.Factory.afterSetFilter)}(),function(){"use strict";Konva.Container=function(t){this.__init(t)},Konva.Util.addMethods(Konva.Container,{__init:function(t){this.children=new Konva.Collection,Konva.Node.call(this,t)},getChildren:function(t){if(!t)return this.children;var e=new Konva.Collection;return this.children.each(function(n){t(n)&&e.push(n)}),e},hasChildren:function(){return this.getChildren().length>0},removeChildren:function(){for(var t,e=Konva.Collection.toCollection(this.children),n=0;n<e.length;n++)delete(t=e[n]).parent,t.index=0,t.remove();return e=null,this.children=new Konva.Collection,this},destroyChildren:function(){for(var t,e=Konva.Collection.toCollection(this.children),n=0;n<e.length;n++)delete(t=e[n]).parent,t.index=0,t.destroy();return e=null,this.children=new Konva.Collection,this},add:function(t){if(arguments.length>1){for(var e=0;e<arguments.length;e++)this.add(arguments[e]);return this}if(t.getParent())return t.moveTo(this),this;var n=this.children;return this._validateAdd(t),t.index=n.length,t.parent=this,n.push(t),this._fire("add",{child:t}),Konva.DD&&t.isDragging()&&Konva.DD.anim.setLayers(t.getLayer()),this},destroy:function(){return this.hasChildren()&&this.destroyChildren(),Konva.Node.prototype.destroy.call(this),this},find:function(t){var e,n,i,a,r,o,s,h=[],c=t.replace(/ /g,"").split(","),l=c.length;for(e=0;e<l;e++)if(i=c[e],Konva.Util.isValidSelector(i)||(Konva.Util.warn('Selector "'+i+'" is invalid. Allowed selectors examples are "#foo", ".bar" or "Group".'),Konva