plotboilerplate 1.15.0 → 1.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/changelog.md +65 -0
- package/dist/index.esm.js +760 -667
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/plotboilerplate.browser.min.js +1 -1
- package/dist/plotboilerplate.browser.min.js.map +1 -1
- package/dist/plotboilerplate.js +765 -571
- package/dist/plotboilerplate.min.js +1 -1
- package/package.json +1 -1
- package/src/cjs/BezierPath.d.ts +32 -14
- package/src/cjs/BezierPath.js +65 -24
- package/src/cjs/BezierPath.js.map +1 -1
- package/src/cjs/Bounds.d.ts +33 -1
- package/src/cjs/Bounds.js +38 -1
- package/src/cjs/Bounds.js.map +1 -1
- package/src/cjs/Circle.d.ts +15 -13
- package/src/cjs/Circle.js +16 -34
- package/src/cjs/Circle.js.map +1 -1
- package/src/cjs/CircleSector.d.ts +17 -16
- package/src/cjs/CircleSector.js +12 -24
- package/src/cjs/CircleSector.js.map +1 -1
- package/src/cjs/CubicBezierCurve.d.ts +16 -15
- package/src/cjs/CubicBezierCurve.js +57 -94
- package/src/cjs/CubicBezierCurve.js.map +1 -1
- package/src/cjs/KeyHandler.d.ts +10 -3
- package/src/cjs/KeyHandler.js +28 -23
- package/src/cjs/KeyHandler.js.map +1 -1
- package/src/cjs/Line.d.ts +2 -15
- package/src/cjs/Line.js +6 -31
- package/src/cjs/Line.js.map +1 -1
- package/src/cjs/MouseHandler.js +43 -53
- package/src/cjs/MouseHandler.js.map +1 -1
- package/src/cjs/PBImage.d.ts +15 -13
- package/src/cjs/PBImage.js +11 -15
- package/src/cjs/PBImage.js.map +1 -1
- package/src/cjs/PBText.d.ts +13 -12
- package/src/cjs/PBText.js +8 -12
- package/src/cjs/PBText.js.map +1 -1
- package/src/cjs/PlotBoilerplate.d.ts +3 -1
- package/src/cjs/PlotBoilerplate.js +9 -2
- package/src/cjs/PlotBoilerplate.js.map +1 -1
- package/src/cjs/Polygon.d.ts +23 -14
- package/src/cjs/Polygon.js +22 -35
- package/src/cjs/Polygon.js.map +1 -1
- package/src/cjs/Triangle.d.ts +15 -13
- package/src/cjs/Triangle.js +23 -64
- package/src/cjs/Triangle.js.map +1 -1
- package/src/cjs/VEllipse.d.ts +14 -9
- package/src/cjs/VEllipse.js +10 -18
- package/src/cjs/VEllipse.js.map +1 -1
- package/src/cjs/VEllipseSector.d.ts +15 -1
- package/src/cjs/VEllipseSector.js +11 -1
- package/src/cjs/VEllipseSector.js.map +1 -1
- package/src/cjs/Vector.d.ts +3 -15
- package/src/cjs/Vector.js +9 -49
- package/src/cjs/Vector.js.map +1 -1
- package/src/cjs/VertTuple.d.ts +15 -1
- package/src/cjs/VertTuple.js +14 -19
- package/src/cjs/VertTuple.js.map +1 -1
- package/src/cjs/Vertex.d.ts +23 -13
- package/src/cjs/Vertex.js +24 -22
- package/src/cjs/Vertex.js.map +1 -1
- package/src/cjs/VertexListeners.d.ts +12 -8
- package/src/cjs/VertexListeners.js +18 -25
- package/src/cjs/VertexListeners.js.map +1 -1
- package/src/cjs/draw.d.ts +45 -3
- package/src/cjs/draw.js +149 -6
- package/src/cjs/draw.js.map +1 -1
- package/src/cjs/drawgl.d.ts +41 -3
- package/src/cjs/drawgl.js +45 -3
- package/src/cjs/drawgl.js.map +1 -1
- package/src/cjs/drawutilssvg.d.ts +69 -4
- package/src/cjs/drawutilssvg.js +147 -16
- package/src/cjs/drawutilssvg.js.map +1 -1
- package/src/cjs/index.d.ts +0 -1
- package/src/cjs/index.js +0 -1
- package/src/cjs/index.js.map +1 -1
- package/src/cjs/interfaces/DrawLib.d.ts +64 -27
- package/src/cjs/interfaces/DrawLib.js +3 -0
- package/src/cjs/interfaces/DrawLib.js.map +1 -1
- package/src/cjs/interfaces/core.d.ts +2 -12
- package/src/cjs/interfaces/core.js +1 -0
- package/src/cjs/interfaces/core.js.map +1 -1
- package/src/cjs/module.d.ts +1 -3
- package/src/cjs/module.js +2 -5
- package/src/cjs/module.js.map +1 -1
- package/src/cjs/utils/creategui.js +16 -2
- package/src/cjs/utils/datastructures/Color.d.ts +3 -1
- package/src/cjs/utils/datastructures/Color.js +17 -6
- package/src/cjs/utils/datastructures/Color.js.map +1 -1
- package/src/cjs/utils/datastructures/Girih.d.ts +5 -0
- package/src/cjs/utils/datastructures/Girih.js +18 -14
- package/src/cjs/utils/datastructures/Girih.js.map +1 -1
- package/src/cjs/utils/datastructures/GirihPenroseRhombus.js +42 -25
- package/src/cjs/utils/datastructures/GirihPenroseRhombus.js.map +1 -1
- package/src/cjs/utils/datastructures/GirihTile.js +2 -13
- package/src/cjs/utils/datastructures/GirihTile.js.map +1 -1
- package/src/cjs/utils/helpers/BezierPathInteractionHelper.d.ts +17 -1
- package/src/cjs/utils/helpers/BezierPathInteractionHelper.js +50 -32
- package/src/cjs/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
- package/src/cjs/utils/io/DndFileHandler.d.ts +63 -0
- package/src/cjs/utils/io/DndFileHandler.js +112 -0
- package/src/cjs/utils/io/DndFileHandler.js.map +1 -0
- package/src/cjs/utils/io/FileDrop.d.ts +98 -0
- package/src/cjs/utils/io/FileDrop.js +142 -0
- package/src/cjs/utils/io/FileDrop.js.map +1 -0
- package/src/esm/BezierPath.d.ts +32 -14
- package/src/esm/BezierPath.js +65 -24
- package/src/esm/BezierPath.js.map +1 -1
- package/src/esm/Bounds.d.ts +33 -1
- package/src/esm/Bounds.js +38 -1
- package/src/esm/Bounds.js.map +1 -1
- package/src/esm/Circle.d.ts +15 -13
- package/src/esm/Circle.js +16 -34
- package/src/esm/Circle.js.map +1 -1
- package/src/esm/CircleSector.d.ts +17 -16
- package/src/esm/CircleSector.js +12 -24
- package/src/esm/CircleSector.js.map +1 -1
- package/src/esm/CubicBezierCurve.d.ts +16 -15
- package/src/esm/CubicBezierCurve.js +57 -94
- package/src/esm/CubicBezierCurve.js.map +1 -1
- package/src/esm/KeyHandler.d.ts +10 -3
- package/src/esm/KeyHandler.js +28 -23
- package/src/esm/KeyHandler.js.map +1 -1
- package/src/esm/Line.d.ts +2 -15
- package/src/esm/Line.js +6 -31
- package/src/esm/Line.js.map +1 -1
- package/src/esm/MouseHandler.js +43 -53
- package/src/esm/MouseHandler.js.map +1 -1
- package/src/esm/PBImage.d.ts +15 -13
- package/src/esm/PBImage.js +11 -15
- package/src/esm/PBImage.js.map +1 -1
- package/src/esm/PBText.d.ts +13 -12
- package/src/esm/PBText.js +8 -12
- package/src/esm/PBText.js.map +1 -1
- package/src/esm/PlotBoilerplate.d.ts +3 -1
- package/src/esm/PlotBoilerplate.js +9 -2
- package/src/esm/PlotBoilerplate.js.map +1 -1
- package/src/esm/Polygon.d.ts +23 -14
- package/src/esm/Polygon.js +22 -35
- package/src/esm/Polygon.js.map +1 -1
- package/src/esm/Triangle.d.ts +15 -13
- package/src/esm/Triangle.js +23 -64
- package/src/esm/Triangle.js.map +1 -1
- package/src/esm/VEllipse.d.ts +14 -9
- package/src/esm/VEllipse.js +10 -18
- package/src/esm/VEllipse.js.map +1 -1
- package/src/esm/VEllipseSector.d.ts +15 -1
- package/src/esm/VEllipseSector.js +11 -1
- package/src/esm/VEllipseSector.js.map +1 -1
- package/src/esm/Vector.d.ts +3 -15
- package/src/esm/Vector.js +9 -49
- package/src/esm/Vector.js.map +1 -1
- package/src/esm/VertTuple.d.ts +15 -1
- package/src/esm/VertTuple.js +14 -19
- package/src/esm/VertTuple.js.map +1 -1
- package/src/esm/Vertex.d.ts +23 -13
- package/src/esm/Vertex.js +24 -22
- package/src/esm/Vertex.js.map +1 -1
- package/src/esm/VertexListeners.d.ts +12 -8
- package/src/esm/VertexListeners.js +18 -25
- package/src/esm/VertexListeners.js.map +1 -1
- package/src/esm/draw.d.ts +45 -3
- package/src/esm/draw.js +147 -6
- package/src/esm/draw.js.map +1 -1
- package/src/esm/drawgl.d.ts +41 -3
- package/src/esm/drawgl.js +44 -3
- package/src/esm/drawgl.js.map +1 -1
- package/src/esm/drawutilssvg.d.ts +69 -4
- package/src/esm/drawutilssvg.js +146 -16
- package/src/esm/drawutilssvg.js.map +1 -1
- package/src/esm/index.d.ts +0 -1
- package/src/esm/index.js +0 -1
- package/src/esm/index.js.map +1 -1
- package/src/esm/interfaces/DrawLib.d.ts +64 -27
- package/src/esm/interfaces/DrawLib.js +3 -0
- package/src/esm/interfaces/DrawLib.js.map +1 -1
- package/src/esm/interfaces/core.d.ts +2 -12
- package/src/esm/interfaces/core.js +1 -0
- package/src/esm/interfaces/core.js.map +1 -1
- package/src/esm/module.d.ts +1 -3
- package/src/esm/module.js +5 -4
- package/src/esm/module.js.map +1 -1
- package/src/esm/utils/datastructures/Color.d.ts +3 -1
- package/src/esm/utils/datastructures/Color.js +17 -6
- package/src/esm/utils/datastructures/Color.js.map +1 -1
- package/src/esm/utils/datastructures/Girih.d.ts +5 -0
- package/src/esm/utils/datastructures/Girih.js +18 -14
- package/src/esm/utils/datastructures/Girih.js.map +1 -1
- package/src/esm/utils/datastructures/GirihPenroseRhombus.js +42 -25
- package/src/esm/utils/datastructures/GirihPenroseRhombus.js.map +1 -1
- package/src/esm/utils/datastructures/GirihTile.js +2 -13
- package/src/esm/utils/datastructures/GirihTile.js.map +1 -1
- package/src/esm/utils/helpers/BezierPathInteractionHelper.d.ts +17 -1
- package/src/esm/utils/helpers/BezierPathInteractionHelper.js +49 -32
- package/src/esm/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
- package/src/esm/utils/io/DndFileHandler.d.ts +63 -0
- package/src/esm/utils/io/DndFileHandler.js +106 -0
- package/src/esm/utils/io/DndFileHandler.js.map +1 -0
- package/src/esm/utils/io/FileDrop.d.ts +98 -0
- package/src/esm/utils/io/FileDrop.js +136 -0
- package/src/esm/utils/io/FileDrop.js.map +1 -0
- package/src/ts/BezierPath.ts +78 -22
- package/src/ts/Bounds.ts +40 -1
- package/src/ts/Circle.ts +187 -200
- package/src/ts/CircleSector.ts +141 -141
- package/src/ts/CubicBezierCurve.ts +738 -810
- package/src/ts/KeyHandler.ts +221 -212
- package/src/ts/Line.ts +48 -80
- package/src/ts/MouseHandler.ts +356 -368
- package/src/ts/PBImage.ts +67 -69
- package/src/ts/PBText.ts +16 -14
- package/src/ts/PlotBoilerplate.ts +9 -2
- package/src/ts/Polygon.ts +34 -34
- package/src/ts/Triangle.ts +444 -489
- package/src/ts/VEllipse.ts +18 -17
- package/src/ts/VEllipseSector.ts +20 -1
- package/src/ts/Vector.ts +124 -160
- package/src/ts/VertTuple.ts +333 -338
- package/src/ts/Vertex.ts +35 -21
- package/src/ts/VertexListeners.ts +248 -266
- package/src/ts/draw.ts +201 -6
- package/src/ts/drawgl.ts +47 -3
- package/src/ts/drawutilssvg.ts +187 -16
- package/src/ts/index.ts +0 -1
- package/src/ts/interfaces/DrawLib.ts +72 -27
- package/src/ts/interfaces/core.ts +3 -13
- package/src/ts/module.ts +2 -4
- package/src/ts/utils/datastructures/Color.ts +17 -6
- package/src/ts/utils/datastructures/Girih.ts +204 -209
- package/src/ts/utils/datastructures/GirihPenroseRhombus.ts +170 -152
- package/src/ts/utils/datastructures/GirihTile.ts +394 -419
- package/src/ts/utils/helpers/BezierPathInteractionHelper.ts +56 -34
- package/src/ts/utils/io/FileDrop.ts +178 -0
- package/src/ts/SVGBuilder.ts +0 -109
package/src/ts/KeyHandler.ts
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* @modified 2020-07-28 Changed the `delete` key code from 8 to 46.
|
|
6
6
|
* @modified 2020-10-04 Changed `window` to `globalThis`.
|
|
7
7
|
* @modified 2020-10-04 Added extended JSDoc.
|
|
8
|
-
* @
|
|
8
|
+
* @modified 2022-02-02 Added the `destroy` method.
|
|
9
|
+
* @version 1.1.0
|
|
9
10
|
*
|
|
10
11
|
* @file KeyHandler
|
|
11
12
|
* @public
|
|
@@ -29,138 +30,129 @@
|
|
|
29
30
|
* ;
|
|
30
31
|
*/
|
|
31
32
|
export class KeyHandler {
|
|
33
|
+
private element: HTMLElement | Window | typeof globalThis;
|
|
34
|
+
private downListeners: Array<any> = [];
|
|
35
|
+
private pressListeners: Array<any> = [];
|
|
36
|
+
private upListeners: Array<any> = [];
|
|
37
|
+
private keyStates: Record<number, string | undefined> = {};
|
|
32
38
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
private trackAllKeys: boolean;
|
|
40
|
+
// For later retrieval
|
|
41
|
+
private _keyDownListener: (e: KeyboardEvent) => void;
|
|
42
|
+
private _keyPressListener: (e: KeyboardEvent) => void;
|
|
43
|
+
private _keyUpListener: (e: KeyboardEvent) => void;
|
|
38
44
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
/**
|
|
46
|
+
* The constructor.
|
|
47
|
+
*
|
|
48
|
+
* @constructor
|
|
49
|
+
* @instance
|
|
50
|
+
* @memberof KeyHandler
|
|
51
|
+
* @param {HTMLElement} options.element (optional) The HTML element to listen on; if null then 'window' will be used.
|
|
52
|
+
* @param {boolean} options.trackAll (optional) Set to true if you want to keep track of _all_ keys (keyStatus).
|
|
53
|
+
**/
|
|
54
|
+
constructor(options: { element?: HTMLElement | Window | typeof globalThis; trackAll?: boolean }) {
|
|
55
|
+
options = options || {};
|
|
56
|
+
this.element = options.element ? options.element : globalThis;
|
|
57
|
+
this.downListeners = [];
|
|
58
|
+
this.pressListeners = [];
|
|
59
|
+
this.upListeners = [];
|
|
60
|
+
this.keyStates = [];
|
|
61
|
+
// This could be made configurable in a later version. It allows to
|
|
62
|
+
// keep track of the key status no matter if there are any listeners
|
|
63
|
+
// on the key or not.
|
|
64
|
+
this.trackAllKeys = options.trackAll || false;
|
|
65
|
+
// Install the listeners
|
|
66
|
+
this.installListeners();
|
|
67
|
+
}
|
|
44
68
|
|
|
69
|
+
/**
|
|
70
|
+
* A helper function to fire key events from this KeyHandler.
|
|
71
|
+
*
|
|
72
|
+
* @param {KeyboardEvent} event - The key event to fire.
|
|
73
|
+
* @param {Array<XKeyListener>} listener - The listeners to fire to.
|
|
74
|
+
*/
|
|
75
|
+
private fireEvent(event: KeyboardEvent, listeners: Array<XKeyListener>): boolean {
|
|
76
|
+
let hasListener: boolean = false;
|
|
77
|
+
for (var i in listeners) {
|
|
78
|
+
var lis: XKeyListener = listeners[i];
|
|
79
|
+
if (lis.keyCode != event.keyCode) continue;
|
|
80
|
+
lis.listener(event);
|
|
81
|
+
hasListener = true;
|
|
82
|
+
}
|
|
83
|
+
return hasListener;
|
|
84
|
+
}
|
|
45
85
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// on the key or not.
|
|
65
|
-
this.trackAllKeys = options.trackAll || false;
|
|
66
|
-
// Install the listeners
|
|
67
|
-
this.installListeners();
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* A helper function to fire key events from this KeyHandler.
|
|
73
|
-
*
|
|
74
|
-
* @param {KeyboardEvent} event - The key event to fire.
|
|
75
|
-
* @param {Array<XKeyListener>} listener - The listeners to fire to.
|
|
76
|
-
*/
|
|
77
|
-
private fireEvent( event : KeyboardEvent, listeners:Array<XKeyListener> ) : boolean {
|
|
78
|
-
let hasListener : boolean = false;
|
|
79
|
-
for( var i in listeners ) {
|
|
80
|
-
var lis : XKeyListener = listeners[i];
|
|
81
|
-
if( lis.keyCode != event.keyCode )
|
|
82
|
-
continue;
|
|
83
|
-
lis.listener(event);
|
|
84
|
-
hasListener = true;
|
|
85
|
-
}
|
|
86
|
-
return hasListener;
|
|
87
|
-
};
|
|
86
|
+
/**
|
|
87
|
+
* Internal function to fire a new keydown event to all listeners.
|
|
88
|
+
* You should not call this function on your own unless you know what you do.
|
|
89
|
+
*
|
|
90
|
+
* @name fireDownEvent
|
|
91
|
+
* @memberof KeyHandler
|
|
92
|
+
* @instance
|
|
93
|
+
* @private
|
|
94
|
+
* @param {KeyboardEvent} e
|
|
95
|
+
* @param {KeyHandler} handler
|
|
96
|
+
* @return {void}
|
|
97
|
+
*/
|
|
98
|
+
private fireDownEvent(e: KeyboardEvent, handler: KeyHandler): void {
|
|
99
|
+
if (handler.fireEvent(e, handler.downListeners) || handler.trackAllKeys) {
|
|
100
|
+
// Down event has listeners. Update key state.
|
|
101
|
+
handler.keyStates[e.keyCode] = "down";
|
|
102
|
+
}
|
|
103
|
+
}
|
|
88
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Internal function to fire a new keypress event to all listeners.
|
|
107
|
+
* You should not call this function on your own unless you know what you do.
|
|
108
|
+
*
|
|
109
|
+
* @name firePressEvent
|
|
110
|
+
* @memberof KeyHandler
|
|
111
|
+
* @instance
|
|
112
|
+
* @private
|
|
113
|
+
* @param {KeyboardEvent} e
|
|
114
|
+
* @param {KeyHandler} handler
|
|
115
|
+
* @return void
|
|
116
|
+
*/
|
|
117
|
+
private firePressEvent(e: KeyboardEvent, handler: KeyHandler): void {
|
|
118
|
+
handler.fireEvent(e, handler.pressListeners);
|
|
119
|
+
}
|
|
89
120
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Internal function to fire a new keypress event to all listeners.
|
|
111
|
-
* You should not call this function on your own unless you know what you do.
|
|
112
|
-
*
|
|
113
|
-
* @name firePressEvent
|
|
114
|
-
* @memberof KeyHandler
|
|
115
|
-
* @instance
|
|
116
|
-
* @private
|
|
117
|
-
* @param {KeyboardEvent} e
|
|
118
|
-
* @param {KeyHandler} handler
|
|
119
|
-
* @return void
|
|
120
|
-
*/
|
|
121
|
-
private firePressEvent(e:KeyboardEvent, handler:KeyHandler) : void {
|
|
122
|
-
handler.fireEvent(e,handler.pressListeners);
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Internal function to fire a new keyup event to all listeners.
|
|
127
|
-
* You should not call this function on your own unless you know what you do.
|
|
128
|
-
*
|
|
129
|
-
* @name fireUpEvent
|
|
130
|
-
* @memberof KeyHandler
|
|
131
|
-
* @instance
|
|
132
|
-
* @private
|
|
133
|
-
* @param {KeyboardEvent} e
|
|
134
|
-
* @param {KeyHandler} handler
|
|
135
|
-
* @return {void}
|
|
136
|
-
*/
|
|
137
|
-
private fireUpEvent(e:KeyboardEvent, handler:KeyHandler) : void {
|
|
138
|
-
if( handler.fireEvent(e,handler.upListeners) || handler.trackAllKeys ) {
|
|
139
|
-
// Up event has listeners. Clear key state.
|
|
140
|
-
delete handler.keyStates[e.keyCode];
|
|
141
|
-
}
|
|
142
|
-
};
|
|
121
|
+
/**
|
|
122
|
+
* Internal function to fire a new keyup event to all listeners.
|
|
123
|
+
* You should not call this function on your own unless you know what you do.
|
|
124
|
+
*
|
|
125
|
+
* @name fireUpEvent
|
|
126
|
+
* @memberof KeyHandler
|
|
127
|
+
* @instance
|
|
128
|
+
* @private
|
|
129
|
+
* @param {KeyboardEvent} e
|
|
130
|
+
* @param {KeyHandler} handler
|
|
131
|
+
* @return {void}
|
|
132
|
+
*/
|
|
133
|
+
private fireUpEvent(e: KeyboardEvent, handler: KeyHandler): void {
|
|
134
|
+
if (handler.fireEvent(e, handler.upListeners) || handler.trackAllKeys) {
|
|
135
|
+
// Up event has listeners. Clear key state.
|
|
136
|
+
delete handler.keyStates[e.keyCode];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
143
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Resolve the key/name code.
|
|
142
|
+
*/
|
|
143
|
+
static key2code(key: number | string): number {
|
|
144
|
+
if (typeof key == "number") return key;
|
|
145
|
+
if (typeof key != "string") throw "Unknown key name or key type (should be a string or integer): " + key;
|
|
146
|
+
if (KeyHandler.KEY_CODES[key]) return KeyHandler.KEY_CODES[key];
|
|
147
|
+
throw "Unknown key (cannot resolve key code): " + key;
|
|
148
|
+
}
|
|
144
149
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if( typeof key != 'string' )
|
|
152
|
-
throw "Unknown key name or key type (should be a string or integer): " + key;
|
|
153
|
-
if( KeyHandler.KEY_CODES[key] )
|
|
154
|
-
return KeyHandler.KEY_CODES[key];
|
|
155
|
-
throw "Unknown key (cannot resolve key code): " + key;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Source:
|
|
161
|
-
* https://keycode.info/
|
|
162
|
-
*/
|
|
163
|
-
private static KEY_CODES: Record<string,number> = {
|
|
150
|
+
/**
|
|
151
|
+
* Source:
|
|
152
|
+
* https://keycode.info/
|
|
153
|
+
*/
|
|
154
|
+
// prettier-ignore
|
|
155
|
+
private static KEY_CODES: Record<string,number> = {
|
|
164
156
|
'break' : 3, // alternate: 19
|
|
165
157
|
'backspace' : 8,
|
|
166
158
|
// 'delete' : 8, // alternate: 46
|
|
@@ -353,101 +345,118 @@ export class KeyHandler {
|
|
|
353
345
|
'toggletouchpad' : 255
|
|
354
346
|
};
|
|
355
347
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
348
|
+
/**
|
|
349
|
+
* Install the required listeners into the initially passed element.
|
|
350
|
+
*
|
|
351
|
+
* By default the listeners are installed into the root element specified on
|
|
352
|
+
* construction (or 'window').
|
|
353
|
+
*/
|
|
354
|
+
installListeners() {
|
|
355
|
+
var _self = this;
|
|
356
|
+
this.element.addEventListener(
|
|
357
|
+
"keydown",
|
|
358
|
+
(this._keyDownListener = (e: KeyboardEvent) => {
|
|
359
|
+
_self.fireDownEvent(e, _self);
|
|
360
|
+
}) as EventListener
|
|
361
|
+
);
|
|
362
|
+
this.element.addEventListener(
|
|
363
|
+
"keypress",
|
|
364
|
+
(this._keyPressListener = (e: KeyboardEvent) => {
|
|
365
|
+
_self.firePressEvent(e, _self);
|
|
366
|
+
}) as EventListener
|
|
367
|
+
);
|
|
368
|
+
this.element.addEventListener(
|
|
369
|
+
"keyup",
|
|
370
|
+
(this._keyUpListener = (e: KeyboardEvent) => {
|
|
371
|
+
_self.fireUpEvent(e, _self);
|
|
372
|
+
}) as EventListener
|
|
373
|
+
);
|
|
374
|
+
}
|
|
379
375
|
|
|
376
|
+
/**
|
|
377
|
+
* Remove all installed event listeners from the underlying element.
|
|
378
|
+
*/
|
|
379
|
+
releaseListeners() {
|
|
380
|
+
this.element.removeEventListener("keydown", this._keyDownListener as EventListener);
|
|
381
|
+
this.element.removeEventListener("keypress", this._keyPressListener as EventListener);
|
|
382
|
+
this.element.removeEventListener("keyup", this._keyUpListener as EventListener);
|
|
383
|
+
}
|
|
380
384
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
385
|
+
/**
|
|
386
|
+
* Listen for key down. This function allows chaining.
|
|
387
|
+
*
|
|
388
|
+
* Example: new KeyHandler().down('enter',function() {console.log('Enter hit.')});
|
|
389
|
+
*
|
|
390
|
+
* @name down
|
|
391
|
+
* @memberof KeyHandler
|
|
392
|
+
* @instance
|
|
393
|
+
* @param {string|number} key - Any key identifier, key code or one from the KEY_CODES list.
|
|
394
|
+
* @param {(e:KeyboardEvent)=>void} e - The callback to be triggered.
|
|
395
|
+
* @return {KeyHandler} this
|
|
396
|
+
*/
|
|
397
|
+
down(key: string | number, listener: XKeyCallback): KeyHandler {
|
|
398
|
+
this.downListeners.push({ key: key, keyCode: KeyHandler.key2code(key), listener: listener } as XKeyListener);
|
|
399
|
+
return this;
|
|
400
|
+
}
|
|
397
401
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
402
|
+
/**
|
|
403
|
+
* Listen for key press.
|
|
404
|
+
*
|
|
405
|
+
* Example: new KeyHandler().press('enter',function() {console.log('Enter pressed.')});
|
|
406
|
+
*
|
|
407
|
+
* @name press
|
|
408
|
+
* @memberof KeyHandler
|
|
409
|
+
* @instance
|
|
410
|
+
* @param {string|number} key - Any key identifier, key code or one from the KEY_CODES list.
|
|
411
|
+
* @param {(e:KeyboardEvent)=>void} listener - The callback to be triggered.
|
|
412
|
+
* @return {KeyHandler} this
|
|
413
|
+
*/
|
|
414
|
+
press(key: string | number, listener: XKeyCallback): KeyHandler {
|
|
415
|
+
this.pressListeners.push({ key: key, keyCode: KeyHandler.key2code(key), listener: listener } as XKeyListener);
|
|
416
|
+
return this;
|
|
417
|
+
}
|
|
414
418
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
419
|
+
/**
|
|
420
|
+
* Listen for key up.
|
|
421
|
+
*
|
|
422
|
+
* Example: new KeyHandler().up('enter',function() {console.log('Enter released.')});
|
|
423
|
+
*
|
|
424
|
+
* @name up
|
|
425
|
+
* @memberof KeyHandler
|
|
426
|
+
* @instance
|
|
427
|
+
* @param {string} key - Any key identifier, key code or one from the KEY_CODES list.
|
|
428
|
+
* @param {(e:KeyboardEvent)=>void)} e - The callback to be triggered.
|
|
429
|
+
* @return {KeyHandler} this
|
|
430
|
+
*/
|
|
431
|
+
up(key: string, listener: XKeyCallback): KeyHandler {
|
|
432
|
+
this.upListeners.push({ key: key, keyCode: KeyHandler.key2code(key), listener: listener } as XKeyListener);
|
|
433
|
+
return this;
|
|
434
|
+
}
|
|
431
435
|
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
return this.keyStates[ KeyHandler.key2code(key) ] ? true : false;
|
|
442
|
-
}
|
|
436
|
+
/**
|
|
437
|
+
* Check if a specific key is currently held pressed.
|
|
438
|
+
*
|
|
439
|
+
* @param {string|number} key - Any key identifier, key code or one from the KEY_CODES list.
|
|
440
|
+
*/
|
|
441
|
+
isDown(key: string | number): boolean {
|
|
442
|
+
if (typeof key == "number") return this.keyStates[key] ? true : false;
|
|
443
|
+
else return this.keyStates[KeyHandler.key2code(key)] ? true : false;
|
|
444
|
+
}
|
|
443
445
|
|
|
446
|
+
/**
|
|
447
|
+
* This function should invalidate any installed listeners and invalidate this object.
|
|
448
|
+
* After calling this function the object might not hold valid data any more and
|
|
449
|
+
* should not be used any more.
|
|
450
|
+
*/
|
|
451
|
+
destroy(): void {
|
|
452
|
+
this.releaseListeners();
|
|
453
|
+
}
|
|
444
454
|
}
|
|
445
455
|
|
|
446
|
-
|
|
447
456
|
export interface XKeyListener {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
457
|
+
key: string;
|
|
458
|
+
keyCode: number;
|
|
459
|
+
listener: (event: KeyboardEvent) => void;
|
|
451
460
|
}
|
|
452
461
|
|
|
453
|
-
export type XKeyCallback = (e:KeyboardEvent)=>void;
|
|
462
|
+
export type XKeyCallback = (e: KeyboardEvent) => void;
|
package/src/ts/Line.ts
CHANGED
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
* @modified 2020-03-16 The Line.angle(Line) parameter is now optional. The baseline (x-axis) will be used if not defined.
|
|
13
13
|
* @modified 2020-03-23 Ported to Typescript from JS.
|
|
14
14
|
* @modified 2020-12-04 The `intersection` function returns undefined if both lines are parallel.
|
|
15
|
-
* @
|
|
15
|
+
* @modified 2022-02-02 Added the `destroy` method.
|
|
16
|
+
* @version 2.2.0
|
|
16
17
|
*
|
|
17
18
|
* @file Line
|
|
18
19
|
* @public
|
|
@@ -20,8 +21,7 @@
|
|
|
20
21
|
|
|
21
22
|
import { VertTuple } from "./VertTuple";
|
|
22
23
|
import { Vertex } from "./Vertex";
|
|
23
|
-
import { SVGSerializable} from "./interfaces";
|
|
24
|
-
|
|
24
|
+
import { SVGSerializable } from "./interfaces";
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* @classdesc A line consists of two vertices a and b.<br>
|
|
@@ -31,86 +31,54 @@ import { SVGSerializable} from "./interfaces";
|
|
|
31
31
|
*
|
|
32
32
|
* @requires Vertex
|
|
33
33
|
*/
|
|
34
|
-
export class Line
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
export class Line extends VertTuple<Line> implements SVGSerializable {
|
|
35
|
+
/**
|
|
36
|
+
* Required to generate proper CSS classes and other class related IDs.
|
|
37
|
+
**/
|
|
38
|
+
readonly className: string = "Line";
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
* @param {Vertex} a The line's first point.
|
|
50
|
-
* @param {Vertex} b The line's second point.
|
|
51
|
-
**/
|
|
52
|
-
constructor(a:Vertex,b:Vertex) {
|
|
53
|
-
super(a,b,(a:Vertex,b:Vertex)=>new Line(a,b));
|
|
54
|
-
}
|
|
55
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Creates an instance of Line.
|
|
42
|
+
*
|
|
43
|
+
* @constructor
|
|
44
|
+
* @name Line
|
|
45
|
+
* @param {Vertex} a The line's first point.
|
|
46
|
+
* @param {Vertex} b The line's second point.
|
|
47
|
+
**/
|
|
48
|
+
constructor(a: Vertex, b: Vertex) {
|
|
49
|
+
super(a, b, (a: Vertex, b: Vertex) => new Line(a, b));
|
|
50
|
+
}
|
|
56
51
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return null;
|
|
71
|
-
|
|
72
|
-
let a : number = this.a.y - line.a.y;
|
|
73
|
-
let b : number = this.a.x - line.a.x;
|
|
74
|
-
const numerator1 : number = ((line.b.x - line.a.x) * a) - ((line.b.y - line.a.y) * b);
|
|
75
|
-
const numerator2 : number = ((this.b.x - this.a.x) * a) - ((this.b.y - this.a.y) * b);
|
|
76
|
-
a = numerator1 / denominator; // NaN if parallel lines
|
|
77
|
-
b = numerator2 / denominator;
|
|
52
|
+
/**
|
|
53
|
+
* Get the intersection if this line and the specified line.
|
|
54
|
+
*
|
|
55
|
+
* @method intersection
|
|
56
|
+
* @param {Line} line The second line.
|
|
57
|
+
* @return {Vertex|undefined} The intersection (may lie outside the end-points) or `undefined` if both lines are parallel.
|
|
58
|
+
* @instance
|
|
59
|
+
* @memberof Line
|
|
60
|
+
**/
|
|
61
|
+
// !!! DO NOT MOVE TO VertTuple
|
|
62
|
+
intersection(line: Line): Vertex | undefined {
|
|
63
|
+
const denominator: number = this.denominator(line);
|
|
64
|
+
if (denominator == 0) return null;
|
|
78
65
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
66
|
+
let a: number = this.a.y - line.a.y;
|
|
67
|
+
let b: number = this.a.x - line.a.x;
|
|
68
|
+
const numerator1: number = (line.b.x - line.a.x) * a - (line.b.y - line.a.y) * b;
|
|
69
|
+
const numerator2: number = (this.b.x - this.a.x) * a - (this.b.y - this.a.y) * b;
|
|
70
|
+
a = numerator1 / denominator; // NaN if parallel lines
|
|
71
|
+
b = numerator2 / denominator;
|
|
82
72
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// if we cast these lines infinitely in both directions, they intersect here:
|
|
88
|
-
return new Vertex( x, y );
|
|
89
|
-
};
|
|
90
|
-
|
|
73
|
+
// Catch NaN?
|
|
74
|
+
const x: number = this.a.x + a * (this.b.x - this.a.x);
|
|
75
|
+
const y: number = this.a.y + a * (this.b.y - this.a.y);
|
|
91
76
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
*
|
|
95
|
-
* @deprecated DEPRECATION Please use the drawutilssvg library and an XMLSerializer instead.
|
|
96
|
-
* @method toSVGString
|
|
97
|
-
* @param {options} p - A set of options, like the 'classname' to use
|
|
98
|
-
* for the line object.
|
|
99
|
-
* @return {string} The SVG string representing this line.
|
|
100
|
-
* @instance
|
|
101
|
-
* @memberof Line
|
|
102
|
-
**/
|
|
103
|
-
toSVGString( options:{ className?: string } ):string {
|
|
104
|
-
options = options || {};
|
|
105
|
-
var buffer = [];
|
|
106
|
-
buffer.push( '<line' );
|
|
107
|
-
if( options.className )
|
|
108
|
-
buffer.push( ' class="' + options.className + '"' );
|
|
109
|
-
buffer.push( ' x1="' + this.a.x + '"' );
|
|
110
|
-
buffer.push( ' y1="' + this.a.y + '"' );
|
|
111
|
-
buffer.push( ' x2="' + this.b.x + '"' );
|
|
112
|
-
buffer.push( ' y2="' + this.b.y + '"' );
|
|
113
|
-
buffer.push( ' />' );
|
|
114
|
-
return buffer.join('');
|
|
115
|
-
};
|
|
77
|
+
if (isNaN(a) || isNaN(x) || isNaN(y)) {
|
|
78
|
+
return undefined;
|
|
116
79
|
}
|
|
80
|
+
|
|
81
|
+
// if we cast these lines infinitely in both directions, they intersect here:
|
|
82
|
+
return new Vertex(x, y);
|
|
83
|
+
}
|
|
84
|
+
}
|