konva/src/Global.ts

175 lines
4.8 KiB
TypeScript
Raw Normal View History

2019-01-02 04:59:27 +08:00
/*
* Konva JavaScript Framework v@@version
2019-02-24 09:54:20 +08:00
* http://konvajs.org/
2019-01-02 04:59:27 +08:00
* Licensed under the MIT
* Date: @@date
*
* Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
* Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva)
*
* @license
*/
var PI_OVER_180 = Math.PI / 180;
/**
* @namespace Konva
*/
2019-03-07 11:19:32 +08:00
function detectBrowser() {
return (
typeof window !== 'undefined' &&
// browser case
({}.toString.call(window) === '[object Window]' ||
// electron case
{}.toString.call(window) === '[object global]')
);
}
2019-01-02 04:59:27 +08:00
declare const WorkerGlobalScope: any;
export const glob: any =
typeof global !== 'undefined'
? global
: typeof window !== 'undefined'
? window
: typeof WorkerGlobalScope !== 'undefined'
? self
: {};
2019-03-07 11:19:32 +08:00
export const Konva = {
2019-06-08 04:24:52 +08:00
_global: glob,
2019-03-07 11:19:32 +08:00
version: '@@version',
isBrowser: detectBrowser(),
2021-05-05 22:19:24 +08:00
isUnminified: /param/.test(function (param: any) {}.toString()),
2019-03-07 11:19:32 +08:00
dblClickWindow: 400,
2021-05-05 22:19:24 +08:00
getAngle(angle: number) {
2019-03-07 11:19:32 +08:00
return Konva.angleDeg ? angle * PI_OVER_180 : angle;
},
enableTrace: false,
_pointerEventsEnabled: false,
2021-05-04 21:57:03 +08:00
/**
* Should Konva automatically update canvas on any changes. Default is true.
* @property autoDrawEnabled
* @default true
* @name autoDrawEnabled
* @memberof Konva
* @example
* Konva.autoDrawEnabled = true;
*/
autoDrawEnabled: true,
2019-08-04 10:41:57 +08:00
/**
* Should we enable hit detection while dragging? For performance reasons, by default it is false.
* But on some rare cases you want to see hit graph and check intersections. Just set it to true.
* @property hitOnDragEnabled
* @default false
* @name hitOnDragEnabled
* @memberof Konva
* @example
* Konva.hitOnDragEnabled = true;
*/
hitOnDragEnabled: false,
/**
* Should we capture touch events and bind them to the touchstart target? That is how it works on DOM elements.
* The case: we touchstart on div1, then touchmove out of that element into another element div2.
* DOM will continue trigger touchmove events on div1 (not div2). Because events are "captured" into initial target.
* By default Konva do not do that and will trigger touchmove on another element, while pointer is moving.
* @property captureTouchEventsEnabled
* @default false
* @name captureTouchEventsEnabled
* @memberof Konva
* @example
* Konva.captureTouchEventsEnabled = true;
*/
captureTouchEventsEnabled: false,
2019-03-07 11:19:32 +08:00
// TODO: move that to stage?
listenClickTap: false,
inDblClickWindow: false,
/**
* Global pixel ratio configuration. KonvaJS automatically detect pixel ratio of current device.
2020-03-16 21:18:28 +08:00
* But you may override such property, if you want to use your value. Set this value before any components initializations.
2019-03-07 11:19:32 +08:00
* @property pixelRatio
* @default undefined
* @name pixelRatio
* @memberof Konva
* @example
2020-03-16 21:18:28 +08:00
* // before any Konva code:
2019-03-07 11:19:32 +08:00
* Konva.pixelRatio = 1;
*/
2021-04-30 22:24:27 +08:00
pixelRatio: (typeof window !== 'undefined' && window.devicePixelRatio) || 1,
2019-03-07 11:19:32 +08:00
/**
* 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. Default is 3px.
* @property dragDistance
* @default 0
* @memberof Konva
* @example
* Konva.dragDistance = 10;
*/
dragDistance: 3,
/**
2020-02-29 05:38:18 +08:00
* Use degree values for angle properties. You may set this property to false if you want to use radian values.
2019-03-07 11:19:32 +08:00
* @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,
/**
* Configure what mouse buttons can be used for drag and drop.
* Default value is [0] - only left mouse button.
* @property dragButtons
* @default true
* @memberof Konva
* @example
* // enable left and right mouse buttons
* Konva.dragButtons = [0, 2];
*/
dragButtons: [0, 1],
/**
* returns whether or not drag and drop is currently active
* @method
* @memberof Konva
*/
isDragging() {
return Konva['DD'].isDragging;
},
/**
* returns whether or not a drag and drop operation is ready, but may
* not necessarily have started
* @method
* @memberof Konva
*/
isDragReady() {
return !!Konva['DD'].node;
},
// user agent
document: glob.document,
2019-08-08 23:12:18 +08:00
// insert Konva into global namespace (window)
2019-03-07 11:19:32 +08:00
// it is required for npm packages
_injectGlobal(Konva) {
glob.Konva = Konva;
},
2019-01-02 04:59:27 +08:00
};
2019-02-27 21:06:04 +08:00
2021-05-05 22:19:24 +08:00
export const _registerNode = (NodeClass: any) => {
2019-03-07 11:19:32 +08:00
Konva[NodeClass.prototype.getClassName()] = NodeClass;
2019-02-27 21:06:04 +08:00
};
2021-04-30 22:24:27 +08:00
Konva._injectGlobal(Konva);