@teachinglab/omd 0.7.12 → 0.7.14
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/core/omdCanvas.js +76 -5
- package/omd/nodes/omdEquationNode.js +71 -5
- package/package.json +1 -1
package/canvas/core/omdCanvas.js
CHANGED
|
@@ -9,6 +9,62 @@ import { Cursor } from '../ui/cursor.js';
|
|
|
9
9
|
import { Toolbar } from '../ui/toolbar.js';
|
|
10
10
|
import { FocusFrameManager } from '../features/focusFrameManager.js';
|
|
11
11
|
import {jsvgGroup} from '@teachinglab/jsvg'
|
|
12
|
+
|
|
13
|
+
// Create a clone that is safe to draw onto an HTML canvas.
|
|
14
|
+
// Chrome will taint canvases if an SVG contains <foreignObject> nodes,
|
|
15
|
+
// so we replace them with basic <text> nodes during export.
|
|
16
|
+
function cloneSVGForExport(svgRoot, options = {}) {
|
|
17
|
+
const { stripForeignObjects = true } = options;
|
|
18
|
+
const clone = svgRoot.cloneNode(true);
|
|
19
|
+
|
|
20
|
+
if (stripForeignObjects) {
|
|
21
|
+
const ns = 'http://www.w3.org/2000/svg';
|
|
22
|
+
clone.querySelectorAll('foreignObject').forEach((fo) => {
|
|
23
|
+
const parent = fo.parentNode;
|
|
24
|
+
if (!parent) return;
|
|
25
|
+
|
|
26
|
+
const htmlEl = fo.firstElementChild;
|
|
27
|
+
const textContent = (htmlEl?.innerText || htmlEl?.textContent || '').trim();
|
|
28
|
+
const textEl = document.createElementNS(ns, 'text');
|
|
29
|
+
textEl.textContent = textContent;
|
|
30
|
+
|
|
31
|
+
const x = parseFloat(fo.getAttribute('x') || '0');
|
|
32
|
+
const y = parseFloat(fo.getAttribute('y') || '0');
|
|
33
|
+
const width = parseFloat(fo.getAttribute('width') || '0');
|
|
34
|
+
const fontFamily = htmlEl?.style?.fontFamily || 'sans-serif';
|
|
35
|
+
const fontSizeStr = htmlEl?.style?.fontSize || '16px';
|
|
36
|
+
const fontSize = parseFloat(fontSizeStr) || 16;
|
|
37
|
+
const fontWeight = htmlEl?.style?.fontWeight || 'normal';
|
|
38
|
+
const fill = htmlEl?.style?.color || 'black';
|
|
39
|
+
const textAlign = htmlEl?.style?.textAlign || 'start';
|
|
40
|
+
|
|
41
|
+
textEl.setAttribute('font-family', fontFamily);
|
|
42
|
+
textEl.setAttribute('font-size', `${fontSize}px`);
|
|
43
|
+
textEl.setAttribute('font-weight', fontWeight);
|
|
44
|
+
textEl.setAttribute('fill', fill);
|
|
45
|
+
textEl.setAttribute('dominant-baseline', 'hanging');
|
|
46
|
+
|
|
47
|
+
// Approximate alignment using the known foreignObject width
|
|
48
|
+
let textX = x;
|
|
49
|
+
if (!isNaN(width) && width > 0) {
|
|
50
|
+
if (textAlign === 'center') {
|
|
51
|
+
textX = x + width / 2;
|
|
52
|
+
textEl.setAttribute('text-anchor', 'middle');
|
|
53
|
+
} else if (textAlign === 'end' || textAlign === 'right') {
|
|
54
|
+
textX = x + width;
|
|
55
|
+
textEl.setAttribute('text-anchor', 'end');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
textEl.setAttribute('x', textX);
|
|
59
|
+
// Bump y by font size so the text sits inside the original box
|
|
60
|
+
textEl.setAttribute('y', y + fontSize);
|
|
61
|
+
|
|
62
|
+
parent.replaceChild(textEl, fo);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return clone;
|
|
67
|
+
}
|
|
12
68
|
/**
|
|
13
69
|
* Main OMD Canvas class
|
|
14
70
|
* Provides the primary interface for creating and managing a drawing canvas
|
|
@@ -301,10 +357,12 @@ export class omdCanvas {
|
|
|
301
357
|
|
|
302
358
|
/**
|
|
303
359
|
* Export canvas as SVG string
|
|
360
|
+
* @param {Object} options - Export options
|
|
361
|
+
* @param {boolean} options.stripForeignObjects - Replace <foreignObject> with <text> for canvas-safe exports (default true)
|
|
304
362
|
* @returns {string} SVG content
|
|
305
363
|
*/
|
|
306
|
-
exportSVG() {
|
|
307
|
-
const svgClone = this.svg
|
|
364
|
+
exportSVG(options = {}) {
|
|
365
|
+
const svgClone = cloneSVGForExport(this.svg, options);
|
|
308
366
|
|
|
309
367
|
// Remove UI elements from export
|
|
310
368
|
const uiLayer = svgClone.querySelector('.ui-layer');
|
|
@@ -320,16 +378,29 @@ export class omdCanvas {
|
|
|
320
378
|
* Export canvas as image
|
|
321
379
|
* @param {string} format - Image format (png, jpeg, webp)
|
|
322
380
|
* @param {number} quality - Image quality (0-1)
|
|
381
|
+
* @param {Object} options - Export options passed to exportSVG (e.g., { stripForeignObjects: true })
|
|
323
382
|
* @returns {Promise<Blob>} Image blob
|
|
324
383
|
*/
|
|
325
|
-
async exportImage(format = 'png', quality = 1) {
|
|
326
|
-
|
|
384
|
+
async exportImage(format = 'png', quality = 1, options = {}) {
|
|
385
|
+
// Allow passing options as the second argument for backward compatibility
|
|
386
|
+
if (typeof quality === 'object' && quality !== null) {
|
|
387
|
+
options = quality;
|
|
388
|
+
quality = 1;
|
|
389
|
+
}
|
|
390
|
+
if (typeof format === 'object' && format !== null) {
|
|
391
|
+
options = format;
|
|
392
|
+
format = 'png';
|
|
393
|
+
quality = 1;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const svgData = this.exportSVG(options);
|
|
327
397
|
const canvas = document.createElement('canvas');
|
|
328
398
|
canvas.width = this.config.width;
|
|
329
399
|
canvas.height = this.config.height;
|
|
330
400
|
|
|
331
401
|
const ctx = canvas.getContext('2d');
|
|
332
402
|
const img = new Image();
|
|
403
|
+
img.crossOrigin = 'anonymous';
|
|
333
404
|
|
|
334
405
|
const svgBlob = new Blob([svgData], { type: 'image/svg+xml' });
|
|
335
406
|
const url = URL.createObjectURL(svgBlob);
|
|
@@ -481,4 +552,4 @@ export class omdCanvas {
|
|
|
481
552
|
this.isDestroyed = true;
|
|
482
553
|
this.emit('destroyed');
|
|
483
554
|
}
|
|
484
|
-
}
|
|
555
|
+
}
|
|
@@ -1121,14 +1121,26 @@ _propagateBackgroundStyle(style, visited = new Set()) {
|
|
|
1121
1121
|
* @private
|
|
1122
1122
|
*/
|
|
1123
1123
|
_renderToTable(options) {
|
|
1124
|
+
// Helper to format expression - if it already has "y =" or starts with variable, use as-is, else prepend "y ="
|
|
1125
|
+
const formatEquation = (expr) => {
|
|
1126
|
+
const normalized = this._normalizeExpressionString(expr);
|
|
1127
|
+
// Check if expression already has "y =" or other variable assignment
|
|
1128
|
+
if (/^[a-zA-Z]\s*=/.test(normalized)) {
|
|
1129
|
+
return normalized;
|
|
1130
|
+
}
|
|
1131
|
+
// Otherwise prepend "y ="
|
|
1132
|
+
return `y = ${normalized}`;
|
|
1133
|
+
};
|
|
1134
|
+
|
|
1124
1135
|
// Single side: let omdTable generate rows from equation
|
|
1125
1136
|
if (options.side === 'left') {
|
|
1126
1137
|
const expr = this._normalizeExpressionString(this.getLeft().toString());
|
|
1138
|
+
const equation = formatEquation(expr);
|
|
1127
1139
|
return {
|
|
1128
1140
|
omdType: "table",
|
|
1129
|
-
title: `Function Table:
|
|
1141
|
+
title: `Function Table: ${equation}`,
|
|
1130
1142
|
headers: ["x", "y"],
|
|
1131
|
-
equation:
|
|
1143
|
+
equation: equation,
|
|
1132
1144
|
xMin: options.xMin,
|
|
1133
1145
|
xMax: options.xMax,
|
|
1134
1146
|
stepSize: options.stepSize,
|
|
@@ -1145,11 +1157,65 @@ _propagateBackgroundStyle(style, visited = new Set()) {
|
|
|
1145
1157
|
};
|
|
1146
1158
|
} else if (options.side === 'right') {
|
|
1147
1159
|
const expr = this._normalizeExpressionString(this.getRight().toString());
|
|
1160
|
+
const equation = formatEquation(expr);
|
|
1161
|
+
return {
|
|
1162
|
+
omdType: "table",
|
|
1163
|
+
title: `Function Table: ${equation}`,
|
|
1164
|
+
headers: ["x", "y"],
|
|
1165
|
+
equation: equation,
|
|
1166
|
+
xMin: options.xMin,
|
|
1167
|
+
xMax: options.xMax,
|
|
1168
|
+
stepSize: options.stepSize,
|
|
1169
|
+
// Background customization options
|
|
1170
|
+
backgroundColor: (options.backgroundColor !== undefined) ? options.backgroundColor : undefined,
|
|
1171
|
+
backgroundCornerRadius: (options.backgroundCornerRadius !== undefined) ? options.backgroundCornerRadius : undefined,
|
|
1172
|
+
backgroundOpacity: (options.backgroundOpacity !== undefined) ? options.backgroundOpacity : undefined,
|
|
1173
|
+
showBackground: (options.showBackground !== undefined) ? options.showBackground : undefined,
|
|
1174
|
+
// Alternating row color options
|
|
1175
|
+
alternatingRowColors: (options.alternatingRowColors !== undefined) ? options.alternatingRowColors : undefined,
|
|
1176
|
+
evenRowColor: (options.evenRowColor !== undefined) ? options.evenRowColor : undefined,
|
|
1177
|
+
oddRowColor: (options.oddRowColor !== undefined) ? options.oddRowColor : undefined,
|
|
1178
|
+
alternatingRowOpacity: (options.alternatingRowOpacity !== undefined) ? options.alternatingRowOpacity : undefined
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
// Both sides: use equation format for proper table generation
|
|
1183
|
+
// The omdTable class can handle this better than manually computing data
|
|
1184
|
+
const leftExpr = this._normalizeExpressionString(this.getLeft().toString());
|
|
1185
|
+
const rightExpr = this._normalizeExpressionString(this.getRight().toString());
|
|
1186
|
+
|
|
1187
|
+
// If left side is just "y", use right side as the equation
|
|
1188
|
+
if (leftExpr.toLowerCase().trim() === 'y') {
|
|
1189
|
+
const equation = formatEquation(rightExpr);
|
|
1190
|
+
return {
|
|
1191
|
+
omdType: "table",
|
|
1192
|
+
title: `Function Table: ${equation}`,
|
|
1193
|
+
headers: ["x", "y"],
|
|
1194
|
+
equation: equation,
|
|
1195
|
+
xMin: options.xMin,
|
|
1196
|
+
xMax: options.xMax,
|
|
1197
|
+
stepSize: options.stepSize,
|
|
1198
|
+
// Background customization options
|
|
1199
|
+
backgroundColor: (options.backgroundColor !== undefined) ? options.backgroundColor : undefined,
|
|
1200
|
+
backgroundCornerRadius: (options.backgroundCornerRadius !== undefined) ? options.backgroundCornerRadius : undefined,
|
|
1201
|
+
backgroundOpacity: (options.backgroundOpacity !== undefined) ? options.backgroundOpacity : undefined,
|
|
1202
|
+
showBackground: (options.showBackground !== undefined) ? options.showBackground : undefined,
|
|
1203
|
+
// Alternating row color options
|
|
1204
|
+
alternatingRowColors: (options.alternatingRowColors !== undefined) ? options.alternatingRowColors : undefined,
|
|
1205
|
+
evenRowColor: (options.evenRowColor !== undefined) ? options.evenRowColor : undefined,
|
|
1206
|
+
oddRowColor: (options.oddRowColor !== undefined) ? options.oddRowColor : undefined,
|
|
1207
|
+
alternatingRowOpacity: (options.alternatingRowOpacity !== undefined) ? options.alternatingRowOpacity : undefined
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
// If right side is just "y", use left side as the equation
|
|
1212
|
+
if (rightExpr.toLowerCase().trim() === 'y') {
|
|
1213
|
+
const equation = formatEquation(leftExpr);
|
|
1148
1214
|
return {
|
|
1149
1215
|
omdType: "table",
|
|
1150
|
-
title: `Function Table:
|
|
1216
|
+
title: `Function Table: ${equation}`,
|
|
1151
1217
|
headers: ["x", "y"],
|
|
1152
|
-
equation:
|
|
1218
|
+
equation: equation,
|
|
1153
1219
|
xMin: options.xMin,
|
|
1154
1220
|
xMax: options.xMax,
|
|
1155
1221
|
stepSize: options.stepSize,
|
|
@@ -1166,7 +1232,7 @@ _propagateBackgroundStyle(style, visited = new Set()) {
|
|
|
1166
1232
|
};
|
|
1167
1233
|
}
|
|
1168
1234
|
|
|
1169
|
-
// Both sides: compute data for x, left(x), right(x)
|
|
1235
|
+
// Both sides have expressions: compute data for x, left(x), right(x)
|
|
1170
1236
|
const leftSide = this.getLeft();
|
|
1171
1237
|
const rightSide = this.getRight();
|
|
1172
1238
|
const leftLabel = leftSide.toString();
|