mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
250 lines
7.9 KiB
JavaScript
250 lines
7.9 KiB
JavaScript
|
|
/*
|
|
* Konva JavaScript Framework v@@version
|
|
* http://konvajs.github.io/
|
|
* Licensed under the MIT or GPL Version 2 licenses.
|
|
* Date: @@date
|
|
*
|
|
* Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
|
|
* Modified work Copyright (C) 2014 - 2015 by Anton Lavrenov (Konva)
|
|
*
|
|
* @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.
|
|
*/
|
|
|
|
// runtime check for already included Konva
|
|
(function(global){
|
|
'use strict';
|
|
/**
|
|
* @namespace Konva
|
|
*/
|
|
|
|
var PI_OVER_180 = Math.PI / 180;
|
|
|
|
var Konva = {
|
|
// public
|
|
version: '@@version',
|
|
|
|
// private
|
|
stages: [],
|
|
idCounter: 0,
|
|
ids: {},
|
|
names: {},
|
|
shapes: {},
|
|
listenClickTap: false,
|
|
inDblClickWindow: false,
|
|
|
|
// configurations
|
|
enableTrace: false,
|
|
traceArrMax: 100,
|
|
dblClickWindow: 400,
|
|
/**
|
|
* Global pixel ratio configuration. KonvaJS automatically detect pixel ratio of current device.
|
|
* But you may override such property, if you want to use your value.
|
|
* @property pixelRatio
|
|
* @default undefined
|
|
* @memberof Konva
|
|
* @example
|
|
* Konva.pixelRatio = 1;
|
|
*/
|
|
pixelRatio: undefined,
|
|
/**
|
|
* Drag distance property. If you start to drag a node you may want to wait until pointer is moved to some distance from start point,
|
|
* only then start dragging.
|
|
* @property dragDistance
|
|
* @default 0
|
|
* @memberof Konva
|
|
* @example
|
|
* Konva.dragDistance = 10;
|
|
*/
|
|
dragDistance: 0,
|
|
/**
|
|
* Use degree values for angle properties. You may set this property to false if you want to use radiant values.
|
|
* @property angleDeg
|
|
* @default true
|
|
* @memberof Konva
|
|
* @example
|
|
* node.rotation(45); // 45 degrees
|
|
* Konva.angleDeg = false;
|
|
* node.rotation(Math.PI / 2); // PI/2 radian
|
|
*/
|
|
angleDeg: true,
|
|
/**
|
|
* Show different warnings about errors or wrong API usage
|
|
* @property showWarnings
|
|
* @default true
|
|
* @memberof Konva
|
|
* @example
|
|
* Konva.showWarnings = false;
|
|
*/
|
|
showWarnings: true,
|
|
|
|
|
|
|
|
/**
|
|
* @namespace Filters
|
|
* @memberof Konva
|
|
*/
|
|
Filters: {},
|
|
|
|
/**
|
|
* returns whether or not drag and drop is currently active
|
|
* @method
|
|
* @memberof Konva
|
|
*/
|
|
isDragging: function() {
|
|
var dd = Konva.DD;
|
|
|
|
// if DD is not included with the build, then
|
|
// drag and drop is not even possible
|
|
if (dd) {
|
|
return dd.isDragging;
|
|
}
|
|
return false;
|
|
},
|
|
/**
|
|
* returns whether or not a drag and drop operation is ready, but may
|
|
* not necessarily have started
|
|
* @method
|
|
* @memberof Konva
|
|
*/
|
|
isDragReady: function() {
|
|
var dd = Konva.DD;
|
|
|
|
// if DD is not included with the build, then
|
|
// drag and drop is not even possible
|
|
if (dd) {
|
|
return !!dd.node;
|
|
}
|
|
return false;
|
|
},
|
|
_addId: function(node, id) {
|
|
if(id !== undefined) {
|
|
this.ids[id] = node;
|
|
}
|
|
},
|
|
_removeId: function(id) {
|
|
if(id !== undefined) {
|
|
delete this.ids[id];
|
|
}
|
|
},
|
|
_addName: function(node, name) {
|
|
if(name) {
|
|
if(!this.names[name]) {
|
|
this.names[name] = [];
|
|
}
|
|
this.names[name].push(node);
|
|
}
|
|
},
|
|
_removeName: function(name, _id) {
|
|
if(!name) {
|
|
return;
|
|
}
|
|
var nodes = this.names[name];
|
|
if(!nodes) {
|
|
return;
|
|
}
|
|
for(var n = 0; n < nodes.length; n++) {
|
|
var no = nodes[n];
|
|
if(no._id === _id) {
|
|
nodes.splice(n, 1);
|
|
}
|
|
}
|
|
if(nodes.length === 0) {
|
|
delete this.names[name];
|
|
}
|
|
},
|
|
getAngle: function(angle) {
|
|
return this.angleDeg ? angle * PI_OVER_180 : angle;
|
|
},
|
|
_parseUA: function(userAgent) {
|
|
var ua = userAgent.toLowerCase(),
|
|
// jQuery UA regex
|
|
match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
|
|
/(webkit)[ \/]([\w.]+)/.exec( ua ) ||
|
|
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
|
|
/(msie) ([\w.]+)/.exec( ua ) ||
|
|
ua.indexOf('compatible') < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
|
|
[],
|
|
|
|
// adding mobile flag as well
|
|
mobile = !!(userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)),
|
|
ieMobile = !!(userAgent.match(/IEMobile/i));
|
|
|
|
return {
|
|
browser: match[ 1 ] || '',
|
|
version: match[ 2 ] || '0',
|
|
|
|
// adding mobile flab
|
|
mobile: mobile,
|
|
ieMobile: ieMobile // If this is true (i.e., WP8), then Konva touch events are executed instead of equivalent Konva mouse events
|
|
};
|
|
},
|
|
// user agent
|
|
UA: undefined
|
|
};
|
|
|
|
var glob =
|
|
typeof window !== 'undefined' ? window :
|
|
typeof global !== 'undefined' ? global :
|
|
typeof WorkerGlobalScope !== 'undefined' ? self : {};
|
|
|
|
|
|
Konva.UA = Konva._parseUA((glob.navigator && glob.navigator.userAgent) || '');
|
|
|
|
if (glob.Konva) {
|
|
console.error(
|
|
'Konva instance is already exist in current eviroment. ' +
|
|
'Please use only one instance.'
|
|
);
|
|
}
|
|
glob.Konva = Konva;
|
|
Konva.global = glob;
|
|
|
|
|
|
if( typeof exports === 'object') {
|
|
// runtime-check for browserify and nw.js (node-webkit)
|
|
if(glob.window && glob.window.document) {
|
|
Konva.document = glob.window.document;
|
|
Konva.window = glob.window;
|
|
} else {
|
|
// Node. Does not work with strict CommonJS, but
|
|
// only CommonJS-like enviroments that support module.exports,
|
|
// like Node.
|
|
var Canvas = require('canvas');
|
|
var jsdom = require('jsdom').jsdom;
|
|
|
|
Konva.window = jsdom('<!DOCTYPE html><html><head></head><body></body></html>').defaultView;
|
|
Konva.document = Konva.window.document;
|
|
Konva.window.Image = Canvas.Image;
|
|
Konva._nodeCanvas = Canvas;
|
|
}
|
|
module.exports = Konva;
|
|
return;
|
|
}
|
|
else if( typeof define === 'function' && define.amd) {
|
|
// AMD. Register as an anonymous module.
|
|
define(function() {
|
|
return Konva;
|
|
});
|
|
}
|
|
Konva.document = document;
|
|
Konva.window = window;
|
|
})(typeof window !== 'undefined' ? window : global);
|