@teachinglab/omd 0.1.0

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 (144) hide show
  1. package/README.md +138 -0
  2. package/canvas/core/canvasConfig.js +203 -0
  3. package/canvas/core/omdCanvas.js +475 -0
  4. package/canvas/drawing/segment.js +168 -0
  5. package/canvas/drawing/stroke.js +386 -0
  6. package/canvas/events/eventManager.js +435 -0
  7. package/canvas/events/pointerEventHandler.js +263 -0
  8. package/canvas/features/focusFrameManager.js +287 -0
  9. package/canvas/index.js +49 -0
  10. package/canvas/tools/eraserTool.js +322 -0
  11. package/canvas/tools/pencilTool.js +319 -0
  12. package/canvas/tools/selectTool.js +457 -0
  13. package/canvas/tools/tool.js +223 -0
  14. package/canvas/tools/toolManager.js +394 -0
  15. package/canvas/ui/cursor.js +438 -0
  16. package/canvas/ui/toolbar.js +304 -0
  17. package/canvas/utils/boundingBox.js +378 -0
  18. package/canvas/utils/mathUtils.js +259 -0
  19. package/docs/api/configuration-options.md +104 -0
  20. package/docs/api/eventManager.md +68 -0
  21. package/docs/api/focusFrameManager.md +150 -0
  22. package/docs/api/index.md +91 -0
  23. package/docs/api/main.md +58 -0
  24. package/docs/api/omdBinaryExpressionNode.md +227 -0
  25. package/docs/api/omdCanvas.md +142 -0
  26. package/docs/api/omdConfigManager.md +192 -0
  27. package/docs/api/omdConstantNode.md +117 -0
  28. package/docs/api/omdDisplay.md +121 -0
  29. package/docs/api/omdEquationNode.md +161 -0
  30. package/docs/api/omdEquationSequenceNode.md +301 -0
  31. package/docs/api/omdEquationStack.md +139 -0
  32. package/docs/api/omdFunctionNode.md +141 -0
  33. package/docs/api/omdGroupNode.md +182 -0
  34. package/docs/api/omdHelpers.md +96 -0
  35. package/docs/api/omdLeafNode.md +163 -0
  36. package/docs/api/omdNode.md +101 -0
  37. package/docs/api/omdOperationDisplayNode.md +139 -0
  38. package/docs/api/omdOperatorNode.md +127 -0
  39. package/docs/api/omdParenthesisNode.md +122 -0
  40. package/docs/api/omdPopup.md +117 -0
  41. package/docs/api/omdPowerNode.md +127 -0
  42. package/docs/api/omdRationalNode.md +128 -0
  43. package/docs/api/omdSequenceNode.md +128 -0
  44. package/docs/api/omdSimplification.md +110 -0
  45. package/docs/api/omdSqrtNode.md +79 -0
  46. package/docs/api/omdStepVisualizer.md +115 -0
  47. package/docs/api/omdStepVisualizerHighlighting.md +61 -0
  48. package/docs/api/omdStepVisualizerInteractiveSteps.md +129 -0
  49. package/docs/api/omdStepVisualizerLayout.md +60 -0
  50. package/docs/api/omdStepVisualizerNodeUtils.md +140 -0
  51. package/docs/api/omdStepVisualizerTextBoxes.md +68 -0
  52. package/docs/api/omdToolbar.md +102 -0
  53. package/docs/api/omdTranscriptionService.md +76 -0
  54. package/docs/api/omdTreeDiff.md +134 -0
  55. package/docs/api/omdUnaryExpressionNode.md +174 -0
  56. package/docs/api/omdUtilities.md +70 -0
  57. package/docs/api/omdVariableNode.md +148 -0
  58. package/docs/api/selectTool.md +74 -0
  59. package/docs/api/simplificationEngine.md +98 -0
  60. package/docs/api/simplificationRules.md +77 -0
  61. package/docs/api/simplificationUtils.md +64 -0
  62. package/docs/api/transcribe.md +43 -0
  63. package/docs/api-reference.md +85 -0
  64. package/docs/index.html +454 -0
  65. package/docs/user-guide.md +9 -0
  66. package/index.js +67 -0
  67. package/omd/config/omdConfigManager.js +267 -0
  68. package/omd/core/index.js +150 -0
  69. package/omd/core/omdEquationStack.js +347 -0
  70. package/omd/core/omdUtilities.js +115 -0
  71. package/omd/display/omdDisplay.js +443 -0
  72. package/omd/display/omdToolbar.js +502 -0
  73. package/omd/nodes/omdBinaryExpressionNode.js +460 -0
  74. package/omd/nodes/omdConstantNode.js +142 -0
  75. package/omd/nodes/omdEquationNode.js +1223 -0
  76. package/omd/nodes/omdEquationSequenceNode.js +1273 -0
  77. package/omd/nodes/omdFunctionNode.js +352 -0
  78. package/omd/nodes/omdGroupNode.js +68 -0
  79. package/omd/nodes/omdLeafNode.js +77 -0
  80. package/omd/nodes/omdNode.js +557 -0
  81. package/omd/nodes/omdOperationDisplayNode.js +322 -0
  82. package/omd/nodes/omdOperatorNode.js +109 -0
  83. package/omd/nodes/omdParenthesisNode.js +293 -0
  84. package/omd/nodes/omdPowerNode.js +236 -0
  85. package/omd/nodes/omdRationalNode.js +295 -0
  86. package/omd/nodes/omdSqrtNode.js +308 -0
  87. package/omd/nodes/omdUnaryExpressionNode.js +178 -0
  88. package/omd/nodes/omdVariableNode.js +123 -0
  89. package/omd/simplification/omdSimplification.js +171 -0
  90. package/omd/simplification/omdSimplificationEngine.js +886 -0
  91. package/omd/simplification/package.json +6 -0
  92. package/omd/simplification/rules/binaryRules.js +1037 -0
  93. package/omd/simplification/rules/functionRules.js +111 -0
  94. package/omd/simplification/rules/index.js +48 -0
  95. package/omd/simplification/rules/parenthesisRules.js +19 -0
  96. package/omd/simplification/rules/powerRules.js +143 -0
  97. package/omd/simplification/rules/rationalRules.js +475 -0
  98. package/omd/simplification/rules/sqrtRules.js +48 -0
  99. package/omd/simplification/rules/unaryRules.js +37 -0
  100. package/omd/simplification/simplificationRules.js +32 -0
  101. package/omd/simplification/simplificationUtils.js +1056 -0
  102. package/omd/step-visualizer/omdStepVisualizer.js +597 -0
  103. package/omd/step-visualizer/omdStepVisualizerHighlighting.js +206 -0
  104. package/omd/step-visualizer/omdStepVisualizerLayout.js +245 -0
  105. package/omd/step-visualizer/omdStepVisualizerTextBoxes.js +163 -0
  106. package/omd/utils/omdNodeOverlay.js +638 -0
  107. package/omd/utils/omdPopup.js +1084 -0
  108. package/omd/utils/omdStepVisualizerInteractiveSteps.js +491 -0
  109. package/omd/utils/omdStepVisualizerNodeUtils.js +268 -0
  110. package/omd/utils/omdTranscriptionService.js +125 -0
  111. package/omd/utils/omdTreeDiff.js +734 -0
  112. package/package.json +46 -0
  113. package/src/index.js +62 -0
  114. package/src/json-schemas.md +109 -0
  115. package/src/omd-json-samples.js +115 -0
  116. package/src/omd.js +109 -0
  117. package/src/omdApp.js +391 -0
  118. package/src/omdAppCanvas.js +336 -0
  119. package/src/omdBalanceHanger.js +172 -0
  120. package/src/omdColor.js +13 -0
  121. package/src/omdCoordinatePlane.js +467 -0
  122. package/src/omdEquation.js +125 -0
  123. package/src/omdExpression.js +104 -0
  124. package/src/omdFunction.js +113 -0
  125. package/src/omdMetaExpression.js +287 -0
  126. package/src/omdNaturalExpression.js +564 -0
  127. package/src/omdNode.js +384 -0
  128. package/src/omdNumber.js +53 -0
  129. package/src/omdNumberLine.js +107 -0
  130. package/src/omdNumberTile.js +119 -0
  131. package/src/omdOperator.js +73 -0
  132. package/src/omdPowerExpression.js +92 -0
  133. package/src/omdProblem.js +55 -0
  134. package/src/omdRatioChart.js +232 -0
  135. package/src/omdRationalExpression.js +115 -0
  136. package/src/omdSampleData.js +215 -0
  137. package/src/omdShapes.js +476 -0
  138. package/src/omdSpinner.js +148 -0
  139. package/src/omdString.js +39 -0
  140. package/src/omdTable.js +369 -0
  141. package/src/omdTapeDiagram.js +245 -0
  142. package/src/omdTerm.js +92 -0
  143. package/src/omdTileEquation.js +349 -0
  144. package/src/omdVariable.js +51 -0
