@teachinglab/omd 0.7.13 → 0.7.15
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/canvas/tools/PointerTool.js +1280 -29
- package/jsvg/jsvg.js +272 -36
- package/jsvg/package.json +13 -0
- 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
|
+
}
|