Skip to content

Add optional center of rotation on setTransform #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,12 +905,13 @@
return matrix as [[number, number, number], [number, number, number]];
}

setTransform(posn?: SimplePoint, angle = 0, scale = 1) {
setTransform(posn?: SimplePoint, angle = 0, scale = 1, rotCenter?: SimplePoint) {
// TODO Safari only supports transform-origin and transform-box for CSS
// transforms, not the [transform=] attribute:
// https://stackoverflow.com/questions/61272308/
const center = rotCenter ?? {x: 0, y: 0};
const t1 = posn ? `translate(${roundTo(posn.x, 0.1)} ${roundTo(posn.y, 0.1)})` : '';
const t2 = nearlyEquals(angle, 0) ? '' : `rotate(${angle * 180 / Math.PI})`;
const t2 = nearlyEquals(angle, 0) ? '' : `rotate(${angle * 180 / Math.PI}, ${center.x} , ${center.y})`;
const t3 = nearlyEquals(scale, 1) ? '' : `scale(${scale})`;
this.setAttr('transform', [t1, t2, t3].join(' '));
}
Expand Down Expand Up @@ -1000,7 +1001,7 @@
if (!obj) return this.setAttr('d', '');
const attributes: SVGDrawingOptions = {};
for (const p of ['mark', 'arrows', 'round'] as const) {
if (this.hasAttr(p)) attributes[p] = this.attr(p) as any;

Check warning on line 1004 in src/elements.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18

Unexpected any. Specify a different type

Check warning on line 1004 in src/elements.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20

Unexpected any. Specify a different type
}
if (this.hasClass('fill')) attributes.fill = 'fill';
if (this.hasAttr('size')) attributes.size = (+this.attr('size')) || undefined;
Expand Down
Loading