@@ -0,0 +1,386 @@
1
+ import { BoundingBox } from '../utils/boundingBox.js';
2
+
3
+ /**
4
+ * Represents a drawing stroke made up of connected points
5
+ */
6
+ export class Stroke {
7
+ /**
8
+ * @param {Object} options - Stroke configuration
9
+ * @param {number} options.x - Starting X coordinate
10
+ * @param {number} options.y - Starting Y coordinate
11
+ * @param {number} [options.strokeWidth=5] - Stroke width
12
+ * @param {string} [options.strokeColor='#000000'] - Stroke color
13
+ * @param {number} [options.strokeOpacity=1] - Stroke opacity
14
+ * @param {string} [options.tool='pencil'] - Tool that created this stroke
15
+ */
16
+ constructor(options = {}) {
17
+ this.id = options.id || this._generateId();
18
+ this.tool = options.tool || 'pencil';
19
+
20
+ // Stroke properties
21
+ this.strokeWidth = options.strokeWidth || 5;
22
+ this.strokeColor = options.strokeColor || '#000000';
23
+ this.strokeOpacity = options.strokeOpacity || 1;
24
+
25
+ // Drawing data
26
+ this.points = [];
27
+ this.isFinished = false;
28
+ this.isSelected = false;
29
+
30
+ // Bounding box for hit testing and selection
31
+ this.boundingBox = new BoundingBox();
32
+
33
+ // Create SVG element
34
+ this._createElement();
35
+
36
+ // Add starting point if provided
37
+ if (options.x !== undefined && options.y !== undefined) {
38
+ this.addPoint({
39
+ x: options.x,
40
+ y: options.y,
41
+ pressure: 0.5,
42
+ width: this.strokeWidth,
43
+ timestamp: Date.now()
44
+ });
45
+ }
46
+ }
47
+
48
+ /**
49
+ * Create the SVG path element
50
+ * @private
51
+ */
52
+ _createElement() {
53
+ this.jsvgPath = new jsvgPath();
54
+ this.element = this.jsvgPath.svgObject; // Get the underlying SVG element
55
+ this.jsvgPath.setFillColor('none');
56
+ this.jsvgPath.setStrokeColor(this.strokeColor);
57
+ this.jsvgPath.setStrokeWidth(this.strokeWidth);
58
+ this.element.setAttribute('stroke-opacity', this.strokeOpacity);
59
+ this.element.setAttribute('stroke-linecap', 'round');
60
+ this.element.setAttribute('stroke-linejoin', 'round');
61
+ this.element.setAttribute('data-stroke-id', this.id);
62
+ this.element.setAttribute('data-tool', this.tool);
63
+ }
64
+
65
+ /**
66
+ * Add a point to the stroke
67
+ * @param {Object} point - Point data
68
+ * @param {number} point.x - X coordinate
69
+ * @param {number} point.y - Y coordinate
70
+ * @param {number} [point.pressure=0.5] - Pressure value
71
+ * @param {number} [point.width] - Stroke width at this point
72
+ * @param {number} [point.timestamp] - Timestamp
73
+ */
74
+ addPoint(point) {
75
+ const normalizedPoint = {
76
+ x: point.x,
77
+ y: point.y,
78
+ pressure: point.pressure || 0.5,
79
+ width: point.width || this.strokeWidth,
80
+ timestamp: point.timestamp || Date.now()
81
+ };
82
+
83
+ this.points.push(normalizedPoint);
84
+ this._updatePath();
85
+ this._updateBoundingBox();
86
+ }
87
+
88
+ /**
89
+ * Update the SVG path based on current points
90
+ * @private
91
+ */
92
+ _updatePath() {
93
+ if (this.points.length === 0) {
94
+ this.jsvgPath.clearPoints();
95
+ return;
96
+ }
97
+
98
+ this.jsvgPath.clearPoints(); // Clear existing points before adding new ones
99
+
100
+ if (this.points.length === 1) {
101
+ // Single point - draw a small circle
102
+ const point = this.points[0];
103
+ this.jsvgPath.addPoint(point.x, point.y);
104
+ this.jsvgPath.addPoint(point.x + 0.1, point.y); // Add a second point for a tiny line
105
+ } else {
106
+ // Multiple points - create smooth path
107
+ this._generateSmoothPath();
108
+ }
109
+
110
+ this.jsvgPath.updatePath();
111
+ }
112
+
113
+ /**
114
+ * Generate smooth path using cubic Bézier curves
115
+ * @private
116
+ */
117
+ _generateSmoothPath() {
118
+ if (this.points.length < 2) return '';
119
+
120
+ this.jsvgPath.addPoint(this.points[0].x, this.points[0].y);
121
+
122
+ if (this.points.length === 2) {
123
+ // Simple line for 2 points
124
+ this.jsvgPath.addPoint(this.points[1].x, this.points[1].y);
125
+ return;
126
+ }
127
+
128
+ // Use cubic Bézier curves for smoother paths
129
+ for (let i = 1; i < this.points.length - 1; i++) {
130
+ const prev = this.points[i - 1];
131
+ const curr = this.points[i];
132
+ const next = this.points[i + 1];
133
+
134
+ // Calculate control points for smooth curve
135
+ const cp1x = curr.x + (next.x - prev.x) * 0.25;
136
+ const cp1y = curr.y + (next.y - prev.y) * 0.25;
137
+ const cp2x = next.x - (next.x - curr.x) * 0.25;
138
+ const cp2y = next.y - (next.y - curr.y) * 0.25;
139
+
140
+ // jsvgPath doesn't directly support cubic bezier curves, so we'll approximate with more points
141
+ // For a true cubic bezier, we'd need to extend jsvgPath or use raw SVG path data.
142
+ // For now, we'll just add the next point.
143
+ this.jsvgPath.addPoint(next.x, next.y);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Update bounding box based on current points
149
+ * @private
150
+ */
151
+ _updateBoundingBox() {
152
+ if (this.points.length === 0) return;
153
+
154
+ let minX = Infinity, minY = Infinity;
155
+ let maxX = -Infinity, maxY = -Infinity;
156
+
157
+ this.points.forEach(point => {
158
+ const radius = point.width / 2;
159
+ minX = Math.min(minX, point.x - radius);
160
+ minY = Math.min(minY, point.y - radius);
161
+ maxX = Math.max(maxX, point.x + radius);
162
+ maxY = Math.max(maxY, point.y + radius);
163
+ });
164
+
165
+ this.boundingBox.set(minX, minY, maxX - minX, maxY - minY);
166
+ }
167
+
168
+ /**
169
+ * Finish the stroke (no more points will be added)
170
+ */
171
+ finish() {
172
+ this.isFinished = true;
173
+ this.element.setAttribute('data-finished', 'true');
174
+ }
175
+
176
+ /**
177
+ * Set stroke selection state
178
+ * @param {boolean} selected - Whether stroke is selected
179
+ */
180
+ setSelected(selected) {
181
+ this.isSelected = selected;
182
+
183
+ if (selected) {
184
+ this.element.setAttribute('stroke-dasharray', '5,5');
185
+ this.element.setAttribute('data-selected', 'true');
186
+ } else {
187
+ this.element.removeAttribute('stroke-dasharray');
188
+ this.element.removeAttribute('data-selected');
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Update stroke configuration
194
+ * @param {Object} config - New configuration
195
+ */
196
+ updateConfig(config) {
197
+ if (config.strokeColor !== undefined) {
198
+ this.strokeColor = config.strokeColor;
199
+ this.jsvgPath.setStrokeColor(this.strokeColor);
200
+ }
201
+
202
+ if (config.strokeWidth !== undefined) {
203
+ this.strokeWidth = config.strokeWidth;
204
+ this.jsvgPath.setStrokeWidth(this.strokeWidth);
205
+ }
206
+
207
+ if (config.strokeOpacity !== undefined) {
208
+ this.strokeOpacity = config.strokeOpacity;
209
+ this.element.setAttribute('stroke-opacity', this.strokeOpacity);
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Check if a point is near this stroke
215
+ * @param {number} x - X coordinate
216
+ * @param {number} y - Y coordinate
217
+ * @param {number} [tolerance=10] - Distance tolerance
218
+ * @returns {boolean} True if point is near stroke
219
+ */
220
+ isNearPoint(x, y, tolerance = 10) {
221
+ // Quick bounding box check first
222
+ if (!this.boundingBox.containsPoint(x, y, tolerance)) {
223
+ return false;
224
+ }
225
+
226
+ // Check distance to each line segment
227
+ for (let i = 1; i < this.points.length; i++) {
228
+ const p1 = this.points[i - 1];
229
+ const p2 = this.points[i];
230
+
231
+ const distance = this._distanceToLineSegment(x, y, p1.x, p1.y, p2.x, p2.y);
232
+ if (distance <= tolerance) {
233
+ return true;
234
+ }
235
+ }
236
+
237
+ return false;
238
+ }
239
+
240
+ /**
241
+ * Calculate distance from point to line segment
242
+ * @private
243
+ */
244
+ _distanceToLineSegment(px, py, x1, y1, x2, y2) {
245
+ const dx = x2 - x1;
246
+ const dy = y2 - y1;
247
+ const length = Math.sqrt(dx * dx + dy * dy);
248
+
249
+ if (length === 0) {
250
+ // Points are the same
251
+ return Math.sqrt((px - x1) * (px - x1) + (py - y1) * (py - y1));
252
+ }
253
+
254
+ const t = Math.max(0, Math.min(1, ((px - x1) * dx + (py - y1) * dy) / (length * length)));
255
+ const projX = x1 + t * dx;
256
+ const projY = y1 + t * dy;
257
+
258
+ return Math.sqrt((px - projX) * (px - projX) + (py - projY) * (py - projY));
259
+ }
260
+
261
+ /**
262
+ * Get stroke length
263
+ * @returns {number} Total stroke length
264
+ */
265
+ getLength() {
266
+ let length = 0;
267
+
268
+ for (let i = 1; i < this.points.length; i++) {
269
+ const p1 = this.points[i - 1];
270
+ const p2 = this.points[i];
271
+ const dx = p2.x - p1.x;
272
+ const dy = p2.y - p1.y;
273
+ length += Math.sqrt(dx * dx + dy * dy);
274
+ }
275
+
276
+ return length;
277
+ }
278
+
279
+ /**
280
+ * Get stroke data for serialization
281
+ * @returns {Object} Serializable stroke data
282
+ */
283
+ toJSON() {
284
+ return {
285
+ id: this.id,
286
+ tool: this.tool,
287
+ strokeWidth: this.strokeWidth,
288
+ strokeColor: this.strokeColor,
289
+ strokeOpacity: this.strokeOpacity,
290
+ points: this.points,
291
+ isFinished: this.isFinished,
292
+ boundingBox: this.boundingBox.toJSON()
293
+ };
294
+ }
295
+
296
+ /**
297
+ * Create stroke from serialized data
298
+ * @param {Object} data - Serialized stroke data
299
+ * @returns {Stroke} New stroke instance
300
+ */
301
+ static fromJSON(data) {
302
+ const stroke = new Stroke({
303
+ id: data.id,
304
+ tool: data.tool,
305
+ strokeWidth: data.strokeWidth,
306
+ strokeColor: data.strokeColor,
307
+ strokeOpacity: data.strokeOpacity
308
+ });
309
+
310
+ // Add all points
311
+ data.points.forEach(point => stroke.addPoint(point));
312
+
313
+ if (data.isFinished) {
314
+ stroke.finish();
315
+ }
316
+
317
+ return stroke;
318
+ }
319
+
320
+ /**
321
+ * Generate unique stroke ID
322
+ * @private
323
+ */
324
+ _generateId() {
325
+ return `stroke_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
326
+ }
327
+
328
+ /**
329
+ * Clone this stroke
330
+ * @returns {Stroke} New stroke instance
331
+ */
332
+ clone() {
333
+ return Stroke.fromJSON(this.toJSON());
334
+ }
335
+
336
+ /**
337
+ * Get point count
338
+ * @returns {number} Number of points in stroke
339
+ */
340
+ getPointCount() {
341
+ return this.points.length;
342
+ }
343
+
344
+ /**
345
+ * Get bounding box
346
+ * @returns {BoundingBox} Stroke bounding box
347
+ */
348
+ getBoundingBox() {
349
+ return this.boundingBox;
350
+ }
351
+
352
+ /**
353
+ * Move stroke by offset
354
+ * @param {number} dx - X offset
355
+ * @param {number} dy - Y offset
356
+ */
357
+ move(dx, dy) {
358
+ this.points.forEach(point => {
359
+ point.x += dx;
360
+ point.y += dy;
361
+ });
362
+
363
+ this._updatePath();
364
+ this._updateBoundingBox();
365
+ }
366
+
367
+ /**
368
+ * Scale stroke by factor
369
+ * @param {number} scale - Scale factor
370
+ * @param {number} [originX=0] - Scale origin X
371
+ * @param {number} [originY=0] - Scale origin Y
372
+ */
373
+ scale(scale, originX = 0, originY = 0) {
374
+ this.points.forEach(point => {
375
+ point.x = originX + (point.x - originX) * scale;
376
+ point.y = originY + (point.y - originY) * scale;
377
+ point.width *= scale;
378
+ });
379
+
380
+ this.strokeWidth *= scale;
381
+ this.jsvgPath.setStrokeWidth(this.strokeWidth);
382
+
383
+ this._updatePath();
384
+ this._updateBoundingBox();
385
+ }
386
+ }