Skip to content

Commit 72c9d4c

Browse files
authored
Merge pull request #32 from bertyhell/fix-svg-image-export
Fix: Ensure correct image transform in SVG export
2 parents b89cb4d + d6736b1 commit 72c9d4c

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/drawControllers/svg.drawController.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,26 @@ export class SvgDrawController implements DrawController {
237237
ctx.drawImage(imageElement, 0, 0);
238238
const dataUri = canvas.toDataURL(); // Convert the image to Base64
239239

240-
const transform = angle
241-
? `transform="rotate(${angle}, ${xMin + width / 2}, ${yMin + height / 2})"`
242-
: '';
240+
const svgWidth = width * this.getScreenScale();
241+
const svgHeight = height * this.getScreenScale();
242+
243+
const worldCenterX = xMin + width / 2;
244+
const worldCenterY = yMin + height / 2;
245+
246+
const targetCenter = this.worldToTarget(new Point(worldCenterX, worldCenterY));
247+
248+
const svgX = targetCenter.x - svgWidth / 2;
249+
const svgY = targetCenter.y - svgHeight / 2;
250+
251+
let transformAttribute = '';
252+
if (angle !== 0) {
253+
const svgAngleDegrees = angle * (180 / Math.PI);
254+
transformAttribute = `transform="rotate(${svgAngleDegrees}, ${targetCenter.x}, ${targetCenter.y})"`;
255+
}
243256

244257
// noinspection HtmlUnknownAttribute
245258
this.svgStrings.push(
246-
`<image href="${dataUri}" x="${xMin}" y="${yMin}" width="${width}" height="${height}" ${transform} />`
259+
`<image href="${dataUri}" x="${svgX}" y="${svgY}" width="${svgWidth}" height="${svgHeight}" ${transformAttribute} />`
247260
);
248261
}
249262

0 commit comments

Comments
 (0)