@teachinglab/omd 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/canvas/tools/EraserTool.js +1 -1
- package/canvas/tools/PencilTool.js +1 -1
- package/canvas/tools/SelectTool.js +1 -1
- package/docs/api/configuration-options.md +198 -104
- package/docs/api/eventManager.md +83 -68
- package/docs/api/focusFrameManager.md +145 -150
- package/docs/api/index.md +106 -91
- package/docs/api/main.md +63 -58
- package/docs/api/omdBinaryExpressionNode.md +86 -227
- package/docs/api/omdCanvas.md +84 -142
- package/docs/api/omdConfigManager.md +113 -192
- package/docs/api/omdConstantNode.md +53 -117
- package/docs/api/omdDisplay.md +87 -121
- package/docs/api/omdEquationNode.md +174 -161
- package/docs/api/omdEquationSequenceNode.md +259 -301
- package/docs/api/omdEquationStack.md +157 -103
- package/docs/api/omdFunctionNode.md +83 -141
- package/docs/api/omdGroupNode.md +79 -182
- package/docs/api/omdHelpers.md +88 -96
- package/docs/api/omdLeafNode.md +86 -163
- package/docs/api/omdNode.md +202 -101
- package/docs/api/omdOperationDisplayNode.md +118 -139
- package/docs/api/omdOperatorNode.md +92 -127
- package/docs/api/omdParenthesisNode.md +134 -122
- package/docs/api/omdPopup.md +192 -117
- package/docs/api/omdPowerNode.md +132 -127
- package/docs/api/omdRationalNode.md +145 -128
- package/docs/api/omdSimplification.md +79 -110
- package/docs/api/omdSqrtNode.md +144 -79
- package/docs/api/omdStepVisualizer.md +147 -115
- package/docs/api/omdStepVisualizerHighlighting.md +66 -61
- package/docs/api/omdStepVisualizerInteractiveSteps.md +109 -129
- package/docs/api/omdStepVisualizerLayout.md +71 -60
- package/docs/api/omdStepVisualizerTextBoxes.md +77 -68
- package/docs/api/omdToolbar.md +131 -102
- package/docs/api/omdTranscriptionService.md +96 -76
- package/docs/api/omdTreeDiff.md +170 -134
- package/docs/api/omdUnaryExpressionNode.md +137 -174
- package/docs/api/omdUtilities.md +83 -70
- package/docs/api/omdVariableNode.md +123 -148
- package/index.js +2 -2
- package/package.json +1 -1
- /package/canvas/drawing/{segment.js → Segment.js} +0 -0
|
@@ -1,227 +1,86 @@
|
|
|
1
|
-
# omdBinaryExpressionNode
|
|
2
|
-
|
|
3
|
-
Represents binary
|
|
4
|
-
|
|
5
|
-
## Class
|
|
6
|
-
|
|
7
|
-
```javascript
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
-
|
|
84
|
-
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
#### `updateLayout()`
|
|
89
|
-
Updates positions of all elements.
|
|
90
|
-
- Aligns operands and operator on baseline
|
|
91
|
-
- Handles spacing for implicit multiplication
|
|
92
|
-
|
|
93
|
-
---
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
#### `getAlignmentBaseline()`
|
|
97
|
-
Gets vertical alignment point.
|
|
98
|
-
- **Returns:** number - Y-coordinate based on:
|
|
99
|
-
- Operator baseline if explicit
|
|
100
|
-
- Max child baseline if implicit
|
|
101
|
-
|
|
102
|
-
---
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
#### `clone()`
|
|
106
|
-
Creates a deep clone of the expression.
|
|
107
|
-
- **Returns:** omdBinaryExpressionNode - A new instance with:
|
|
108
|
-
- Cloned operands and operator
|
|
109
|
-
- Rebuilt argument list
|
|
110
|
-
- Copied AST data and operation
|
|
111
|
-
- Original node's ID added to provenance array
|
|
112
|
-
|
|
113
|
-
---
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
#### `getSpacing()`
|
|
117
|
-
Gets horizontal spacing between elements.
|
|
118
|
-
- **Returns:** number - 0 for implicit multiplication, otherwise scales with font size.
|
|
119
|
-
|
|
120
|
-
---
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
#### `_shouldUseImplicitMultiplication(left, right)` (internal)
|
|
125
|
-
Internal method to check if multiplication should be implicit.
|
|
126
|
-
- Based on configuration and operand types
|
|
127
|
-
- **Returns:** boolean
|
|
128
|
-
> Note: This method is intended for internal use and may change without notice.
|
|
129
|
-
|
|
130
|
-
---
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
#### `_shouldReorderMultiplication(left, right)` (internal)
|
|
134
|
-
Internal method to check if operands should be reordered for coefficient multiplication (e.g., x*2 → 2*x).
|
|
135
|
-
- **Returns:** boolean
|
|
136
|
-
> Note: This method is intended for internal use and may change without notice.
|
|
137
|
-
|
|
138
|
-
---
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
#### `toMathJSNode()`
|
|
142
|
-
Converts to math.js AST format.
|
|
143
|
-
- **Returns:** Object - A math.js-compatible AST node with:
|
|
144
|
-
- `type`: "OperatorNode"
|
|
145
|
-
- `op`: Operator symbol
|
|
146
|
-
- `fn`: Function name
|
|
147
|
-
- `args`: Operand ASTs
|
|
148
|
-
- `implicit`: Boolean flag
|
|
149
|
-
- `id`: Node ID
|
|
150
|
-
- `provenance`: Provenance array
|
|
151
|
-
|
|
152
|
-
---
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
#### `toString()`
|
|
156
|
-
Convert to string representation.
|
|
157
|
-
- **Returns:** string - Format depends on operation. Handles implicit multiplication and adds parentheses as needed for precedence.
|
|
158
|
-
|
|
159
|
-
---
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
#### `evaluate(variables)`
|
|
164
|
-
Evaluate the expression.
|
|
165
|
-
- `variables` {Object} - Variable name to value mapping
|
|
166
|
-
- **Returns:** number - The evaluated result
|
|
167
|
-
- **Throws:** Error if division by zero is encountered or if the operation is unsupported.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
#### `needsParentheses()`
|
|
171
|
-
Determines if parentheses are needed based on parent operator precedence.
|
|
172
|
-
- **Returns:** boolean
|
|
173
|
-
|
|
174
|
-
### Examples
|
|
175
|
-
|
|
176
|
-
```javascript
|
|
177
|
-
// Create from AST
|
|
178
|
-
const node = new omdBinaryExpressionNode({
|
|
179
|
-
type: 'OperatorNode',
|
|
180
|
-
op: '+',
|
|
181
|
-
fn: 'add',
|
|
182
|
-
args: [
|
|
183
|
-
{ type: 'SymbolNode', name: 'x' },
|
|
184
|
-
{ type: 'ConstantNode', value: 2 }
|
|
185
|
-
]
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
// Implicit multiplication
|
|
189
|
-
const implicitMult = new omdBinaryExpressionNode({
|
|
190
|
-
type: 'OperatorNode',
|
|
191
|
-
op: '*',
|
|
192
|
-
fn: 'multiply',
|
|
193
|
-
args: [
|
|
194
|
-
{ type: 'ConstantNode', value: 2 },
|
|
195
|
-
{ type: 'SymbolNode', name: 'x' }
|
|
196
|
-
],
|
|
197
|
-
implicit: true
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
// Division by zero (throws error)
|
|
201
|
-
try {
|
|
202
|
-
const divByZero = new omdBinaryExpressionNode({
|
|
203
|
-
type: 'OperatorNode',
|
|
204
|
-
op: '/',
|
|
205
|
-
fn: 'divide',
|
|
206
|
-
args: [
|
|
207
|
-
{ type: 'ConstantNode', value: 5 },
|
|
208
|
-
{ type: 'ConstantNode', value: 0 }
|
|
209
|
-
]
|
|
210
|
-
});
|
|
211
|
-
divByZero.evaluate();
|
|
212
|
-
} catch (e) {
|
|
213
|
-
console.error('Error:', e.message); // Error: Division by zero.
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// Render with proper spacing
|
|
217
|
-
node.setFontSize(24);
|
|
218
|
-
node.computeDimensions();
|
|
219
|
-
node.updateLayout();
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
### See Also
|
|
223
|
-
|
|
224
|
-
- [omdNode](./omdNode.md) - Parent class
|
|
225
|
-
- [omdOperatorNode](./omdOperatorNode.md) - For operators
|
|
226
|
-
- [omdConstantNode](./omdConstantNode.md) - For numeric operands
|
|
227
|
-
- [omdVariableNode](./omdVariableNode.md) - For variable operands
|
|
1
|
+
# omdBinaryExpressionNode
|
|
2
|
+
|
|
3
|
+
Represents a binary operation in a mathematical expression, such as addition (`a + b`), subtraction, multiplication, or division. This node is a cornerstone of the expression tree, containing a left operand, a right operand, and an operator.
|
|
4
|
+
|
|
5
|
+
## Class Definition
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
export class omdBinaryExpressionNode extends omdNode
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Constructor
|
|
12
|
+
|
|
13
|
+
### `new omdBinaryExpressionNode(ast)`
|
|
14
|
+
|
|
15
|
+
Creates a new `omdBinaryExpressionNode` instance.
|
|
16
|
+
|
|
17
|
+
- **`ast`** (`object`): The abstract syntax tree (AST) node from a parser like math.js. It must contain:
|
|
18
|
+
- `args`: An array with at least two operand nodes.
|
|
19
|
+
- `op`: The operator symbol (e.g., `+`, `*`).
|
|
20
|
+
- `fn`: The function name for the operation (e.g., `add`, `multiply`).
|
|
21
|
+
- `implicit` (`boolean`, optional): `true` if the multiplication is implicit (e.g., `2x`).
|
|
22
|
+
|
|
23
|
+
During construction, the node automatically handles a critical piece of mathematical convention: **implicit multiplication**. Based on the configuration in `omdConfigManager`, it will:
|
|
24
|
+
|
|
25
|
+
1. **Reorder Operands**: If it encounters an expression like `x * 2`, it will automatically reorder it to the conventional `2 * x` by swapping the left and right child nodes.
|
|
26
|
+
2. **Determine Implicit Form**: It checks if the combination of operands (e.g., a constant and a variable) should be represented implicitly. If so, it removes the visible operator (`*`) and marks the node as implicit.
|
|
27
|
+
|
|
28
|
+
## Public Properties
|
|
29
|
+
|
|
30
|
+
- **`left`** ([`omdNode`](./omdNode.md)): The node representing the left-hand side of the operation.
|
|
31
|
+
- **`right`** ([`omdNode`](./omdNode.md)): The node representing the right-hand side of the operation.
|
|
32
|
+
- **`op`** ([`omdOperatorNode`](./omdOperatorNode.md) | `null`): The node for the operator. This is `null` for implicit multiplication.
|
|
33
|
+
- **`operation`** (`string`): The name of the mathematical function (e.g., `'add'`, `'multiply'`).
|
|
34
|
+
- **`isImplicit`** (`boolean`): `true` if the node represents implicit multiplication.
|
|
35
|
+
|
|
36
|
+
## Public Methods
|
|
37
|
+
|
|
38
|
+
### `clone()`
|
|
39
|
+
|
|
40
|
+
Creates a deep, recursive clone of the node, including its children (`left`, `right`, `op`). The new node's `provenance` property will link back to the ID of this original node.
|
|
41
|
+
|
|
42
|
+
- **Returns**: A new `omdBinaryExpressionNode` instance.
|
|
43
|
+
|
|
44
|
+
### `evaluate(variables)`
|
|
45
|
+
|
|
46
|
+
Recursively evaluates the expression and returns the numerical result.
|
|
47
|
+
|
|
48
|
+
- **`variables`** (`object`, optional): A map of variable names to their numeric values (e.g., `{ x: 5 }`).
|
|
49
|
+
- **Returns**: `number` - The result of the calculation.
|
|
50
|
+
- **Throws**: An `Error` for unsupported operations or division by zero.
|
|
51
|
+
|
|
52
|
+
### `needsParentheses()`
|
|
53
|
+
|
|
54
|
+
Determines if this expression needs to be wrapped in parentheses to maintain the correct order of operations when it is a child of another binary expression.
|
|
55
|
+
|
|
56
|
+
- **Returns**: `boolean` - `true` if parentheses are required.
|
|
57
|
+
|
|
58
|
+
### `setHighlight(highlightOn, color)`
|
|
59
|
+
|
|
60
|
+
Applies or removes a highlight from the node and all of its children (left, right, and operator).
|
|
61
|
+
|
|
62
|
+
- **`highlightOn`** (`boolean`): `true` to highlight, `false` to remove.
|
|
63
|
+
- **`color`** (`string`, optional): The color of the highlight.
|
|
64
|
+
|
|
65
|
+
### `clearProvenanceHighlights()`
|
|
66
|
+
|
|
67
|
+
Recursively clears all provenance-related highlights from this node and its children.
|
|
68
|
+
|
|
69
|
+
### `toMathJSNode()`
|
|
70
|
+
|
|
71
|
+
Converts the node back into a math.js-compatible AST format.
|
|
72
|
+
|
|
73
|
+
- **Returns**: `object` - A math.js OperatorNode.
|
|
74
|
+
|
|
75
|
+
### `toString()`
|
|
76
|
+
|
|
77
|
+
Converts the node into a human-readable string, automatically handling parentheses for precedence.
|
|
78
|
+
|
|
79
|
+
- **Returns**: `string` - The string representation of the expression.
|
|
80
|
+
|
|
81
|
+
## Internal Methods
|
|
82
|
+
|
|
83
|
+
- **`_shouldUseImplicitMultiplication(left, right)`**: Checks the configuration to decide if implicit multiplication is appropriate for the given operand types.
|
|
84
|
+
- **`_shouldReorderMultiplication(left, right)`**: Determines if operands should be swapped to follow convention (e.g., `variable * constant` -> `constant * variable`).
|
|
85
|
+
- **`computeDimensions()`**: Calculates the bounding box of the expression.
|
|
86
|
+
- **`updateLayout()`**: Positions the left operand, operator, and right operand correctly, aligning them along their baseline.
|
package/docs/api/omdCanvas.md
CHANGED
|
@@ -1,142 +1,84 @@
|
|
|
1
|
-
# omdCanvas
|
|
2
|
-
|
|
3
|
-
The `omdCanvas` class is the core component for creating and managing
|
|
4
|
-
|
|
5
|
-
## Class Definition
|
|
6
|
-
|
|
7
|
-
```javascript
|
|
8
|
-
export class omdCanvas
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
* **Returns**: `Promise<Blob>` - A promise that resolves with the image data as a `Blob`.
|
|
86
|
-
|
|
87
|
-
### `resize(width, height)`
|
|
88
|
-
|
|
89
|
-
Resizes the canvas to the specified dimensions.
|
|
90
|
-
|
|
91
|
-
* **width** (`number`): The new width of the canvas.
|
|
92
|
-
* **height** (`number`): The new height of the canvas.
|
|
93
|
-
|
|
94
|
-
### `toggleGrid()`
|
|
95
|
-
|
|
96
|
-
Toggles the visibility of the background grid.
|
|
97
|
-
|
|
98
|
-
### `on(event, callback)`
|
|
99
|
-
|
|
100
|
-
Registers an event listener for a custom canvas event.
|
|
101
|
-
|
|
102
|
-
* **event** (`string`): The name of the event to listen for.
|
|
103
|
-
* **callback** (`Function`): The callback function to execute when the event is emitted.
|
|
104
|
-
|
|
105
|
-
### `off(event, callback)`
|
|
106
|
-
|
|
107
|
-
Removes an event listener.
|
|
108
|
-
|
|
109
|
-
* **event** (`string`): The name of the event.
|
|
110
|
-
* **callback** (`Function`): The callback function to remove.
|
|
111
|
-
|
|
112
|
-
### `emit(event, [data])`
|
|
113
|
-
|
|
114
|
-
Emits a custom canvas event.
|
|
115
|
-
|
|
116
|
-
* **event** (`string`): The name of the event to emit.
|
|
117
|
-
* **[data]** (`object`, optional): Optional data to pass with the event.
|
|
118
|
-
|
|
119
|
-
### `getInfo()`
|
|
120
|
-
|
|
121
|
-
Gets information about the current state of the canvas.
|
|
122
|
-
|
|
123
|
-
* **Returns**: `object` - An object containing canvas dimensions, stroke counts, active tool, and other status information.
|
|
124
|
-
|
|
125
|
-
### `destroy()`
|
|
126
|
-
|
|
127
|
-
Destroys the canvas instance, removes all DOM elements, and cleans up event listeners and managers.
|
|
128
|
-
|
|
129
|
-
## Events
|
|
130
|
-
|
|
131
|
-
The `omdCanvas` emits the following custom events:
|
|
132
|
-
|
|
133
|
-
* `strokeAdded`: Fired when a new stroke is added to the canvas.
|
|
134
|
-
* `strokeRemoved`: Fired when a stroke is removed from the canvas.
|
|
135
|
-
* `cleared`: Fired when all strokes are cleared from the canvas.
|
|
136
|
-
* `selectionChanged`: Fired when the selection of strokes changes.
|
|
137
|
-
* `resized`: Fired when the canvas is resized.
|
|
138
|
-
* `pointerDown`, `pointerMove`, `pointerUp`, `pointerCancel`, `pointerEnter`, `pointerLeave`: Pointer events, normalized to canvas coordinates.
|
|
139
|
-
* `keyDown`, `keyUp`: Keyboard events.
|
|
140
|
-
* `wheel`: Wheel events.
|
|
141
|
-
* `focusFrameCreated`, `focusFrameRemoved`, `focusFrameActivated`, `focusFramesCleared`: Events related to focus frame management.
|
|
142
|
-
* `destroyed`: Fired when the canvas instance is destroyed.
|
|
1
|
+
# omdCanvas
|
|
2
|
+
|
|
3
|
+
The `omdCanvas` class is the core component for creating and managing an interactive 2D drawing surface. It provides the primary API for all canvas operations, including drawing and managing strokes, handling user input, and orchestrating various features like tools, a toolbar, and focus frames.
|
|
4
|
+
|
|
5
|
+
## Class Definition
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
export class omdCanvas
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Constructor
|
|
12
|
+
|
|
13
|
+
### `new omdCanvas(container, [options])`
|
|
14
|
+
|
|
15
|
+
Creates a new `omdCanvas` instance within a specified container element.
|
|
16
|
+
|
|
17
|
+
- **`container`** (`HTMLElement` | `string`): The HTML element or a CSS selector string for the container where the canvas will be rendered. Throws an error if the container is not found.
|
|
18
|
+
- **`options`** (`object`, optional): Configuration options for the canvas. These are passed to the `CanvasConfig` class. See the [Configuration Options](./configuration-options.md) for details.
|
|
19
|
+
|
|
20
|
+
## Properties
|
|
21
|
+
|
|
22
|
+
- **`container`** (`HTMLElement`): The HTML container element for the canvas.
|
|
23
|
+
- **`config`** (`CanvasConfig`): The configuration object for the canvas.
|
|
24
|
+
- **`svg`** (`SVGElement`): The main `<svg>` element that serves as the drawing surface.
|
|
25
|
+
- **`backgroundLayer`** (`SVGGElement`): An SVG group (`<g>`) for background elements like the grid.
|
|
26
|
+
- **`drawingLayer`** (`SVGGElement`): The SVG group where all drawing strokes are rendered.
|
|
27
|
+
- **`uiLayer`** (`SVGGElement`): The SVG group for UI elements that overlay the drawing, such as selection boxes.
|
|
28
|
+
- **`focusFrameLayer`** (`SVGGElement`): The SVG group dedicated to holding `FocusFrame` elements.
|
|
29
|
+
- **`strokes`** (`Map<string, Stroke>`): A map of all stroke objects on the canvas, with their unique IDs as keys.
|
|
30
|
+
- **`selectedStrokes`** (`Set<string>`): A set of the IDs of all currently selected strokes.
|
|
31
|
+
- **`toolManager`** (`ToolManager`): The instance managing the active drawing tools (Pencil, Eraser, etc.).
|
|
32
|
+
- **`eventManager`** (`EventManager`): The instance managing all user input events.
|
|
33
|
+
- **`cursor`** (`Cursor`): The instance that manages the custom cursor's appearance and behavior.
|
|
34
|
+
- **`toolbar`** (`Toolbar`): The UI toolbar for tool selection (if enabled in config).
|
|
35
|
+
- **`focusFrameManager`** (`FocusFrameManager`): The instance managing focus frames (if enabled in config).
|
|
36
|
+
|
|
37
|
+
## Public Methods
|
|
38
|
+
|
|
39
|
+
### Stroke Management
|
|
40
|
+
|
|
41
|
+
- **`addStroke(stroke)`**: Adds a `Stroke` object to the canvas and renders it. Emits `strokeAdded`.
|
|
42
|
+
- **`removeStroke(strokeId)`**: Removes a stroke from the canvas by its ID. Emits `strokeRemoved`.
|
|
43
|
+
- **`clear()`**: Removes all strokes from the canvas. Emits `cleared`.
|
|
44
|
+
|
|
45
|
+
### Selection
|
|
46
|
+
|
|
47
|
+
- **`selectStrokes(strokeIds)`**: Programmatically selects a set of strokes by their IDs. Emits `selectionChanged`.
|
|
48
|
+
|
|
49
|
+
### Coordinate Conversion
|
|
50
|
+
|
|
51
|
+
- **`clientToSVG(clientX, clientY)`**: Converts screen coordinates (e.g., from a mouse event) to the canvas's local SVG coordinate system.
|
|
52
|
+
- **Returns**: `{ x, y }` object.
|
|
53
|
+
|
|
54
|
+
### Exporting
|
|
55
|
+
|
|
56
|
+
- **`exportSVG()`**: Exports the canvas drawing as a clean SVG string. UI layers (`uiLayer`, `focusFrameLayer`) are excluded.
|
|
57
|
+
- **`async exportImage([format], [quality])`**: Exports the canvas as a bitmap image.
|
|
58
|
+
- **`format`** (`string`): `'png'`, `'jpeg'`, or `'webp'`. Default: `'png'`.
|
|
59
|
+
- **`quality`** (`number`): For JPEG/WebP, a value from 0 to 1. Default: `1`.
|
|
60
|
+
- **Returns**: `Promise<Blob>` that resolves with the image data.
|
|
61
|
+
|
|
62
|
+
### Canvas Management
|
|
63
|
+
|
|
64
|
+
- **`resize(width, height)`**: Resizes the canvas. Emits `resized`.
|
|
65
|
+
- **`toggleGrid()`**: Toggles the visibility of the background grid.
|
|
66
|
+
- **`getInfo()`**: Returns an object with status information (dimensions, stroke count, active tool, etc.).
|
|
67
|
+
- **`destroy()`**: Completely removes the canvas and all associated managers, event listeners, and DOM elements. Emits `destroyed`.
|
|
68
|
+
|
|
69
|
+
### Event Handling (Pub/Sub)
|
|
70
|
+
|
|
71
|
+
- **`on(event, callback)`**: Registers a listener for a custom canvas event.
|
|
72
|
+
- **`off(event, callback)`**: Removes a previously registered event listener.
|
|
73
|
+
- **`emit(event, [data])`**: Dispatches a custom event to all registered listeners.
|
|
74
|
+
|
|
75
|
+
## Emitted Events
|
|
76
|
+
|
|
77
|
+
The `omdCanvas` is an event emitter. You can listen for these events using the `on()` method.
|
|
78
|
+
|
|
79
|
+
- `strokeAdded`, `strokeRemoved`, `cleared`: Fired for stroke operations.
|
|
80
|
+
- `selectionChanged`: Fired when the set of selected strokes changes.
|
|
81
|
+
- `resized`: Fired when the canvas dimensions change.
|
|
82
|
+
- `pointerDown`, `pointerMove`, `pointerUp`, etc.: Raw input events, normalized by the `EventManager`.
|
|
83
|
+
- `focusFrameCreated`, `focusFrameRemoved`, etc.: Events from the `FocusFrameManager`.
|
|
84
|
+
- `destroyed`: Fired when the `destroy()` method has completed.
|