@teachinglab/omd 0.6.0 → 0.6.2

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.
Files changed (198) hide show
  1. package/README.md +257 -251
  2. package/README.old.md +137 -137
  3. package/canvas/core/canvasConfig.js +202 -202
  4. package/canvas/drawing/segment.js +167 -167
  5. package/canvas/drawing/stroke.js +385 -385
  6. package/canvas/events/eventManager.js +444 -444
  7. package/canvas/events/pointerEventHandler.js +262 -262
  8. package/canvas/index.js +48 -48
  9. package/canvas/tools/PointerTool.js +71 -71
  10. package/canvas/tools/tool.js +222 -222
  11. package/canvas/utils/boundingBox.js +377 -377
  12. package/canvas/utils/mathUtils.js +258 -258
  13. package/docs/api/configuration-options.md +198 -198
  14. package/docs/api/eventManager.md +82 -82
  15. package/docs/api/focusFrameManager.md +144 -144
  16. package/docs/api/index.md +105 -105
  17. package/docs/api/main.md +62 -62
  18. package/docs/api/omdBinaryExpressionNode.md +86 -86
  19. package/docs/api/omdCanvas.md +83 -83
  20. package/docs/api/omdConfigManager.md +112 -112
  21. package/docs/api/omdConstantNode.md +52 -52
  22. package/docs/api/omdDisplay.md +87 -87
  23. package/docs/api/omdEquationNode.md +174 -174
  24. package/docs/api/omdEquationSequenceNode.md +258 -258
  25. package/docs/api/omdEquationStack.md +192 -192
  26. package/docs/api/omdFunctionNode.md +82 -82
  27. package/docs/api/omdGroupNode.md +78 -78
  28. package/docs/api/omdHelpers.md +87 -87
  29. package/docs/api/omdLeafNode.md +85 -85
  30. package/docs/api/omdNode.md +201 -201
  31. package/docs/api/omdOperationDisplayNode.md +117 -117
  32. package/docs/api/omdOperatorNode.md +91 -91
  33. package/docs/api/omdParenthesisNode.md +133 -133
  34. package/docs/api/omdPopup.md +191 -191
  35. package/docs/api/omdPowerNode.md +131 -131
  36. package/docs/api/omdRationalNode.md +144 -144
  37. package/docs/api/omdSequenceNode.md +128 -128
  38. package/docs/api/omdSimplification.md +78 -78
  39. package/docs/api/omdSqrtNode.md +144 -144
  40. package/docs/api/omdStepVisualizer.md +146 -146
  41. package/docs/api/omdStepVisualizerHighlighting.md +65 -65
  42. package/docs/api/omdStepVisualizerInteractiveSteps.md +108 -108
  43. package/docs/api/omdStepVisualizerLayout.md +70 -70
  44. package/docs/api/omdStepVisualizerNodeUtils.md +140 -140
  45. package/docs/api/omdStepVisualizerTextBoxes.md +76 -76
  46. package/docs/api/omdToolbar.md +130 -130
  47. package/docs/api/omdTranscriptionService.md +95 -95
  48. package/docs/api/omdTreeDiff.md +169 -169
  49. package/docs/api/omdUnaryExpressionNode.md +137 -137
  50. package/docs/api/omdUtilities.md +82 -82
  51. package/docs/api/omdVariableNode.md +123 -123
  52. package/docs/api/selectTool.md +74 -74
  53. package/docs/api/simplificationEngine.md +97 -97
  54. package/docs/api/simplificationRules.md +76 -76
  55. package/docs/api/simplificationUtils.md +64 -64
  56. package/docs/api/transcribe.md +43 -43
  57. package/docs/api-reference.md +85 -85
  58. package/docs/index.html +453 -453
  59. package/docs/index.md +38 -38
  60. package/docs/omd-objects.md +258 -258
  61. package/index.js +79 -79
  62. package/jsvg/index.js +3 -0
  63. package/jsvg/jsvg.js +898 -898
  64. package/jsvg/jsvgComponents.js +357 -358
  65. package/npm-docs/DOCUMENTATION_SUMMARY.md +220 -220
  66. package/npm-docs/README.md +251 -251
  67. package/npm-docs/api/api-reference.md +85 -85
  68. package/npm-docs/api/configuration-options.md +198 -198
  69. package/npm-docs/api/eventManager.md +82 -82
  70. package/npm-docs/api/expression-nodes.md +561 -561
  71. package/npm-docs/api/focusFrameManager.md +144 -144
  72. package/npm-docs/api/index.md +105 -105
  73. package/npm-docs/api/main.md +62 -62
  74. package/npm-docs/api/omdBinaryExpressionNode.md +86 -86
  75. package/npm-docs/api/omdCanvas.md +83 -83
  76. package/npm-docs/api/omdConfigManager.md +112 -112
  77. package/npm-docs/api/omdConstantNode.md +52 -52
  78. package/npm-docs/api/omdDisplay.md +87 -87
  79. package/npm-docs/api/omdEquationNode.md +174 -174
  80. package/npm-docs/api/omdEquationSequenceNode.md +258 -258
  81. package/npm-docs/api/omdEquationStack.md +192 -192
  82. package/npm-docs/api/omdFunctionNode.md +82 -82
  83. package/npm-docs/api/omdGroupNode.md +78 -78
  84. package/npm-docs/api/omdHelpers.md +87 -87
  85. package/npm-docs/api/omdLeafNode.md +85 -85
  86. package/npm-docs/api/omdNode.md +201 -201
  87. package/npm-docs/api/omdOperationDisplayNode.md +117 -117
  88. package/npm-docs/api/omdOperatorNode.md +91 -91
  89. package/npm-docs/api/omdParenthesisNode.md +133 -133
  90. package/npm-docs/api/omdPopup.md +191 -191
  91. package/npm-docs/api/omdPowerNode.md +131 -131
  92. package/npm-docs/api/omdRationalNode.md +144 -144
  93. package/npm-docs/api/omdSequenceNode.md +128 -128
  94. package/npm-docs/api/omdSimplification.md +78 -78
  95. package/npm-docs/api/omdSqrtNode.md +144 -144
  96. package/npm-docs/api/omdStepVisualizer.md +146 -146
  97. package/npm-docs/api/omdStepVisualizerHighlighting.md +65 -65
  98. package/npm-docs/api/omdStepVisualizerInteractiveSteps.md +108 -108
  99. package/npm-docs/api/omdStepVisualizerLayout.md +70 -70
  100. package/npm-docs/api/omdStepVisualizerNodeUtils.md +140 -140
  101. package/npm-docs/api/omdStepVisualizerTextBoxes.md +76 -76
  102. package/npm-docs/api/omdToolbar.md +130 -130
  103. package/npm-docs/api/omdTranscriptionService.md +95 -95
  104. package/npm-docs/api/omdTreeDiff.md +169 -169
  105. package/npm-docs/api/omdUnaryExpressionNode.md +137 -137
  106. package/npm-docs/api/omdUtilities.md +82 -82
  107. package/npm-docs/api/omdVariableNode.md +123 -123
  108. package/npm-docs/api/selectTool.md +74 -74
  109. package/npm-docs/api/simplificationEngine.md +97 -97
  110. package/npm-docs/api/simplificationRules.md +76 -76
  111. package/npm-docs/api/simplificationUtils.md +64 -64
  112. package/npm-docs/api/transcribe.md +43 -43
  113. package/npm-docs/guides/equations.md +854 -854
  114. package/npm-docs/guides/factory-functions.md +354 -354
  115. package/npm-docs/guides/getting-started.md +318 -318
  116. package/npm-docs/guides/quick-examples.md +525 -525
  117. package/npm-docs/guides/visualizations.md +682 -682
  118. package/npm-docs/index.html +12 -0
  119. package/npm-docs/json-schemas.md +826 -826
  120. package/omd/config/omdConfigManager.js +279 -267
  121. package/omd/core/index.js +158 -158
  122. package/omd/core/omdEquationStack.js +546 -546
  123. package/omd/core/omdUtilities.js +113 -113
  124. package/omd/display/omdDisplay.js +969 -962
  125. package/omd/display/omdToolbar.js +501 -501
  126. package/omd/nodes/omdBinaryExpressionNode.js +459 -459
  127. package/omd/nodes/omdConstantNode.js +141 -141
  128. package/omd/nodes/omdEquationNode.js +1327 -1327
  129. package/omd/nodes/omdFunctionNode.js +351 -351
  130. package/omd/nodes/omdGroupNode.js +67 -67
  131. package/omd/nodes/omdLeafNode.js +76 -76
  132. package/omd/nodes/omdNode.js +556 -556
  133. package/omd/nodes/omdOperationDisplayNode.js +321 -321
  134. package/omd/nodes/omdOperatorNode.js +108 -108
  135. package/omd/nodes/omdParenthesisNode.js +292 -292
  136. package/omd/nodes/omdPowerNode.js +235 -235
  137. package/omd/nodes/omdRationalNode.js +295 -295
  138. package/omd/nodes/omdSqrtNode.js +307 -307
  139. package/omd/nodes/omdUnaryExpressionNode.js +227 -227
  140. package/omd/nodes/omdVariableNode.js +122 -122
  141. package/omd/simplification/omdSimplification.js +140 -140
  142. package/omd/simplification/omdSimplificationEngine.js +887 -887
  143. package/omd/simplification/package.json +5 -5
  144. package/omd/simplification/rules/binaryRules.js +1037 -1037
  145. package/omd/simplification/rules/functionRules.js +111 -111
  146. package/omd/simplification/rules/index.js +48 -48
  147. package/omd/simplification/rules/parenthesisRules.js +19 -19
  148. package/omd/simplification/rules/powerRules.js +143 -143
  149. package/omd/simplification/rules/rationalRules.js +725 -725
  150. package/omd/simplification/rules/sqrtRules.js +48 -48
  151. package/omd/simplification/rules/unaryRules.js +37 -37
  152. package/omd/simplification/simplificationRules.js +31 -31
  153. package/omd/simplification/simplificationUtils.js +1055 -1055
  154. package/omd/step-visualizer/omdStepVisualizer.js +947 -947
  155. package/omd/step-visualizer/omdStepVisualizerHighlighting.js +246 -246
  156. package/omd/step-visualizer/omdStepVisualizerLayout.js +892 -892
  157. package/omd/step-visualizer/omdStepVisualizerTextBoxes.js +200 -200
  158. package/omd/utils/aiNextEquationStep.js +106 -106
  159. package/omd/utils/omdNodeOverlay.js +638 -638
  160. package/omd/utils/omdPopup.js +1203 -1203
  161. package/omd/utils/omdStepVisualizerInteractiveSteps.js +684 -684
  162. package/omd/utils/omdStepVisualizerNodeUtils.js +267 -267
  163. package/omd/utils/omdTranscriptionService.js +123 -123
  164. package/omd/utils/omdTreeDiff.js +733 -733
  165. package/package.json +59 -56
  166. package/readme.html +184 -120
  167. package/src/index.js +74 -74
  168. package/src/json-schemas.md +576 -576
  169. package/src/omd-json-samples.js +147 -147
  170. package/src/omdApp.js +391 -391
  171. package/src/omdAppCanvas.js +335 -335
  172. package/src/omdBalanceHanger.js +199 -199
  173. package/src/omdColor.js +13 -13
  174. package/src/omdCoordinatePlane.js +541 -541
  175. package/src/omdExpression.js +115 -115
  176. package/src/omdFactory.js +150 -150
  177. package/src/omdFunction.js +114 -114
  178. package/src/omdMetaExpression.js +290 -290
  179. package/src/omdNaturalExpression.js +563 -563
  180. package/src/omdNode.js +383 -383
  181. package/src/omdNumber.js +52 -52
  182. package/src/omdNumberLine.js +114 -112
  183. package/src/omdNumberTile.js +118 -118
  184. package/src/omdOperator.js +72 -72
  185. package/src/omdPowerExpression.js +91 -91
  186. package/src/omdProblem.js +259 -259
  187. package/src/omdRatioChart.js +251 -251
  188. package/src/omdRationalExpression.js +114 -114
  189. package/src/omdSampleData.js +215 -215
  190. package/src/omdShapes.js +512 -512
  191. package/src/omdSpinner.js +151 -151
  192. package/src/omdString.js +49 -49
  193. package/src/omdTable.js +498 -498
  194. package/src/omdTapeDiagram.js +244 -244
  195. package/src/omdTerm.js +91 -91
  196. package/src/omdTileEquation.js +349 -349
  197. package/src/omdUtils.js +84 -84
  198. package/src/omdVariable.js +51 -51
