Skip to content

Plot.image rotate option #1084

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1331,10 +1331,11 @@ In addition to the [standard mark options](#marks), the following optional chann
* **y** - the vertical position; bound to the *y* scale
* **width** - the image width (in pixels)
* **height** - the image height (in pixels)
* **rotate** - the image rotation angle (in degrees, defaults to 0)

If either of the **x** or **y** channels are not specified, the corresponding position is controlled by the **frameAnchor** option.

The **width** and **height** options default to 16 pixels and can be specified as either a channel or constant. When the width or height is specified as a number, it is interpreted as a constant; otherwise it is interpreted as a channel. Images with a nonpositive width or height are not drawn. If a **width** is specified but not a **height**, or *vice versa*, the one defaults to the other. Images do not support either a fill or a stroke.
The **width** and **height** options default to 16 pixels and can be specified as either a channel or constant. When the width, height, or rotate is specified as a number, it is interpreted as a constant; otherwise it is interpreted as a channel. Images with a nonpositive width or height are not drawn. If a **width** is specified but not a **height**, or *vice versa*, the one defaults to the other. Images do not support either a fill or a stroke.

The following image-specific constant options are also supported:

Expand Down
20 changes: 18 additions & 2 deletions src/marks/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,21 @@ function maybePathChannel(value) {

export class Image extends Mark {
constructor(data, options = {}) {
let {x, y, width, height, src, preserveAspectRatio, crossOrigin, frameAnchor} = options;
let {x, y, width, height, rotate, src, preserveAspectRatio, crossOrigin, frameAnchor} = options;
if (width === undefined && height !== undefined) width = height;
else if (height === undefined && width !== undefined) height = width;
const [vs, cs] = maybePathChannel(src);
const [vw, cw] = maybeNumberChannel(width, 16);
const [vh, ch] = maybeNumberChannel(height, 16);
const [vrotate, crotate] = maybeNumberChannel(rotate, 0);
super(
data,
{
x: {value: x, scale: "x", optional: true},
y: {value: y, scale: "y", optional: true},
width: {value: vw, filter: positive, optional: true},
height: {value: vh, filter: positive, optional: true},
rotate: {value: vrotate, optional: true},
src: {value: vs, optional: true}
},
options,
Expand All @@ -61,13 +63,14 @@ export class Image extends Mark {
this.src = cs;
this.width = cw;
this.height = ch;
this.rotate = crotate;
this.preserveAspectRatio = impliedString(preserveAspectRatio, "xMidYMid");
this.crossOrigin = string(crossOrigin);
this.frameAnchor = maybeFrameAnchor(frameAnchor);
}
render(index, scales, channels, dimensions, context) {
const {x, y} = scales;
const {x: X, y: Y, width: W, height: H, src: S} = channels;
const {x: X, y: Y, width: W, height: H, rotate: R, src: S} = channels;
const [cx, cy] = applyFrameAnchor(this, dimensions);
return create("svg:g", context)
.call(applyIndirectStyles, this, scales, dimensions, context)
Expand Down Expand Up @@ -105,6 +108,19 @@ export class Image extends Mark {
.call(applyAttr, "preserveAspectRatio", this.preserveAspectRatio)
.call(applyAttr, "crossorigin", this.crossOrigin)
.call(applyChannelStyles, this, channels)
.attr("transform", R ? (i) => `rotate(${R[i]})` : this.rotate ? `rotate(${this.rotate})` : null)
.attr(
"transform-origin",
R || this.rotate
? X && Y
? (i) => `${X[i]}px ${Y[i]}px`
: X
? (i) => `${X[i]}px ${cy}px`
: Y
? (i) => `${cx}px ${Y[i]}px`
: `${cx}px ${cy}px`
: null
)
Comment on lines +112 to +123
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunate complexity… am I missing a better way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you set use transform-origin="center" instead?

Copy link
Member

@mbostock mbostock Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it and it doesn’t work. ☹️ So I guess this is the only option… unless you wrap the image in a G element or something, but I bet that’s worse.

)
.node();
}
Expand Down