Skip to content

Commit 1db1a2c

Browse files
committed
Canvas dims + clipboard fixes
1 parent 90b6b07 commit 1db1a2c

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

web/index.html

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,13 +1154,25 @@ <h3 class="font-bold text-gray-900 mb-1 uppercase text-xs tracking-wider">MIT Li
11541154
textInfos.push({ bbox, ctm, fill, text, fontSize, bold, italic, align });
11551155
});
11561156

1157-
// Resize the .note canvas to match the SVG's aspect ratio.
1158-
// Uses generous padding to prevent strokes from being clipped at edges
1159-
// (getBBox excludes stroke widths, so strokes extend beyond the bbox).
1157+
// Resize the .note canvas to match the SVG's coordinate space.
1158+
// Prefer viewBox (defines intended coordinate system) over getBBox
1159+
// (tight content bounds) for sizing — getBBox gives wrong dimensions
1160+
// when content doesn't fill the full viewBox.
11601161
let scaleX = 1, scaleY = 1, offsetX = 0, offsetY = 0;
11611162
try {
1162-
const bbox = svgEl.getBBox();
1163-
window._lastSvgBBox = bbox ? { x: bbox.x, y: bbox.y, width: bbox.width, height: bbox.height } : null;
1163+
// Use viewBox if available, fall back to getBBox for content bounds
1164+
let bbox;
1165+
const vb = svgEl.getAttribute('viewBox');
1166+
if (vb) {
1167+
const parts = vb.trim().split(/[\s,]+/).map(Number);
1168+
if (parts.length === 4 && parts[2] > 0 && parts[3] > 0)
1169+
bbox = { x: parts[0], y: parts[1], width: parts[2], height: parts[3] };
1170+
}
1171+
if (!bbox) {
1172+
const gb = svgEl.getBBox();
1173+
bbox = gb ? { x: gb.x, y: gb.y, width: gb.width, height: gb.height } : null;
1174+
}
1175+
window._lastSvgBBox = bbox;
11641176
if (bbox && bbox.width > 0 && bbox.height > 0) {
11651177
const padding = 100;
11661178
const maxDim = 2480;
@@ -2034,11 +2046,14 @@ <h3 class="font-bold text-gray-900 mb-1 uppercase text-xs tracking-wider">MIT Li
20342046
// gives a fresh gesture activation, needed by Safari for clipboard + window.open.
20352047
if (!await styledConfirm("This will open a drawing editor in a new tab and copy the SVG to your clipboard.\n\nOnce the empty online editor opens, click File \u2192 New From Clipboard (or Cmd+V / Ctrl+V).\n\nYou can also download the SVG and open it in any online or desktop editor (e.g. Inkscape).")) return;
20362048

2037-
// Both must be called synchronously within the confirm gesture —
2038-
// any await before these would expire Safari's user activation.
2039-
const svgBlobPromise = generateSVG().then(svg => new Blob([svg], {type: 'text/plain'}));
2040-
navigator.clipboard.write([new ClipboardItem({'text/plain': svgBlobPromise})]);
2049+
// Open editor first (needs user activation for popup), then clipboard.
20412050
window.open('https://boxy-svg.com/app', '_blank');
2051+
try {
2052+
const svg = await generateSVG();
2053+
await navigator.clipboard.writeText(svg);
2054+
} catch(e) {
2055+
alert('Could not copy SVG to clipboard: ' + e.message + '\n\nYou can use the Download SVG button instead.');
2056+
}
20422057
};
20432058

20442059
})().catch(function(err) {

0 commit comments

Comments
 (0)