package/jsvg/jsvg.js CHANGED
@@ -1,898 +1,898 @@
1
-
2
-
3
- // ================ jsvgObject ================================= //
4
-
5
- export class jsvgObject
6
- {
7
- constructor()
8
- {
9
- this.name = "";
10
- this.xpos = 0;
11
- this.scale = 1.0;
12
- this.rotation = 0.0;
13
- this.ypos = 0;
14
- this.width = 0;
15
- this.height = 0;
16
- this.opacity = 1.0;
17
- this.visible = true;
18
- this.svgObject = null;
19
- this.childList = [];
20
- this.parent = null;
21
- }
22
-
23
- // set properties
24
-
25
- setName( N )
26
- {
27
- this.name = N;
28
- }
29
-
30
- setPosition( x, y )
31
- {
32
- this.xpos = x;
33
- this.ypos = y;
34
- this.updateTransform();
35
- }
36
-
37
- setScale( s )
38
- {
39
- this.scale = s;
40
- this.updateTransform();
41
- }
42
-
43
- setRotation( r )
44
- {
45
- this.rotation = r;
46
- this.updateTransform();
47
- }
48
-
49
- updateTransform()
50
- {
51
- // Ensure xpos and ypos are valid numbers
52
- const x = isNaN(this.xpos) ? 0 : this.xpos;
53
- const y = isNaN(this.ypos) ? 0 : this.ypos;
54
-
55
- var transformParams = " translate(" + x.toString() + "," + y.toString() + ") "
56
- if ( this.scale != 1.0 )
57
- transformParams += " scale(" + this.scale.toString() + ")";
58
- if ( this.rotation != 0.0 )
59
- transformParams += " rotate(" + this.rotation + ")";
60
- this.svgObject.setAttribute("transform", transformParams );
61
- }
62
-
63
- setWidth( w )
64
- {
65
- this.width = w;
66
- this.svgObject.setAttribute("width", this.width.toString() );
67
- }
68
-
69
- setHeight( h )
70
- {
71
- this.height = h;
72
- this.svgObject.setAttribute("height", this.height.toString() );
73
- }
74
-
75
- setWidthAndHeight( w,h )
76
- {
77
- this.setWidth( w );
78
- this.setHeight( h );
79
- }
80
-
81
- setFillColor( color )
82
- {
83
- this.svgObject.setAttribute( "fill", color );
84
- }
85
-
86
- setStrokeColor( color )
87
- {
88
- this.svgObject.setAttribute( "stroke", color );
89
- }
90
-
91
- setStrokeWidth( sW )
92
- {
93
- this.svgObject.setAttribute( "stroke-width", sW.toString() );
94
- }
95
-
96
- setOpacity( opacity )
97
- {
98
- this.opacity = opacity;
99
- this.svgObject.style.opacity = opacity;
100
- }
101
-
102
- setDropShadow()
103
- {
104
- // this is a generic dropshadow
105
- this.svgObject.style.filter = "drop-shadow(0px 3px 6px rgba(0, 0, 0, 0.4))";
106
- }
107
-
108
- show()
109
- {
110
- this.visible = true;
111
- this.svgObject.style.display = "";
112
- }
113
-
114
- hide()
115
- {
116
- this.visible = false;
117
- this.svgObject.style.display = "none";
118
- }
119
-
120
- // update
121
-
122
- update()
123
- {
124
- if ( ! this.visible )
125
- return;
126
-
127
- for( var C of this.childList )
128
- {
129
- C.update();
130
- }
131
- }
132
-
133
- // child stuff
134
-
135
- addChild( child )
136
- {
137
- if ( this.svgObject && child.svgObject )
138
- {
139
- this.childList.push( child );
140
- this.svgObject.appendChild( child.svgObject );
141
- child.parent = this;
142
- }
143
- }
144
-
145
- getChild( index )
146
- {
147
- return this.childList[index];
148
- }
149
-
150
- replaceChild( newChild, oldChild )
151
- {
152
- // replace the node
153
- this.svgObject.replaceChild( newChild.svgObject, oldChild.svgObject );
154
-
155
- // replace in childList
156
- for ( var i=0; i<this.childList.length; i++ )
157
- {
158
- if ( this.childList[i] == oldChild )
159
- {
160
- this.childList[i] = newChild;
161
- break;
162
- }
163
- }
164
-
165
- oldChild.parent = null;
166
- newChild.parent = this;
167
- }
168
-
169
- getNumberOfChildren()
170
- {
171
- return this.childList.length;
172
- }
173
-
174
- removeChild( C )
175
- {
176
- const index = this.childList.indexOf(C);
177
- this.removeChildByIndex( index );
178
- C.parent = null;
179
- }
180
-
181
- removeChildByIndex( index )
182
- {
183
- if (index >= 0)
184
- {
185
- var C = this.childList[index];
186
- this.childList.splice(index, 1);
187
- this.svgObject.removeChild( C.svgObject );
188
- C.parent = null;
189
- }
190
- }
191
-
192
- removeAllChildren()
193
- {
194
- while( this.svgObject.firstChild )
195
- {
196
- this.svgObject.removeChild( this.svgObject.lastChild );
197
- }
198
- this.childList = [];
199
- }
200
-
201
- sortChildren( sortFunction )
202
- {
203
- this.childList.sort( sortFunction );
204
-
205
- // If the child is a reference to an existing node in the document, appendChild() moves it from its current position to the new positio
206
- this.childList.forEach(child => this.svgObject.appendChild( child.svgObject ));
207
- }
208
-
209
- // set up events
210
- setClickCallback( callback )
211
- {
212
- if ( this.svgObject )
213
- {
214
- var outerThis = this;
215
- this.svgObject.onclick = function() { callback(outerThis) };
216
- this.svgObject.style.cursor = "pointer";
217
- }
218
- }
219
-
220
- enableDragging()
221
- {
222
- this.svgObject.style.cursor = "pointer";
223
-
224
- // on mouse down
225
- var outerThis = this;
226
- this.svgObject.onmousedown = function(E)
227
- {
228
- // console.log("mouse down");
229
- outerThis.lastX = E.clientX;
230
- outerThis.lastY = E.clientY;
231
-
232
- // register on mouse move function (unregister on mouseup)
233
- window.onmousemove = function(E)
234
- {
235
- // console.log("mouse move");
236
- var dX = E.clientX - outerThis.lastX; // this method works if there's no scaling or rotation
237
- var dY = E.clientY - outerThis.lastY;
238
- outerThis.lastX = E.clientX;
239
- outerThis.lastY = E.clientY;
240
- outerThis.setPosition( outerThis.xpos+dX, outerThis.ypos+dY );
241
- }
242
-
243
- // on mouse up
244
- window.onmouseup = function()
245
- {
246
- // console.log("mouse up");
247
- window.onmousemove = null;
248
- }
249
- }
250
- }
251
- }
252
-
253
-
254
- // ================ jsvgContainer ================================= //
255
-
256
- export class jsvgContainer extends jsvgObject
257
- {
258
- constructor()
259
- {
260
- super();
261
-
262
- const svgNS = "http://www.w3.org/2000/svg";
263
- this.svgObject = document.createElementNS(svgNS, "svg");
264
- this.svgObject.setAttribute("x", "0");
265
- this.svgObject.setAttribute("y", "0");
266
- this.svgObject.setAttribute("width", "500");
267
- this.svgObject.setAttribute("height", "500");
268
- this.svgObject.setAttribute("backgroundColor", "blue");
269
- }
270
-
271
- setViewbox( w, h )
272
- {
273
- this.setWidth(w);
274
- this.setHeight(h);
275
- var viewBoxParams = "0 0 " + w.toString() + " " + h.toString();
276
- this.svgObject.setAttribute("viewBox", viewBoxParams );
277
- }
278
- }
279
-
280
- // ================ jsvgGroup ================================= //
281
-
282
- export class jsvgGroup extends jsvgObject
283
- {
284
- constructor()
285
- {
286
- super();
287
-
288
- const svgNS = "http://www.w3.org/2000/svg";
289
- this.svgObject = document.createElementNS(svgNS, "g");
290
- this.svgObject.setAttribute("x", "0");
291
- this.svgObject.setAttribute("y", "0");
292
- this.svgObject.setAttribute("width", "500");
293
- this.svgObject.setAttribute("height", "500");
294
- this.svgObject.setAttribute("viewBox", "0 0 500 500");
295
- }
296
- }
297
-
298
- // ================ jsvgLine ================================= //
299
-
300
- export class jsvgLine extends jsvgObject
301
- {
302
- constructor()
303
- {
304
- super();
305
-
306
- this.x1 = 0;
307
- this.y1 = 0;
308
- this.x2 = 100;
309
- this.y2 = 100;
310
-
311
- const svgNS = "http://www.w3.org/2000/svg";
312
- this.svgObject = document.createElementNS(svgNS, "line");
313
- this.svgObject.setAttribute("x1", "0");
314
- this.svgObject.setAttribute("y1", "0");
315
- this.svgObject.setAttribute("x2", "100");
316
- this.svgObject.setAttribute("y2", "100");
317
-
318
- this.setStrokeColor("black");
319
- this.setStrokeWidth( 1 );
320
-
321
- this.setEndpointA( this.x1, this.y1 );
322
- this.setEndpointB( this.x2, this.y2 );
323
- }
324
-
325
- setEndpointA( x, y )
326
- {
327
- this.x1 = x;
328
- this.y1 = y;
329
- this.svgObject.setAttribute("x1", x.toString() );
330
- this.svgObject.setAttribute("y1", y.toString() );
331
- }
332
-
333
- setEndpointB( x, y )
334
- {
335
- this.x2 = x;
336
- this.y2 = y;
337
- this.svgObject.setAttribute("x2", x.toString() );
338
- this.svgObject.setAttribute("y2", y.toString() );
339
- }
340
-
341
- setEndpoints( x1,y1, x2,y2 )
342
- {
343
- this.setEndpointA( x1,y1 );
344
- this.setEndpointB( x2,y2 );
345
- }
346
- }
347
-
348
- // ================ jsvgRect ================================= //
349
-
350
- export class jsvgRect extends jsvgObject
351
- {
352
- constructor()
353
- {
354
- super();
355
-
356
- this.cornerRadius = 0;
357
-
358
- const svgNS = "http://www.w3.org/2000/svg";
359
- this.svgObject = document.createElementNS(svgNS, "rect");
360
- this.setWidthAndHeight( 100,100 );
361
- this.setFillColor( "white" );
362
- this.setStrokeColor( "black" );
363
- this.setStrokeWidth( 0 );
364
- }
365
-
366
- setCornerRadius( r )
367
- {
368
- this.cornerRadius = r;
369
- this.svgObject.setAttribute( "rx", r.toString() );
370
- this.svgObject.setAttribute( "ry", r.toString() );
371
- }
372
- }
373
-
374
- // ================ jsvgEllipse ================================= //
375
-
376
- export class jsvgEllipse extends jsvgObject
377
- {
378
- constructor()
379
- {
380
- super();
381
-
382
- const svgNS = "http://www.w3.org/2000/svg";
383
- this.svgObject = document.createElementNS(svgNS, "ellipse");
384
-
385
- this.setPosition( 0,0 );
386
- this.setWidthAndHeight( 100,100 );
387
- this.setFillColor( "white" );
388
- this.setStrokeColor( "black" );
389
- this.setStrokeWidth( 0 );
390
- }
391
-
392
- setWidth( w )
393
- {
394
- this.width = w;
395
- this.svgObject.setAttribute("rx", (this.width/2).toString() );
396
- }
397
-
398
- setHeight( h )
399
- {
400
- this.height = h;
401
- this.svgObject.setAttribute("ry", (this.height/2).toString() );
402
- }
403
- }
404
-
405
-
406
- // ================ jsvgArc ================================= //
407
-
408
- export class jsvgArc extends jsvgObject
409
- {
410
- constructor()
411
- {
412
- super();
413
-
414
- const svgNS = "http://www.w3.org/2000/svg";
415
- this.svgObject = document.createElementNS(svgNS, "path");
416
-
417
- this.setFillColor("white");
418
- this.setStrokeColor("black");
419
- this.setStrokeWidth(0);
420
- }
421
-
422
- createPieSlice(radius, startAngle, endAngle)
423
- {
424
- // Convert polar coordinates to Cartesian for SVG arc
425
- function polarToCartesian(radius, angleInDegrees)
426
- {
427
- const angleInRadians = (angleInDegrees - 90) * Math.PI / 180;
428
- return {
429
- x: (radius * Math.cos(angleInRadians)),
430
- y: (radius * Math.sin(angleInRadians))
431
- };
432
- }
433
-
434
- const start = polarToCartesian(radius, endAngle);
435
- const end = polarToCartesian(radius, startAngle);
436
-
437
- const largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
438
-
439
- const d = [
440
- `M ${0} ${0}`, // Move to center
441
- `L ${start.x} ${start.y}`, // Draw line to start of arc
442
- `A ${radius} ${radius} 0 ${largeArcFlag} 0 ${end.x} ${end.y}`, // Draw arc
443
- `Z` // Close path to center
444
- ].join(" ");
445
-
446
- this.svgObject.setAttribute("d", d);
447
- }
448
- }
449
-
450
- // ================ jsvgPath ================================= //
451
-
452
- export class jsvgPath extends jsvgObject
453
- {
454
- constructor()
455
- {
456
- super();
457
-
458
- // Create the path element and add it to the SVG element
459
- this.path = document.createElementNS("http://www.w3.org/2000/svg", "path");
460
- this.path.setAttribute("fill", "none"); // Default fill is none
461
- this.path.setAttribute("stroke", "black"); // Default stroke is black
462
- this.path.setAttribute("stroke-width", "2"); // Default stroke width
463
- // this.svgElement.appendChild(this.path);
464
-
465
- this.svgObject = this.path;
466
-
467
- // Initialize the points array
468
- this.points = [];
469
-
470
- // Update the path's 'd' attribute
471
- this.updatePath();
472
- }
473
-
474
- addPoint(x, y)
475
- {
476
- if (typeof x !== "number" || typeof y !== "number")
477
- {
478
- throw new Error("Coordinates must be numbers.");
479
- }
480
-
481
- this.points.push({ x:x, y:y });
482
-
483
- // must call updatePath to see new points
484
- }
485
-
486
- updatePath()
487
- {
488
- if (this.points.length === 0)
489
- {
490
- this.path.setAttribute("d", "");
491
- return;
492
- }
493
-
494
- // Create the 'd' attribute for the path
495
- const d = this.points
496
- .map((point, index) => (index === 0 ? `M ${point.x},${point.y}` : `L ${point.x},${point.y}`))
497
- .join(" ");
498
-
499
- // Close the polygon if it has at least three points
500
- const pathData = d;
501
-
502
- this.path.setAttribute("d", pathData);
503
- }
504
-
505
- clearPoints()
506
- {
507
- this.points = [];
508
- this.updatePath();
509
- }
510
- }
511
-
512
-
513
- // ================ jsvgClipMask ================================= //
514
-
515
- export class jsvgClipMask extends jsvgObject
516
- {
517
- constructor( width=300, height=300, cornerRadius=10 )
518
- {
519
- super();
520
-
521
- // make a new svg group
522
- const svgNamespace = "http://www.w3.org/2000/svg";
523
- const svg = document.createElementNS(svgNamespace, "g");
524
- this.svgObject = svg;
525
-
526
- this.width = width;
527
- this.height = height;
528
- this.cornerRadius = cornerRadius;
529
-
530
- this.maskID = "clipMaskID" + Math.random();
531
-
532
- svg.setAttribute("width", this.width);
533
- svg.setAttribute("height", this.height);
534
- svg.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
535
-
536
- // Create a clipPath to apply rounded corners
537
- const clipPath = document.createElementNS(svgNamespace, "clipPath");
538
- clipPath.setAttribute("id", this.maskID );
539
-
540
- this.clipRect = document.createElementNS(svgNamespace, "rect");
541
- this.clipRect.setAttribute("x", 0);
542
- this.clipRect.setAttribute("y", 0);
543
- this.clipRect.setAttribute("width", this.width);
544
- this.clipRect.setAttribute("height", this.height);
545
- this.clipRect.setAttribute("rx", this.cornerRadius); // Rounded corner radius
546
- this.clipRect.setAttribute("ry", this.cornerRadius);
547
-
548
- clipPath.appendChild(this.clipRect);
549
- svg.appendChild(clipPath);
550
-
551
- // we create an inner group that gets masked
552
- // all children get added this group
553
- this.clipGroup = new jsvgGroup();
554
- this.clipGroup.svgObject.setAttribute("clip-path", `url(#${this.maskID})`);
555
- super.addChild( this.clipGroup ); // use super to avoid recursion
556
- }
557
-
558
- setWidth( width )
559
- {
560
- this.width = width;
561
-
562
- this.svgObject.setAttribute("width", this.width);
563
- this.svgObject.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
564
- this.clipRect.setAttribute("width", this.width);
565
- }
566
-
567
- setHeight( height )
568
- {
569
- this.height = height;
570
-
571
- this.svgObject.setAttribute("height", this.height);
572
- this.svgObject.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
573
- this.clipRect.setAttribute("height", this.height);
574
- }
575
-
576
- // setWidthAndHeight( width, height )
577
- // {
578
- // this.width = width;
579
- // this.height = height;
580
-
581
- // this.svgObject.setAttribute("width", this.width);
582
- // this.svgObject.setAttribute("height", this.height);
583
- // this.svgObject.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
584
-
585
- // this.clipRect.setAttribute("width", this.width);
586
- // this.clipRect.setAttribute("height", this.height);
587
- // }
588
-
589
- setCornerRadius( cornerRadius )
590
- {
591
- this.cornerRadius = cornerRadius;
592
- this.clipRect.setAttribute("rx", this.cornerRadius); // Rounded corner radius
593
- this.clipRect.setAttribute("ry", this.cornerRadius);
594
- }
595
-
596
- addChild( obj )
597
- {
598
- // add to masked clip group
599
- this.clipGroup.addChild( obj );
600
- }
601
- }
602
-
603
-
604
-
605
-
606
- // ================ jsvgImage ================================= //
607
-
608
- export class jsvgImage extends jsvgObject
609
- {
610
- constructor()
611
- {
612
- super();
613
-
614
- this.preserveAspectRatio = true;
615
-
616
- const svgNS = "http://www.w3.org/2000/svg";
617
- this.svgObject = document.createElementNS(svgNS, "image");
618
- this.setWidthAndHeight( 100,100 );
619
- }
620
-
621
- setImageURL( newURL )
622
- {
623
- this.svgObject.setAttribute("href", newURL );
624
- this.imageURL = newURL;
625
- }
626
-
627
- setAspectRatio( svgAspectRatio )
628
- {
629
- // to fill the whole space use "xMidYMid slice"
630
- this.svgObject.setAttribute("preserveAspectRatio", svgAspectRatio );
631
- }
632
-
633
- applyRoundedCorners( cornerRadius, svgAspectRatio = 0 )
634
- {
635
- // remove the original image
636
- if ( this.svgObject )
637
- this.svgObject.remove();
638
-
639
- // make a new svg image
640
- const svgNamespace = "http://www.w3.org/2000/svg";
641
- const svg = document.createElementNS(svgNamespace, "g");
642
- this.svgObject = svg;
643
-
644
- svg.setAttribute("width", this.width);
645
- svg.setAttribute("height", this.height);
646
- svg.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
647
-
648
- // Create a clipPath to apply rounded corners
649
- const clipPath = document.createElementNS(svgNamespace, "clipPath");
650
- clipPath.setAttribute("id", "rounded-corners");
651
-
652
- const rect = document.createElementNS(svgNamespace, "rect");
653
- rect.setAttribute("x", 0);
654
- rect.setAttribute("y", 0);
655
- rect.setAttribute("width", this.width);
656
- rect.setAttribute("height", this.height);
657
- rect.setAttribute("rx", cornerRadius); // Rounded corner radius
658
- rect.setAttribute("ry", cornerRadius);
659
-
660
- clipPath.appendChild(rect);
661
- svg.appendChild(clipPath);
662
-
663
- // Create the image element
664
- const image = document.createElementNS(svgNamespace, "image");
665
- image.setAttribute("href", this.imageURL );
666
- image.setAttribute("width", this.width);
667
- image.setAttribute("height", this.height);
668
- image.setAttribute("clip-path", "url(#rounded-corners)");
669
- if ( svgAspectRatio )
670
- image.setAttribute("preserveAspectRatio", svgAspectRatio );
671
-
672
- svg.appendChild(image);
673
- }
674
- }
675
-
676
-
677
- // ================ jsvgTextLine ================================= //
678
-
679
- export class jsvgTextLine extends jsvgObject
680
- {
681
- constructor()
682
- {
683
- super();
684
-
685
- this.text = "";
686
-
687
- const svgNS = "http://www.w3.org/2000/svg";
688
- this.svgObject = document.createElementNS(svgNS, "text");
689
- this.svgObject.textContent = "hello world";
690
- this.svgObject.setAttribute("x", "0"); // x-coordinate of the rectangle
691
- this.svgObject.setAttribute("y", "0"); // y-coordinate of the rectangle
692
- this.svgObject.setAttribute("fill", "black"); // fill color of the rectangle
693
- this.svgObject.style.fontFamily = "Arial, Helvetica, sans-serif;";
694
- }
695
-
696
- setText( text ) { this.text = text; this.svgObject.textContent = text; }
697
- getText() { return this.text; }
698
-
699
- setFontSize( S ) { this.svgObject.style.fontSize = S.toString() + 'px'; }
700
-
701
- setFontFamily( F ) { this.svgObject.style.fontFamily = F; }
702
-
703
- setFontColor( C ) { this.svgObject.style.fill = C; }
704
-
705
- setTextAnchor( A )
706
- {
707
- // left, middle or right
708
- this.svgObject.setAttribute("text-anchor", A ); // fill color of the rectangle
709
- }
710
-
711
- setAlignment( A )
712
- {
713
- this.alignment = A;
714
- if ( A == 'left' )
715
- this.setTextAnchor( 'start' );
716
- if ( A == 'center' )
717
- this.setTextAnchor( 'middle' );
718
- if ( A == 'right' )
719
- this.setTextAnchor( 'end' );
720
- }
721
-
722
- setFontWeight( W ) {
723
- this.svgObject.style.fontWeight = W.toString();
724
- }
725
- }
726
-
727
-
728
- // ================ jsvgTextBox (Div) ================================= //
729
-
730
- export class jsvgTextBox extends jsvgGroup
731
- {
732
- constructor()
733
- {
734
- super();
735
-
736
- this.text = "";
737
-
738
- var ns = 'http://www.w3.org/2000/svg';
739
- this.foreignObject = document.createElementNS( ns, 'foreignObject');
740
-
741
- this.foreignObject.style.backgroundColor = 'none';
742
- this.svgObject.appendChild( this.foreignObject );
743
-
744
- this.div = document.createElement('div');
745
- this.foreignObject.appendChild( this.div );
746
-
747
- this.setWidthAndHeight( 200,100 );
748
- }
749
-
750
- setText( T ) { this.text = T; this.div.innerHTML = T; }
751
-
752
- getText() { return this.text; }
753
-
754
- setStyle( S ) // for style setting with CSS
755
- {
756
- this.div.className = S;
757
- this.div.style.position = 'fixed'; // for safari
758
- };
759
-
760
- setFontFamily( F ) { this.div.style.fontFamily = F; }
761
-
762
- setFontSize( S ) { this.div.style.fontSize = S.toString() + "px"; }
763
-
764
- setLineHeight( H ) { this.div.style.lineHeight = H + 'px'; }
765
-
766
- setFontWeight( W ) { this.div.style.fontWeight = W; }
767
-
768
- setFontColor( C ) { this.div.style.color = C; }
769
-
770
- setAlignment( A ) { this.div.style.textAlign = A; }
771
-
772
- setVerticalCentering()
773
- {
774
- this.div.style.display = "flex";
775
- this.div.style.flexFlow = "column"
776
- this.div.style.justifyContent = "center";
777
- this.div.style.alignItems = "center";
778
- }
779
-
780
- makeEditable()
781
- {
782
- this.div.style.contentEditable = 'true';
783
- this.div.onclick = function() { console.log("here"); this.focus(); };
784
- }
785
-
786
- setBorderWidthAndColor( width, color ) { this.div.style.border = width + "px solid " + color; }
787
-
788
- setWidth( W )
789
- {
790
- this.width = W;
791
- if ( this.div )
792
- this.div.style.width = W + 'px'
793
-
794
- if ( this.foreignObject )
795
- this.foreignObject.setAttribute('width', W );
796
- }
797
-
798
- setHeight( H )
799
- {
800
- this.height = H;
801
- if ( this.div )
802
- this.div.style.height = H + 'px'
803
-
804
- if ( this.foreignObject )
805
- this.foreignObject.setAttribute('height', H );
806
- }
807
- }
808
-
809
-
810
-
811
- // ================ jsvgTextInput ================================= //
812
-
813
- export class jsvgTextInput extends jsvgTextBox
814
- {
815
- constructor()
816
- {
817
- super();
818
-
819
- this.callback = null;
820
-
821
- // remove the generic div before replacing it
822
- this.foreignObject.removeChild( this.div );
823
-
824
- this.div = document.createElement('input');
825
- this.div.contentEditable = true;
826
-
827
- this.div.onkeyup = this.handleInput.bind(this);
828
- this.foreignObject.appendChild( this.div );
829
-
830
- this.setWidthAndHeight( 200,30 );
831
- this.setPlaceholderText("placeholder");
832
- }
833
-
834
- setStyle( S ) // for style setting with CSS
835
- {
836
- this.div.className = S;
837
- };
838
-
839
- setPlaceholderText( T ) { this.div.placeholder = T }
840
-
841
- setText( T ) { this.div.value = T; }
842
-
843
- getText() { return this.div.value; }
844
-
845
- setInputCallback( callback ) { this.callback = callback; } // the callback is a function which receives text
846
-
847
- handleInput( event )
848
- {
849
- if ( event.key == 'Enter' || event.keyCode==13 )
850
- {
851
- if ( this.callback )
852
- this.callback( this.div.value )
853
- }
854
- }
855
- }
856
-
857
-
858
- // ================ jsvgTextArea ================================= //
859
-
860
- export class jsvgTextArea extends jsvgTextBox
861
- {
862
- constructor()
863
- {
864
- super();
865
-
866
- this.callback = null;
867
-
868
- // remove the generic div before replacing it
869
- this.foreignObject.removeChild( this.div );
870
-
871
- this.div = document.createElement('textarea');
872
-
873
- this.div.onkeyup = this.handleInput.bind(this);
874
- this.foreignObject.appendChild( this.div );
875
-
876
- this.setWidthAndHeight( 200,30 );
877
- this.setPlaceholderText("placeholder");
878
- }
879
-
880
- setText( T ) { this.div.value = T; }
881
- getText() { return this.div.value; }
882
-
883
- setStyle( S ) // for style setting with CSS
884
- {
885
- this.div.className = S;
886
- };
887
-
888
- handleInput( event )
889
- {
890
- if ( this.callback )
891
- this.callback( this );
892
- }
893
-
894
- setPlaceholderText( T ) { this.div.placeholder = T }
895
-
896
- setInputCallback( callback ) { this.callback = callback; } // the callback is a function which receives text
897
-
898
- }
1
+
2
+
3
+ // ================ jsvgObject ================================= //
4
+
5
+ export class jsvgObject
6
+ {
7
+ constructor()
8
+ {
9
+ this.name = "";
10
+ this.xpos = 0;
11
+ this.scale = 1.0;
12
+ this.rotation = 0.0;
13
+ this.ypos = 0;
14
+ this.width = 0;
15
+ this.height = 0;
16
+ this.opacity = 1.0;
17
+ this.visible = true;
18
+ this.svgObject = null;
19
+ this.childList = [];
20
+ this.parent = null;
21
+ }
22
+
23
+ // set properties
24
+
25
+ setName( N )
26
+ {
27
+ this.name = N;
28
+ }
29
+
30
+ setPosition( x, y )
31
+ {
32
+ this.xpos = x;
33
+ this.ypos = y;
34
+ this.updateTransform();
35
+ }
36
+
37
+ setScale( s )
38
+ {
39
+ this.scale = s;
40
+ this.updateTransform();
41
+ }
42
+
43
+ setRotation( r )
44
+ {
45
+ this.rotation = r;
46
+ this.updateTransform();
47
+ }
48
+
49
+ updateTransform()
50
+ {
51
+ // Ensure xpos and ypos are valid numbers
52
+ const x = isNaN(this.xpos) ? 0 : this.xpos;
53
+ const y = isNaN(this.ypos) ? 0 : this.ypos;
54
+
55
+ var transformParams = " translate(" + x.toString() + "," + y.toString() + ") "
56
+ if ( this.scale != 1.0 )
57
+ transformParams += " scale(" + this.scale.toString() + ")";
58
+ if ( this.rotation != 0.0 )
59
+ transformParams += " rotate(" + this.rotation + ")";
60
+ this.svgObject.setAttribute("transform", transformParams );
61
+ }
62
+
63
+ setWidth( w )
64
+ {
65
+ this.width = w;
66
+ this.svgObject.setAttribute("width", this.width.toString() );
67
+ }
68
+
69
+ setHeight( h )
70
+ {
71
+ this.height = h;
72
+ this.svgObject.setAttribute("height", this.height.toString() );
73
+ }
74
+
75
+ setWidthAndHeight( w,h )
76
+ {
77
+ this.setWidth( w );
78
+ this.setHeight( h );
79
+ }
80
+
81
+ setFillColor( color )
82
+ {
83
+ this.svgObject.setAttribute( "fill", color );
84
+ }
85
+
86
+ setStrokeColor( color )
87
+ {
88
+ this.svgObject.setAttribute( "stroke", color );
89
+ }
90
+
91
+ setStrokeWidth( sW )
92
+ {
93
+ this.svgObject.setAttribute( "stroke-width", sW.toString() );
94
+ }
95
+
96
+ setOpacity( opacity )
97
+ {
98
+ this.opacity = opacity;
99
+ this.svgObject.style.opacity = opacity;
100
+ }
101
+
102
+ setDropShadow()
103
+ {
104
+ // this is a generic dropshadow
105
+ this.svgObject.style.filter = "drop-shadow(0px 3px 6px rgba(0, 0, 0, 0.4))";
106
+ }
107
+
108
+ show()
109
+ {
110
+ this.visible = true;
111
+ this.svgObject.style.display = "";
112
+ }
113
+
114
+ hide()
115
+ {
116
+ this.visible = false;
117
+ this.svgObject.style.display = "none";
118
+ }
119
+
120
+ // update
121
+
122
+ update()
123
+ {
124
+ if ( ! this.visible )
125
+ return;
126
+
127
+ for( var C of this.childList )
128
+ {
129
+ C.update();
130
+ }
131
+ }
132
+
133
+ // child stuff
134
+
135
+ addChild( child )
136
+ {
137
+ if ( this.svgObject && child.svgObject )
138
+ {
139
+ this.childList.push( child );
140
+ this.svgObject.appendChild( child.svgObject );
141
+ child.parent = this;
142
+ }
143
+ }
144
+
145
+ getChild( index )
146
+ {
147
+ return this.childList[index];
148
+ }
149
+
150
+ replaceChild( newChild, oldChild )
151
+ {
152
+ // replace the node
153
+ this.svgObject.replaceChild( newChild.svgObject, oldChild.svgObject );
154
+
155
+ // replace in childList
156
+ for ( var i=0; i<this.childList.length; i++ )
157
+ {
158
+ if ( this.childList[i] == oldChild )
159
+ {
160
+ this.childList[i] = newChild;
161
+ break;
162
+ }
163
+ }
164
+
165
+ oldChild.parent = null;
166
+ newChild.parent = this;
167
+ }
168
+
169
+ getNumberOfChildren()
170
+ {
171
+ return this.childList.length;
172
+ }
173
+
174
+ removeChild( C )
175
+ {
176
+ const index = this.childList.indexOf(C);
177
+ this.removeChildByIndex( index );
178
+ C.parent = null;
179
+ }
180
+
181
+ removeChildByIndex( index )
182
+ {
183
+ if (index >= 0)
184
+ {
185
+ var C = this.childList[index];
186
+ this.childList.splice(index, 1);
187
+ this.svgObject.removeChild( C.svgObject );
188
+ C.parent = null;
189
+ }
190
+ }
191
+
192
+ removeAllChildren()
193
+ {
194
+ while( this.svgObject.firstChild )
195
+ {
196
+ this.svgObject.removeChild( this.svgObject.lastChild );
197
+ }
198
+ this.childList = [];
199
+ }
200
+
201
+ sortChildren( sortFunction )
202
+ {
203
+ this.childList.sort( sortFunction );
204
+
205
+ // If the child is a reference to an existing node in the document, appendChild() moves it from its current position to the new positio
206
+ this.childList.forEach(child => this.svgObject.appendChild( child.svgObject ));
207
+ }
208
+
209
+ // set up events
210
+ setClickCallback( callback )
211
+ {
212
+ if ( this.svgObject )
213
+ {
214
+ var outerThis = this;
215
+ this.svgObject.onclick = function() { callback(outerThis) };
216
+ this.svgObject.style.cursor = "pointer";
217
+ }
218
+ }
219
+
220
+ enableDragging()
221
+ {
222
+ this.svgObject.style.cursor = "pointer";
223
+
224
+ // on mouse down
225
+ var outerThis = this;
226
+ this.svgObject.onmousedown = function(E)
227
+ {
228
+ // console.log("mouse down");
229
+ outerThis.lastX = E.clientX;
230
+ outerThis.lastY = E.clientY;
231
+
232
+ // register on mouse move function (unregister on mouseup)
233
+ window.onmousemove = function(E)
234
+ {
235
+ // console.log("mouse move");
236
+ var dX = E.clientX - outerThis.lastX; // this method works if there's no scaling or rotation
237
+ var dY = E.clientY - outerThis.lastY;
238
+ outerThis.lastX = E.clientX;
239
+ outerThis.lastY = E.clientY;
240
+ outerThis.setPosition( outerThis.xpos+dX, outerThis.ypos+dY );
241
+ }
242
+
243
+ // on mouse up
244
+ window.onmouseup = function()
245
+ {
246
+ // console.log("mouse up");
247
+ window.onmousemove = null;
248
+ }
249
+ }
250
+ }
251
+ }
252
+
253
+
254
+ // ================ jsvgContainer ================================= //
255
+
256
+ export class jsvgContainer extends jsvgObject
257
+ {
258
+ constructor()
259
+ {
260
+ super();
261
+
262
+ const svgNS = "http://www.w3.org/2000/svg";
263
+ this.svgObject = document.createElementNS(svgNS, "svg");
264
+ this.svgObject.setAttribute("x", "0");
265
+ this.svgObject.setAttribute("y", "0");
266
+ this.svgObject.setAttribute("width", "500");
267
+ this.svgObject.setAttribute("height", "500");
268
+ this.svgObject.setAttribute("backgroundColor", "blue");
269
+ }
270
+
271
+ setViewbox( w, h )
272
+ {
273
+ this.setWidth(w);
274
+ this.setHeight(h);
275
+ var viewBoxParams = "0 0 " + w.toString() + " " + h.toString();
276
+ this.svgObject.setAttribute("viewBox", viewBoxParams );
277
+ }
278
+ }
279
+
280
+ // ================ jsvgGroup ================================= //
281
+
282
+ export class jsvgGroup extends jsvgObject
283
+ {
284
+ constructor()
285
+ {
286
+ super();
287
+
288
+ const svgNS = "http://www.w3.org/2000/svg";
289
+ this.svgObject = document.createElementNS(svgNS, "g");
290
+ this.svgObject.setAttribute("x", "0");
291
+ this.svgObject.setAttribute("y", "0");
292
+ this.svgObject.setAttribute("width", "500");
293
+ this.svgObject.setAttribute("height", "500");
294
+ this.svgObject.setAttribute("viewBox", "0 0 500 500");
295
+ }
296
+ }
297
+
298
+ // ================ jsvgLine ================================= //
299
+
300
+ export class jsvgLine extends jsvgObject
301
+ {
302
+ constructor()
303
+ {
304
+ super();
305
+
306
+ this.x1 = 0;
307
+ this.y1 = 0;
308
+ this.x2 = 100;
309
+ this.y2 = 100;
310
+
311
+ const svgNS = "http://www.w3.org/2000/svg";
312
+ this.svgObject = document.createElementNS(svgNS, "line");
313
+ this.svgObject.setAttribute("x1", "0");
314
+ this.svgObject.setAttribute("y1", "0");
315
+ this.svgObject.setAttribute("x2", "100");
316
+ this.svgObject.setAttribute("y2", "100");
317
+
318
+ this.setStrokeColor("black");
319
+ this.setStrokeWidth( 1 );
320
+
321
+ this.setEndpointA( this.x1, this.y1 );
322
+ this.setEndpointB( this.x2, this.y2 );
323
+ }
324
+
325
+ setEndpointA( x, y )
326
+ {
327
+ this.x1 = x;
328
+ this.y1 = y;
329
+ this.svgObject.setAttribute("x1", x.toString() );
330
+ this.svgObject.setAttribute("y1", y.toString() );
331
+ }
332
+
333
+ setEndpointB( x, y )
334
+ {
335
+ this.x2 = x;
336
+ this.y2 = y;
337
+ this.svgObject.setAttribute("x2", x.toString() );
338
+ this.svgObject.setAttribute("y2", y.toString() );
339
+ }
340
+
341
+ setEndpoints( x1,y1, x2,y2 )
342
+ {
343
+ this.setEndpointA( x1,y1 );
344
+ this.setEndpointB( x2,y2 );
345
+ }
346
+ }
347
+
348
+ // ================ jsvgRect ================================= //
349
+
350
+ export class jsvgRect extends jsvgObject
351
+ {
352
+ constructor()
353
+ {
354
+ super();
355
+
356
+ this.cornerRadius = 0;
357
+
358
+ const svgNS = "http://www.w3.org/2000/svg";
359
+ this.svgObject = document.createElementNS(svgNS, "rect");
360
+ this.setWidthAndHeight( 100,100 );
361
+ this.setFillColor( "white" );
362
+ this.setStrokeColor( "black" );
363
+ this.setStrokeWidth( 0 );
364
+ }
365
+
366
+ setCornerRadius( r )
367
+ {
368
+ this.cornerRadius = r;
369
+ this.svgObject.setAttribute( "rx", r.toString() );
370
+ this.svgObject.setAttribute( "ry", r.toString() );
371
+ }
372
+ }
373
+
374
+ // ================ jsvgEllipse ================================= //
375
+
376
+ export class jsvgEllipse extends jsvgObject
377
+ {
378
+ constructor()
379
+ {
380
+ super();
381
+
382
+ const svgNS = "http://www.w3.org/2000/svg";
383
+ this.svgObject = document.createElementNS(svgNS, "ellipse");
384
+
385
+ this.setPosition( 0,0 );
386
+ this.setWidthAndHeight( 100,100 );
387
+ this.setFillColor( "white" );
388
+ this.setStrokeColor( "black" );
389
+ this.setStrokeWidth( 0 );
390
+ }
391
+
392
+ setWidth( w )
393
+ {
394
+ this.width = w;
395
+ this.svgObject.setAttribute("rx", (this.width/2).toString() );
396
+ }
397
+
398
+ setHeight( h )
399
+ {
400
+ this.height = h;
401
+ this.svgObject.setAttribute("ry", (this.height/2).toString() );
402
+ }
403
+ }
404
+
405
+
406
+ // ================ jsvgArc ================================= //
407
+
408
+ export class jsvgArc extends jsvgObject
409
+ {
410
+ constructor()
411
+ {
412
+ super();
413
+
414
+ const svgNS = "http://www.w3.org/2000/svg";
415
+ this.svgObject = document.createElementNS(svgNS, "path");
416
+
417
+ this.setFillColor("white");
418
+ this.setStrokeColor("black");
419
+ this.setStrokeWidth(0);
420
+ }
421
+
422
+ createPieSlice(radius, startAngle, endAngle)
423
+ {
424
+ // Convert polar coordinates to Cartesian for SVG arc
425
+ function polarToCartesian(radius, angleInDegrees)
426
+ {
427
+ const angleInRadians = (angleInDegrees - 90) * Math.PI / 180;
428
+ return {
429
+ x: (radius * Math.cos(angleInRadians)),
430
+ y: (radius * Math.sin(angleInRadians))
431
+ };
432
+ }
433
+
434
+ const start = polarToCartesian(radius, endAngle);
435
+ const end = polarToCartesian(radius, startAngle);
436
+
437
+ const largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
438
+
439
+ const d = [
440
+ `M ${0} ${0}`, // Move to center
441
+ `L ${start.x} ${start.y}`, // Draw line to start of arc
442
+ `A ${radius} ${radius} 0 ${largeArcFlag} 0 ${end.x} ${end.y}`, // Draw arc
443
+ `Z` // Close path to center
444
+ ].join(" ");
445
+
446
+ this.svgObject.setAttribute("d", d);
447
+ }
448
+ }
449
+
450
+ // ================ jsvgPath ================================= //
451
+
452
+ export class jsvgPath extends jsvgObject
453
+ {
454
+ constructor()
455
+ {
456
+ super();
457
+
458
+ // Create the path element and add it to the SVG element
459
+ this.path = document.createElementNS("http://www.w3.org/2000/svg", "path");
460
+ this.path.setAttribute("fill", "none"); // Default fill is none
461
+ this.path.setAttribute("stroke", "black"); // Default stroke is black
462
+ this.path.setAttribute("stroke-width", "2"); // Default stroke width
463
+ // this.svgElement.appendChild(this.path);
464
+
465
+ this.svgObject = this.path;
466
+
467
+ // Initialize the points array
468
+ this.points = [];
469
+
470
+ // Update the path's 'd' attribute
471
+ this.updatePath();
472
+ }
473
+
474
+ addPoint(x, y)
475
+ {
476
+ if (typeof x !== "number" || typeof y !== "number")
477
+ {
478
+ throw new Error("Coordinates must be numbers.");
479
+ }
480
+
481
+ this.points.push({ x:x, y:y });
482
+
483
+ // must call updatePath to see new points
484
+ }
485
+
486
+ updatePath()
487
+ {
488
+ if (this.points.length === 0)
489
+ {
490
+ this.path.setAttribute("d", "");
491
+ return;
492
+ }
493
+
494
+ // Create the 'd' attribute for the path
495
+ const d = this.points
496
+ .map((point, index) => (index === 0 ? `M ${point.x},${point.y}` : `L ${point.x},${point.y}`))
497
+ .join(" ");
498
+
499
+ // Close the polygon if it has at least three points
500
+ const pathData = d;
501
+
502
+ this.path.setAttribute("d", pathData);
503
+ }
504
+
505
+ clearPoints()
506
+ {
507
+ this.points = [];
508
+ this.updatePath();
509
+ }
510
+ }
511
+
512
+
513
+ // ================ jsvgClipMask ================================= //
514
+
515
+ export class jsvgClipMask extends jsvgObject
516
+ {
517
+ constructor( width=300, height=300, cornerRadius=10 )
518
+ {
519
+ super();
520
+
521
+ // make a new svg group
522
+ const svgNamespace = "http://www.w3.org/2000/svg";
523
+ const svg = document.createElementNS(svgNamespace, "g");
524
+ this.svgObject = svg;
525
+
526
+ this.width = width;
527
+ this.height = height;
528
+ this.cornerRadius = cornerRadius;
529
+
530
+ this.maskID = "clipMaskID" + Math.random();
531
+
532
+ svg.setAttribute("width", this.width);
533
+ svg.setAttribute("height", this.height);
534
+ svg.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
535
+
536
+ // Create a clipPath to apply rounded corners
537
+ const clipPath = document.createElementNS(svgNamespace, "clipPath");
538
+ clipPath.setAttribute("id", this.maskID );
539
+
540
+ this.clipRect = document.createElementNS(svgNamespace, "rect");
541
+ this.clipRect.setAttribute("x", 0);
542
+ this.clipRect.setAttribute("y", 0);
543
+ this.clipRect.setAttribute("width", this.width);
544
+ this.clipRect.setAttribute("height", this.height);
545
+ this.clipRect.setAttribute("rx", this.cornerRadius); // Rounded corner radius
546
+ this.clipRect.setAttribute("ry", this.cornerRadius);
547
+
548
+ clipPath.appendChild(this.clipRect);
549
+ svg.appendChild(clipPath);
550
+
551
+ // we create an inner group that gets masked
552
+ // all children get added this group
553
+ this.clipGroup = new jsvgGroup();
554
+ this.clipGroup.svgObject.setAttribute("clip-path", `url(#${this.maskID})`);
555
+ super.addChild( this.clipGroup ); // use super to avoid recursion
556
+ }
557
+
558
+ setWidth( width )
559
+ {
560
+ this.width = width;
561
+
562
+ this.svgObject.setAttribute("width", this.width);
563
+ this.svgObject.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
564
+ this.clipRect.setAttribute("width", this.width);
565
+ }
566
+
567
+ setHeight( height )
568
+ {
569
+ this.height = height;
570
+
571
+ this.svgObject.setAttribute("height", this.height);
572
+ this.svgObject.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
573
+ this.clipRect.setAttribute("height", this.height);
574
+ }
575
+
576
+ // setWidthAndHeight( width, height )
577
+ // {
578
+ // this.width = width;
579
+ // this.height = height;
580
+
581
+ // this.svgObject.setAttribute("width", this.width);
582
+ // this.svgObject.setAttribute("height", this.height);
583
+ // this.svgObject.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
584
+
585
+ // this.clipRect.setAttribute("width", this.width);
586
+ // this.clipRect.setAttribute("height", this.height);
587
+ // }
588
+
589
+ setCornerRadius( cornerRadius )
590
+ {
591
+ this.cornerRadius = cornerRadius;
592
+ this.clipRect.setAttribute("rx", this.cornerRadius); // Rounded corner radius
593
+ this.clipRect.setAttribute("ry", this.cornerRadius);
594
+ }
595
+
596
+ addChild( obj )
597
+ {
598
+ // add to masked clip group
599
+ this.clipGroup.addChild( obj );
600
+ }
601
+ }
602
+
603
+
604
+
605
+
606
+ // ================ jsvgImage ================================= //
607
+
608
+ export class jsvgImage extends jsvgObject
609
+ {
610
+ constructor()
611
+ {
612
+ super();
613
+
614
+ this.preserveAspectRatio = true;
615
+
616
+ const svgNS = "http://www.w3.org/2000/svg";
617
+ this.svgObject = document.createElementNS(svgNS, "image");
618
+ this.setWidthAndHeight( 100,100 );
619
+ }
620
+
621
+ setImageURL( newURL )
622
+ {
623
+ this.svgObject.setAttribute("href", newURL );
624
+ this.imageURL = newURL;
625
+ }
626
+
627
+ setAspectRatio( svgAspectRatio )
628
+ {
629
+ // to fill the whole space use "xMidYMid slice"
630
+ this.svgObject.setAttribute("preserveAspectRatio", svgAspectRatio );
631
+ }
632
+
633
+ applyRoundedCorners( cornerRadius, svgAspectRatio = 0 )
634
+ {
635
+ // remove the original image
636
+ if ( this.svgObject )
637
+ this.svgObject.remove();
638
+
639
+ // make a new svg image
640
+ const svgNamespace = "http://www.w3.org/2000/svg";
641
+ const svg = document.createElementNS(svgNamespace, "g");
642
+ this.svgObject = svg;
643
+
644
+ svg.setAttribute("width", this.width);
645
+ svg.setAttribute("height", this.height);
646
+ svg.setAttribute("viewBox", `0 0 ${this.width} ${this.height}`);
647
+
648
+ // Create a clipPath to apply rounded corners
649
+ const clipPath = document.createElementNS(svgNamespace, "clipPath");
650
+ clipPath.setAttribute("id", "rounded-corners");
651
+
652
+ const rect = document.createElementNS(svgNamespace, "rect");
653
+ rect.setAttribute("x", 0);
654
+ rect.setAttribute("y", 0);
655
+ rect.setAttribute("width", this.width);
656
+ rect.setAttribute("height", this.height);
657
+ rect.setAttribute("rx", cornerRadius); // Rounded corner radius
658
+ rect.setAttribute("ry", cornerRadius);
659
+
660
+ clipPath.appendChild(rect);
661
+ svg.appendChild(clipPath);
662
+
663
+ // Create the image element
664
+ const image = document.createElementNS(svgNamespace, "image");
665
+ image.setAttribute("href", this.imageURL );
666
+ image.setAttribute("width", this.width);
667
+ image.setAttribute("height", this.height);
668
+ image.setAttribute("clip-path", "url(#rounded-corners)");
669
+ if ( svgAspectRatio )
670
+ image.setAttribute("preserveAspectRatio", svgAspectRatio );
671
+
672
+ svg.appendChild(image);
673
+ }
674
+ }
675
+
676
+
677
+ // ================ jsvgTextLine ================================= //
678
+
679
+ export class jsvgTextLine extends jsvgObject
680
+ {
681
+ constructor()
682
+ {
683
+ super();
684
+
685
+ this.text = "";
686
+
687
+ const svgNS = "http://www.w3.org/2000/svg";
688
+ this.svgObject = document.createElementNS(svgNS, "text");
689
+ this.svgObject.textContent = "hello world";
690
+ this.svgObject.setAttribute("x", "0"); // x-coordinate of the rectangle
691
+ this.svgObject.setAttribute("y", "0"); // y-coordinate of the rectangle
692
+ this.svgObject.setAttribute("fill", "black"); // fill color of the rectangle
693
+ this.svgObject.style.fontFamily = "Arial, Helvetica, sans-serif;";
694
+ }
695
+
696
+ setText( text ) { this.text = text; this.svgObject.textContent = text; }
697
+ getText() { return this.text; }
698
+
699
+ setFontSize( S ) { this.svgObject.style.fontSize = S.toString() + 'px'; }
700
+
701
+ setFontFamily( F ) { this.svgObject.style.fontFamily = F; }
702
+
703
+ setFontColor( C ) { this.svgObject.style.fill = C; }
704
+
705
+ setTextAnchor( A )
706
+ {
707
+ // left, middle or right
708
+ this.svgObject.setAttribute("text-anchor", A ); // fill color of the rectangle
709
+ }
710
+
711
+ setAlignment( A )
712
+ {
713
+ this.alignment = A;
714
+ if ( A == 'left' )
715
+ this.setTextAnchor( 'start' );
716
+ if ( A == 'center' )
717
+ this.setTextAnchor( 'middle' );
718
+ if ( A == 'right' )
719
+ this.setTextAnchor( 'end' );
720
+ }
721
+
722
+ setFontWeight( W ) {
723
+ this.svgObject.style.fontWeight = W.toString();
724
+ }
725
+ }
726
+
727
+
728
+ // ================ jsvgTextBox (Div) ================================= //
729
+
730
+ export class jsvgTextBox extends jsvgGroup
731
+ {
732
+ constructor()
733
+ {
734
+ super();
735
+
736
+ this.text = "";
737
+
738
+ var ns = 'http://www.w3.org/2000/svg';
739
+ this.foreignObject = document.createElementNS( ns, 'foreignObject');
740
+
741
+ this.foreignObject.style.backgroundColor = 'none';
742
+ this.svgObject.appendChild( this.foreignObject );
743
+
744
+ this.div = document.createElement('div');
745
+ this.foreignObject.appendChild( this.div );
746
+
747
+ this.setWidthAndHeight( 200,100 );
748
+ }
749
+
750
+ setText( T ) { this.text = T; this.div.innerHTML = T; }
751
+
752
+ getText() { return this.text; }
753
+
754
+ setStyle( S ) // for style setting with CSS
755
+ {
756
+ this.div.className = S;
757
+ this.div.style.position = 'fixed'; // for safari
758
+ };
759
+
760
+ setFontFamily( F ) { this.div.style.fontFamily = F; }
761
+
762
+ setFontSize( S ) { this.div.style.fontSize = S.toString() + "px"; }
763
+
764
+ setLineHeight( H ) { this.div.style.lineHeight = H + 'px'; }
765
+
766
+ setFontWeight( W ) { this.div.style.fontWeight = W; }
767
+
768
+ setFontColor( C ) { this.div.style.color = C; }
769
+
770
+ setAlignment( A ) { this.div.style.textAlign = A; }
771
+
772
+ setVerticalCentering()
773
+ {
774
+ this.div.style.display = "flex";
775
+ this.div.style.flexFlow = "column"
776
+ this.div.style.justifyContent = "center";
777
+ this.div.style.alignItems = "center";
778
+ }
779
+
780
+ makeEditable()
781
+ {
782
+ this.div.style.contentEditable = 'true';
783
+ this.div.onclick = function() { console.log("here"); this.focus(); };
784
+ }
785
+
786
+ setBorderWidthAndColor( width, color ) { this.div.style.border = width + "px solid " + color; }
787
+
788
+ setWidth( W )
789
+ {
790
+ this.width = W;
791
+ if ( this.div )
792
+ this.div.style.width = W + 'px'
793
+
794
+ if ( this.foreignObject )
795
+ this.foreignObject.setAttribute('width', W );
796
+ }
797
+
798
+ setHeight( H )
799
+ {
800
+ this.height = H;
801
+ if ( this.div )
802
+ this.div.style.height = H + 'px'
803
+
804
+ if ( this.foreignObject )
805
+ this.foreignObject.setAttribute('height', H );
806
+ }
807
+ }
808
+
809
+
810
+
811
+ // ================ jsvgTextInput ================================= //
812
+
813
+ export class jsvgTextInput extends jsvgTextBox
814
+ {
815
+ constructor()
816
+ {
817
+ super();
818
+
819
+ this.callback = null;
820
+
821
+ // remove the generic div before replacing it
822
+ this.foreignObject.removeChild( this.div );
823
+
824
+ this.div = document.createElement('input');
825
+ this.div.contentEditable = true;
826
+
827
+ this.div.onkeyup = this.handleInput.bind(this);
828
+ this.foreignObject.appendChild( this.div );
829
+
830
+ this.setWidthAndHeight( 200,30 );
831
+ this.setPlaceholderText("placeholder");
832
+ }
833
+
834
+ setStyle( S ) // for style setting with CSS
835
+ {
836
+ this.div.className = S;
837
+ };
838
+
839
+ setPlaceholderText( T ) { this.div.placeholder = T }
840
+
841
+ setText( T ) { this.div.value = T; }
842
+
843
+ getText() { return this.div.value; }
844
+
845
+ setInputCallback( callback ) { this.callback = callback; } // the callback is a function which receives text
846
+
847
+ handleInput( event )
848
+ {
849
+ if ( event.key == 'Enter' || event.keyCode==13 )
850
+ {
851
+ if ( this.callback )
852
+ this.callback( this.div.value )
853
+ }
854
+ }
855
+ }
856
+
857
+
858
+ // ================ jsvgTextArea ================================= //
859
+
860
+ export class jsvgTextArea extends jsvgTextBox
861
+ {
862
+ constructor()
863
+ {
864
+ super();
865
+
866
+ this.callback = null;
867
+
868
+ // remove the generic div before replacing it
869
+ this.foreignObject.removeChild( this.div );
870
+
871
+ this.div = document.createElement('textarea');
872
+
873
+ this.div.onkeyup = this.handleInput.bind(this);
874
+ this.foreignObject.appendChild( this.div );
875
+
876
+ this.setWidthAndHeight( 200,30 );
877
+ this.setPlaceholderText("placeholder");
878
+ }
879
+
880
+ setText( T ) { this.div.value = T; }
881
+ getText() { return this.div.value; }
882
+
883
+ setStyle( S ) // for style setting with CSS
884
+ {
885
+ this.div.className = S;
886
+ };
887
+
888
+ handleInput( event )
889
+ {
890
+ if ( this.callback )
891
+ this.callback( this );
892
+ }
893
+
894
+ setPlaceholderText( T ) { this.div.placeholder = T }
895
+
896
+ setInputCallback( callback ) { this.callback = callback; } // the callback is a function which receives text
897
+
898
+ }