@teachinglab/omd 0.6.1 → 0.6.3

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 +606 -547
  123. package/omd/core/omdUtilities.js +113 -113
  124. package/omd/display/omdDisplay.js +1045 -963
  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 -57
  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/README.old.md CHANGED
@@ -1,138 +1,138 @@
1
- # OMD (Open Math Display)
2
-
3
- OMD is a JavaScript library for creating interactive mathematical interfaces in web applications. Build everything from simple equation displays to complex step-by-step solution systems with rich visual feedback and user interaction.
4
-
5
- ![OMD Demo](https://i.imgur.com/CdtEi33.png)
6
-
7
- ## Features
8
-
9
- ### **Interactive Math Rendering**
10
- - High-quality SVG-based mathematical notation
11
- - Real-time expression manipulation and visualization
12
- - Automatic layout and alignment for complex equations
13
-
14
- ### **Step-by-Step Solutions**
15
- - Visual step tracking with detailed explanations
16
- - Simplification engine with rule-based transformations
17
- - Provenance tracking for highlighting related elements
18
-
19
- ### **Rich UI Components**
20
- - Built-in toolbar for common mathematical operations
21
- - Drag & drop interface for intuitive manipulation
22
- - Customizable canvas for multi-expression layouts
23
-
24
- ### **Educational Features**
25
- - Interactive learning experiences
26
- - Progressive step revelation
27
- - Visual operation feedback and highlighting
28
-
29
- ## Installation
30
-
31
- ### npm
32
- ```bash
33
- npm install @teachinglab/omd
34
- ```
35
-
36
- ## Basic Usage
37
- ```javascript
38
- import { omdDisplay } from '@teachinglab/omd';
39
-
40
- // Create a math display
41
- const container = document.getElementById('math-container');
42
- const display = new omdDisplay(container);
43
-
44
- // Render an equation
45
- display.render('2x + 3 = 11');
46
- ```
47
-
48
- ### Step-by-Step Solutions
49
- ```javascript
50
- import { omdEquationStack, omdEquationNode } from '@teachinglab/omd';
51
-
52
- // Create solution steps
53
- const steps = [
54
- omdEquationNode.fromString('2x + 3 = 11'),
55
- omdEquationNode.fromString('2x = 8'),
56
- omdEquationNode.fromString('x = 4')
57
- ];
58
-
59
- // Create interactive equation stack
60
- const stack = new omdEquationStack(steps, {
61
- toolbar: true,
62
- stepVisualizer: true
63
- });
64
-
65
- display.render(stack);
66
- ```
67
-
68
- ## Core Concepts
69
-
70
- ### **Nodes** - Building Blocks
71
- Every mathematical element is a node in an expression tree:
72
- - `omdEquationNode` - Complete equations (e.g., `2x + 3 = 11`)
73
- - `omdConstantNode` - Numbers (e.g., `5`, `3.14`)
74
- - `omdVariableNode` - Variables (e.g., `x`, `y`)
75
- - `omdBinaryExpressionNode` - Operations (e.g., `+`, `-`, `*`, `/`)
76
-
77
- ### **Sequences** - Solution Steps
78
- Group related equations for step-by-step solving:
79
- ```javascript
80
- const sequence = new omdEquationSequenceNode([
81
- equation1, equation2, equation3
82
- ]);
83
- ```
84
-
85
- ### **Display** - Rendering Engine
86
- Handles layout, centering, and visualization:
87
- ```javascript
88
- const display = new omdDisplay(container, {
89
- fontSize: 36,
90
- centerContent: true
91
- });
92
- ```
93
-
94
- ## Interactive Examples
95
-
96
- Explore OMD's capabilities with our comprehensive examples:
97
-
98
- | Category | Example | Description |
99
- |----------|---------|-------------|
100
- | **Getting Started** | [Minimal](examples/minimal.html) | Basic equation rendering |
101
- | | [Simple Usage](examples/simple-usage.html) | Interactive features |
102
- | **Advanced** | [Expression Playground](examples/expression-playground.html) | Full manipulation interface |
103
- | | [Drag & Drop](examples/drag-and-drop-playground.html) | Intuitive interaction |
104
- | **Educational** | [Worked Solutions](examples/worked-solution.html) | Step-by-step solving |
105
- | | [Kids Interactive](examples/kids-interactive.html) | Child-friendly interface |
106
- | **Components** | [Equation Stack](examples/equation-stack-test.html) | Stacked equations |
107
- | | [Canvas Demo](examples/canvas-multiple-nodes.html) | Multi-expression layouts |
108
-
109
- **[Browse All Examples](examples/index.html)**
110
-
111
- ## Documentation
112
-
113
- | Resource | Description |
114
- |----------|-------------|
115
- | **[API Reference](docs/api-reference.md)** | Complete component documentation |
116
- | **[User Guide](docs/user-guide.md)** | Getting started and tutorials |
117
-
118
-
119
- ## Architecture
120
-
121
- ```
122
- OMD Library Structure
123
- ├── Display Layer (omdDisplay)
124
- ├── Node System (Expression tree components)
125
- ├── UI Components (Toolbar, Step visualizer)
126
- ├── Core Systems (Simplification, Layout)
127
- └── Utilities (Configuration, Helpers)
128
- ```
129
-
130
- ## Dependencies
131
-
132
- - **JSVG** - High-performance SVG rendering
133
- - **math.js** - Mathematical expression parsing
134
- - **Modern Browser** - ES6 modules, SVG support
135
-
136
- ---
137
-
1
+ # OMD (On-screen Math Display)
2
+
3
+ OMD is a JavaScript library for creating interactive mathematical interfaces in web applications. Build everything from simple equation displays to complex step-by-step solution systems with rich visual feedback and user interaction.
4
+
5
+ ![OMD Demo](https://i.imgur.com/CdtEi33.png)
6
+
7
+ ## Features
8
+
9
+ ### **Interactive Math Rendering**
10
+ - High-quality SVG-based mathematical notation
11
+ - Real-time expression manipulation and visualization
12
+ - Automatic layout and alignment for complex equations
13
+
14
+ ### **Step-by-Step Solutions**
15
+ - Visual step tracking with detailed explanations
16
+ - Simplification engine with rule-based transformations
17
+ - Provenance tracking for highlighting related elements
18
+
19
+ ### **Rich UI Components**
20
+ - Built-in toolbar for common mathematical operations
21
+ - Drag & drop interface for intuitive manipulation
22
+ - Customizable canvas for multi-expression layouts
23
+
24
+ ### **Educational Features**
25
+ - Interactive learning experiences
26
+ - Progressive step revelation
27
+ - Visual operation feedback and highlighting
28
+
29
+ ## Installation
30
+
31
+ ### npm
32
+ ```bash
33
+ npm install @teachinglab/omd
34
+ ```
35
+
36
+ ## Basic Usage
37
+ ```javascript
38
+ import { omdDisplay } from '@teachinglab/omd';
39
+
40
+ // Create a math display
41
+ const container = document.getElementById('math-container');
42
+ const display = new omdDisplay(container);
43
+
44
+ // Render an equation
45
+ display.render('2x + 3 = 11');
46
+ ```
47
+
48
+ ### Step-by-Step Solutions
49
+ ```javascript
50
+ import { omdEquationStack, omdEquationNode } from '@teachinglab/omd';
51
+
52
+ // Create solution steps
53
+ const steps = [
54
+ omdEquationNode.fromString('2x + 3 = 11'),
55
+ omdEquationNode.fromString('2x = 8'),
56
+ omdEquationNode.fromString('x = 4')
57
+ ];
58
+
59
+ // Create interactive equation stack
60
+ const stack = new omdEquationStack(steps, {
61
+ toolbar: true,
62
+ stepVisualizer: true
63
+ });
64
+
65
+ display.render(stack);
66
+ ```
67
+
68
+ ## Core Concepts
69
+
70
+ ### **Nodes** - Building Blocks
71
+ Every mathematical element is a node in an expression tree:
72
+ - `omdEquationNode` - Complete equations (e.g., `2x + 3 = 11`)
73
+ - `omdConstantNode` - Numbers (e.g., `5`, `3.14`)
74
+ - `omdVariableNode` - Variables (e.g., `x`, `y`)
75
+ - `omdBinaryExpressionNode` - Operations (e.g., `+`, `-`, `*`, `/`)
76
+
77
+ ### **Sequences** - Solution Steps
78
+ Group related equations for step-by-step solving:
79
+ ```javascript
80
+ const sequence = new omdEquationSequenceNode([
81
+ equation1, equation2, equation3
82
+ ]);
83
+ ```
84
+
85
+ ### **Display** - Rendering Engine
86
+ Handles layout, centering, and visualization:
87
+ ```javascript
88
+ const display = new omdDisplay(container, {
89
+ fontSize: 36,
90
+ centerContent: true
91
+ });
92
+ ```
93
+
94
+ ## Interactive Examples
95
+
96
+ Explore OMD's capabilities with our comprehensive examples:
97
+
98
+ | Category | Example | Description |
99
+ |----------|---------|-------------|
100
+ | **Getting Started** | [Minimal](examples/minimal.html) | Basic equation rendering |
101
+ | | [Simple Usage](examples/simple-usage.html) | Interactive features |
102
+ | **Advanced** | [Expression Playground](examples/expression-playground.html) | Full manipulation interface |
103
+ | | [Drag & Drop](examples/drag-and-drop-playground.html) | Intuitive interaction |
104
+ | **Educational** | [Worked Solutions](examples/worked-solution.html) | Step-by-step solving |
105
+ | | [Kids Interactive](examples/kids-interactive.html) | Child-friendly interface |
106
+ | **Components** | [Equation Stack](examples/equation-stack-test.html) | Stacked equations |
107
+ | | [Canvas Demo](examples/canvas-multiple-nodes.html) | Multi-expression layouts |
108
+
109
+ **[Browse All Examples](examples/index.html)**
110
+
111
+ ## Documentation
112
+
113
+ | Resource | Description |
114
+ |----------|-------------|
115
+ | **[API Reference](docs/api-reference.md)** | Complete component documentation |
116
+ | **[User Guide](docs/user-guide.md)** | Getting started and tutorials |
117
+
118
+
119
+ ## Architecture
120
+
121
+ ```
122
+ OMD Library Structure
123
+ ├── Display Layer (omdDisplay)
124
+ ├── Node System (Expression tree components)
125
+ ├── UI Components (Toolbar, Step visualizer)
126
+ ├── Core Systems (Simplification, Layout)
127
+ └── Utilities (Configuration, Helpers)
128
+ ```
129
+
130
+ ## Dependencies
131
+
132
+ - **JSVG** - High-performance SVG rendering
133
+ - **math.js** - Mathematical expression parsing
134
+ - **Modern Browser** - ES6 modules, SVG support
135
+
136
+ ---
137
+
138
138
  **Ready to get started?** Check out our [examples](examples/index.html) or dive into the [documentation](docs/api-reference.md)!
@@ -1,203 +1,203 @@
1
- export class CanvasConfig {
2
- /**
3
- * @param {Object} options - Configuration options
4
- */
5
- constructor(options = {}) {
6
- // Default configuration
7
- this.width = options.width || 800;
8
- this.height = options.height || 500;
9
- this.backgroundColor = options.backgroundColor || 'white';
10
-
11
- // UI options
12
- this.showToolbar = options.showToolbar !== false;
13
- this.showGrid = options.showGrid || false;
14
- this.gridSpacing = options.gridSpacing || 20;
15
-
16
- // Tool configuration
17
- this.enabledTools = options.enabledTools || ['pointer', 'pencil', 'eraser', 'select'];
18
- this.defaultTool = options.defaultTool || 'pointer';
19
-
20
- // Feature flags
21
- this.enableFocusFrames = options.enableFocusFrames !== false;
22
- this.enableKeyboardShortcuts = options.enableKeyboardShortcuts !== false;
23
- this.enableMultiTouch = options.enableMultiTouch !== false;
24
-
25
- // Tool defaults
26
- this.tools = {
27
- pencil: {
28
- strokeWidth: 5,
29
- strokeColor: '#000000',
30
- strokeOpacity: 1,
31
- smoothing: 0.5,
32
- pressureSensitivity: true,
33
- ...options.tools?.pencil
34
- },
35
- eraser: {
36
- size: 20,
37
- hardness: 0.8,
38
- ...options.tools?.eraser
39
- },
40
- select: {
41
- selectionColor: '#007bff',
42
- selectionOpacity: 0.3,
43
- ...options.tools?.select
44
- }
45
- };
46
-
47
- // Theme
48
- this.theme = {
49
- primary: '#007bff',
50
- secondary: '#6c757d',
51
- success: '#28a745',
52
- warning: '#ffc107',
53
- danger: '#dc3545',
54
- ...options.theme
55
- };
56
-
57
- // Validate configuration
58
- this._validate();
59
- }
60
-
61
- /**
62
- * Validate configuration
63
- * @private
64
- */
65
- _validate() {
66
- // Validate dimensions
67
- if (this.width <= 0 || this.height <= 0) {
68
- throw new Error('Canvas dimensions must be positive numbers');
69
- }
70
-
71
- // Validate enabled tools
72
- const availableTools = ['pointer', 'pencil', 'eraser', 'select'];
73
- const invalidTools = this.enabledTools.filter(tool => !availableTools.includes(tool));
74
- if (invalidTools.length > 0) {
75
- console.warn(`Invalid tools specified: ${invalidTools.join(', ')}`);
76
- this.enabledTools = this.enabledTools.filter(tool => availableTools.includes(tool));
77
- }
78
-
79
- // Ensure at least one tool is enabled
80
- if (this.enabledTools.length === 0) {
81
- this.enabledTools = ['pencil'];
82
- }
83
-
84
- // Validate default tool
85
- if (!this.enabledTools.includes(this.defaultTool)) {
86
- this.defaultTool = this.enabledTools[0];
87
- }
88
-
89
- // Validate tool configurations
90
- this._validateToolConfigs();
91
- }
92
-
93
- /**
94
- * Validate tool configurations
95
- * @private
96
- */
97
- _validateToolConfigs() {
98
- // Validate pencil config
99
- if (this.tools.pencil.strokeWidth <= 0) {
100
- this.tools.pencil.strokeWidth = 5;
101
- }
102
- if (this.tools.pencil.strokeOpacity < 0 || this.tools.pencil.strokeOpacity > 1) {
103
- this.tools.pencil.strokeOpacity = 1;
104
- }
105
-
106
- // Validate eraser config
107
- if (this.tools.eraser.size <= 0) {
108
- this.tools.eraser.size = 20;
109
- }
110
- if (this.tools.eraser.hardness < 0 || this.tools.eraser.hardness > 1) {
111
- this.tools.eraser.hardness = 0.8;
112
- }
113
-
114
- // Validate select config
115
- if (this.tools.select.selectionOpacity < 0 || this.tools.select.selectionOpacity > 1) {
116
- this.tools.select.selectionOpacity = 0.3;
117
- }
118
- }
119
-
120
- /**
121
- * Update configuration
122
- * @param {Object} updates - Configuration updates
123
- */
124
- update(updates) {
125
- Object.assign(this, updates);
126
- this._validate();
127
- }
128
-
129
- /**
130
- * Get tool configuration
131
- * @param {string} toolName - Tool name
132
- * @returns {Object} Tool configuration
133
- */
134
- getToolConfig(toolName) {
135
- return this.tools[toolName] || {};
136
- }
137
-
138
- /**
139
- * Update tool configuration
140
- * @param {string} toolName - Tool name
141
- * @param {Object} config - Tool configuration updates
142
- */
143
- updateToolConfig(toolName, config) {
144
- if (!this.tools[toolName]) {
145
- this.tools[toolName] = {};
146
- }
147
- Object.assign(this.tools[toolName], config);
148
- this._validateToolConfigs();
149
- }
150
-
151
- /**
152
- * Clone configuration
153
- * @returns {CanvasConfig} New configuration instance
154
- */
155
- clone() {
156
- return new CanvasConfig({
157
- width: this.width,
158
- height: this.height,
159
- backgroundColor: this.backgroundColor,
160
- showToolbar: this.showToolbar,
161
- showGrid: this.showGrid,
162
- gridSpacing: this.gridSpacing,
163
- enabledTools: [...this.enabledTools],
164
- defaultTool: this.defaultTool,
165
- enableFocusFrames: this.enableFocusFrames,
166
- enableKeyboardShortcuts: this.enableKeyboardShortcuts,
167
- enableMultiTouch: this.enableMultiTouch,
168
- tools: JSON.parse(JSON.stringify(this.tools)),
169
- theme: { ...this.theme }
170
- });
171
- }
172
-
173
- /**
174
- * Convert to JSON
175
- * @returns {Object} JSON representation
176
- */
177
- toJSON() {
178
- return {
179
- width: this.width,
180
- height: this.height,
181
- backgroundColor: this.backgroundColor,
182
- showToolbar: this.showToolbar,
183
- showGrid: this.showGrid,
184
- gridSpacing: this.gridSpacing,
185
- enabledTools: this.enabledTools,
186
- defaultTool: this.defaultTool,
187
- enableFocusFrames: this.enableFocusFrames,
188
- enableKeyboardShortcuts: this.enableKeyboardShortcuts,
189
- enableMultiTouch: this.enableMultiTouch,
190
- tools: this.tools,
191
- theme: this.theme
192
- };
193
- }
194
-
195
- /**
196
- * Create from JSON
197
- * @param {Object} data - JSON data
198
- * @returns {CanvasConfig} New configuration instance
199
- */
200
- static fromJSON(data) {
201
- return new CanvasConfig(data);
202
- }
1
+ export class CanvasConfig {
2
+ /**
3
+ * @param {Object} options - Configuration options
4
+ */
5
+ constructor(options = {}) {
6
+ // Default configuration
7
+ this.width = options.width || 800;
8
+ this.height = options.height || 500;
9
+ this.backgroundColor = options.backgroundColor || 'white';
10
+
11
+ // UI options
12
+ this.showToolbar = options.showToolbar !== false;
13
+ this.showGrid = options.showGrid || false;
14
+ this.gridSpacing = options.gridSpacing || 20;
15
+
16
+ // Tool configuration
17
+ this.enabledTools = options.enabledTools || ['pointer', 'pencil', 'eraser', 'select'];
18
+ this.defaultTool = options.defaultTool || 'pointer';
19
+
20
+ // Feature flags
21
+ this.enableFocusFrames = options.enableFocusFrames !== false;
22
+ this.enableKeyboardShortcuts = options.enableKeyboardShortcuts !== false;
23
+ this.enableMultiTouch = options.enableMultiTouch !== false;
24
+
25
+ // Tool defaults
26
+ this.tools = {
27
+ pencil: {
28
+ strokeWidth: 5,
29
+ strokeColor: '#000000',
30
+ strokeOpacity: 1,
31
+ smoothing: 0.5,
32
+ pressureSensitivity: true,
33
+ ...options.tools?.pencil
34
+ },
35
+ eraser: {
36
+ size: 20,
37
+ hardness: 0.8,
38
+ ...options.tools?.eraser
39
+ },
40
+ select: {
41
+ selectionColor: '#007bff',
42
+ selectionOpacity: 0.3,
43
+ ...options.tools?.select
44
+ }
45
+ };
46
+
47
+ // Theme
48
+ this.theme = {
49
+ primary: '#007bff',
50
+ secondary: '#6c757d',
51
+ success: '#28a745',
52
+ warning: '#ffc107',
53
+ danger: '#dc3545',
54
+ ...options.theme
55
+ };
56
+
57
+ // Validate configuration
58
+ this._validate();
59
+ }
60
+
61
+ /**
62
+ * Validate configuration
63
+ * @private
64
+ */
65
+ _validate() {
66
+ // Validate dimensions
67
+ if (this.width <= 0 || this.height <= 0) {
68
+ throw new Error('Canvas dimensions must be positive numbers');
69
+ }
70
+
71
+ // Validate enabled tools
72
+ const availableTools = ['pointer', 'pencil', 'eraser', 'select'];
73
+ const invalidTools = this.enabledTools.filter(tool => !availableTools.includes(tool));
74
+ if (invalidTools.length > 0) {
75
+ console.warn(`Invalid tools specified: ${invalidTools.join(', ')}`);
76
+ this.enabledTools = this.enabledTools.filter(tool => availableTools.includes(tool));
77
+ }
78
+
79
+ // Ensure at least one tool is enabled
80
+ if (this.enabledTools.length === 0) {
81
+ this.enabledTools = ['pencil'];
82
+ }
83
+
84
+ // Validate default tool
85
+ if (!this.enabledTools.includes(this.defaultTool)) {
86
+ this.defaultTool = this.enabledTools[0];
87
+ }
88
+
89
+ // Validate tool configurations
90
+ this._validateToolConfigs();
91
+ }
92
+
93
+ /**
94
+ * Validate tool configurations
95
+ * @private
96
+ */
97
+ _validateToolConfigs() {
98
+ // Validate pencil config
99
+ if (this.tools.pencil.strokeWidth <= 0) {
100
+ this.tools.pencil.strokeWidth = 5;
101
+ }
102
+ if (this.tools.pencil.strokeOpacity < 0 || this.tools.pencil.strokeOpacity > 1) {
103
+ this.tools.pencil.strokeOpacity = 1;
104
+ }
105
+
106
+ // Validate eraser config
107
+ if (this.tools.eraser.size <= 0) {
108
+ this.tools.eraser.size = 20;
109
+ }
110
+ if (this.tools.eraser.hardness < 0 || this.tools.eraser.hardness > 1) {
111
+ this.tools.eraser.hardness = 0.8;
112
+ }
113
+
114
+ // Validate select config
115
+ if (this.tools.select.selectionOpacity < 0 || this.tools.select.selectionOpacity > 1) {
116
+ this.tools.select.selectionOpacity = 0.3;
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Update configuration
122
+ * @param {Object} updates - Configuration updates
123
+ */
124
+ update(updates) {
125
+ Object.assign(this, updates);
126
+ this._validate();
127
+ }
128
+
129
+ /**
130
+ * Get tool configuration
131
+ * @param {string} toolName - Tool name
132
+ * @returns {Object} Tool configuration
133
+ */
134
+ getToolConfig(toolName) {
135
+ return this.tools[toolName] || {};
136
+ }
137
+
138
+ /**
139
+ * Update tool configuration
140
+ * @param {string} toolName - Tool name
141
+ * @param {Object} config - Tool configuration updates
142
+ */
143
+ updateToolConfig(toolName, config) {
144
+ if (!this.tools[toolName]) {
145
+ this.tools[toolName] = {};
146
+ }
147
+ Object.assign(this.tools[toolName], config);
148
+ this._validateToolConfigs();
149
+ }
150
+
151
+ /**
152
+ * Clone configuration
153
+ * @returns {CanvasConfig} New configuration instance
154
+ */
155
+ clone() {
156
+ return new CanvasConfig({
157
+ width: this.width,
158
+ height: this.height,
159
+ backgroundColor: this.backgroundColor,
160
+ showToolbar: this.showToolbar,
161
+ showGrid: this.showGrid,
162
+ gridSpacing: this.gridSpacing,
163
+ enabledTools: [...this.enabledTools],
164
+ defaultTool: this.defaultTool,
165
+ enableFocusFrames: this.enableFocusFrames,
166
+ enableKeyboardShortcuts: this.enableKeyboardShortcuts,
167
+ enableMultiTouch: this.enableMultiTouch,
168
+ tools: JSON.parse(JSON.stringify(this.tools)),
169
+ theme: { ...this.theme }
170
+ });
171
+ }
172
+
173
+ /**
174
+ * Convert to JSON
175
+ * @returns {Object} JSON representation
176
+ */
177
+ toJSON() {
178
+ return {
179
+ width: this.width,
180
+ height: this.height,
181
+ backgroundColor: this.backgroundColor,
182
+ showToolbar: this.showToolbar,
183
+ showGrid: this.showGrid,
184
+ gridSpacing: this.gridSpacing,
185
+ enabledTools: this.enabledTools,
186
+ defaultTool: this.defaultTool,
187
+ enableFocusFrames: this.enableFocusFrames,
188
+ enableKeyboardShortcuts: this.enableKeyboardShortcuts,
189
+ enableMultiTouch: this.enableMultiTouch,
190
+ tools: this.tools,
191
+ theme: this.theme
192
+ };
193
+ }
194
+
195
+ /**
196
+ * Create from JSON
197
+ * @param {Object} data - JSON data
198
+ * @returns {CanvasConfig} New configuration instance
199
+ */
200
+ static fromJSON(data) {
201
+ return new CanvasConfig(data);
202
+ }
203
203
  }