Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/shape/svg/svg_import.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,26 @@ export function SVGImportAddon(p5, fn, lifecycles) {
? this.num(node, "height")
: (referenced.height?.baseVal?.value || vb.height);

const scale = Math.min(w / vb.width, h / vb.height); // default: xMidYMid meet
this.tStack.current.translateSelf(
(w - vb.width * scale) / 2 - vb.x * scale,
(h - vb.height * scale) / 2 - vb.y * scale
);
this.tStack.current.scaleSelf(scale, scale);
const preserveAspectRatio = referenced.getAttribute("preserveAspectRatio");

if (preserveAspectRatio?.trim() === "none") {
const scaleX = w / vb.width;
const scaleY = h / vb.height;

this.tStack.current.translateSelf(
-vb.x * scaleX,
-vb.y * scaleY
);
this.tStack.current.scaleSelf(scaleX, scaleY);
} else {
const scale = Math.min(w / vb.width, h / vb.height); // default: xMidYMid meet

this.tStack.current.translateSelf(
(w - vb.width * scale) / 2 - vb.x * scale,
(h - vb.height * scale) / 2 - vb.y * scale
);
this.tStack.current.scaleSelf(scale, scale);
}
}
this.visit(referenced);
});
Expand Down Expand Up @@ -1623,4 +1637,4 @@ export function SVGImportAddon(p5, fn, lifecycles) {

if (typeof p5 !== 'undefined') {
p5.registerAddon(SVGImportAddon);
}
}
19 changes: 19 additions & 0 deletions test/unit/assets/svg/defs-symbol-preserve-aspect-ratio-none.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions test/unit/svg/svg_import.js
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,43 @@ suite('<defs> and <use> elements', function () {
});
});

test('use respects preserveAspectRatio=none', function () {
const record = createSVG(`
<svg xmlns="http://www.w3.org/2000/svg">
<defs>
<symbol id="stretch" viewBox="0 0 100 100" preserveAspectRatio="none">
<rect x="0" y="0" width="100" height="100" />
</symbol>
</defs>
<use href="#stretch" width="200" height="100" />
</svg>
`);
const scope = firstChild(record);
const node = scope.children[0];
assert.closeTo(node.state.transform.a, 2, 0.001);
assert.closeTo(node.state.transform.d, 1, 0.001);
});

test('use with preserveAspectRatio=none accounts for viewBox origin', function () {
const record = createSVG(`
<svg xmlns="http://www.w3.org/2000/svg">
<defs>
<symbol id="stretch-offset" viewBox="10 20 100 100" preserveAspectRatio="none">
<rect x="10" y="20" width="100" height="100" />
</symbol>
</defs>
<use href="#stretch-offset" width="200" height="100" />
</svg>
`);
const scope = firstChild(record);
const node = scope.children[0];

assert.closeTo(node.state.transform.a, 2, 0.001);
assert.closeTo(node.state.transform.d, 1, 0.001);
assert.closeTo(node.state.transform.e, -20, 0.001);
assert.closeTo(node.state.transform.f, -20, 0.001);
});

test('handles missing references gracefully', function () {
const record = createSVG(`
<svg xmlns="http://www.w3.org/2000/svg">
Expand Down
10 changes: 10 additions & 0 deletions test/unit/visual/cases/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,16 @@ visualSuite('svg', function () {
await screenshot();
});

visualTest('symbol preserveAspectRatio none', async (p, screenshot) => {
setupDefault(p);
const shape = await loadFixture(
p,
'defs-symbol-preserve-aspect-ratio-none'
);
p.shape(shape);
await screenshot();
});

visualTest('x and y', async (p, screenshot) => {
setupDefault(p);
const shape = await loadFixture(p, 'defs-use-xy');
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"numScreenshots": 1
}
Loading