Skip to content

Commit 28b1a4c

Browse files
Filmbostock
andauthored
Plot.image supports imageRendering (#1267)
Co-authored-by: Mike Bostock <[email protected]>
1 parent 516cbe3 commit 28b1a4c

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,8 +1636,9 @@ The following image-specific constant options are also supported:
16361636
* **frameAnchor** - the [frame anchor](#frameanchor); defaults to *middle*
16371637
* **preserveAspectRatio** - the [aspect ratio](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio); defaults to “xMidYMid meet”
16381638
* **crossOrigin** - the [cross-origin](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/crossorigin) behavior
1639+
* **imageRendering** - the [image-rendering attribute](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/image-rendering); defaults to *auto* (bilinear)
16391640
1640-
To crop the image instead of scaling it to fit, set **preserveAspectRatio** to “xMidYMid slice”.
1641+
To crop the image instead of scaling it to fit, set **preserveAspectRatio** to “xMidYMid slice”. The **imageRendering** option may be set to *pixelated* to disable bilinear interpolation on enlarged images; however, note that this is not supported in WebKit.
16411642
16421643
Images are drawn in input order, with the last data drawn on top. If sorting is needed, say to mitigate overplotting, consider a [sort and reverse transform](#transforms).
16431644
@@ -1834,7 +1835,7 @@ The following raster-specific constant options are supported:
18341835
* **imageRendering** - the [image-rendering attribute](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/image-rendering); defaults to *auto* (bilinear)
18351836
* **blur** - a non-negative pixel radius for smoothing; defaults to 0
18361837
1837-
The **imageRendering** option may be sent to *pixelated* to disable bilinear interpolation for a sharper image; however, note that this is not supported in WebKit. The **interpolate** option is ignored when **fill** or **fillOpacity** is a function of *x* and *y*.
1838+
The **imageRendering** option may be set to *pixelated* to disable bilinear interpolation for a sharper image; however, note that this is not supported in WebKit. The **interpolate** option is ignored when **fill** or **fillOpacity** is a function of *x* and *y*.
18381839
18391840
#### Plot.raster(*data*, *options*)
18401841

src/marks/image.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function maybePathChannel(value) {
4040

4141
export class Image extends Mark {
4242
constructor(data, options = {}) {
43-
let {x, y, width, height, src, preserveAspectRatio, crossOrigin, frameAnchor} = options;
43+
let {x, y, width, height, src, preserveAspectRatio, crossOrigin, frameAnchor, imageRendering} = options;
4444
if (width === undefined && height !== undefined) width = height;
4545
else if (height === undefined && width !== undefined) height = width;
4646
const [vs, cs] = maybePathChannel(src);
@@ -64,6 +64,7 @@ export class Image extends Mark {
6464
this.preserveAspectRatio = impliedString(preserveAspectRatio, "xMidYMid");
6565
this.crossOrigin = string(crossOrigin);
6666
this.frameAnchor = maybeFrameAnchor(frameAnchor);
67+
this.imageRendering = impliedString(imageRendering, "auto");
6768
}
6869
render(index, scales, channels, dimensions, context) {
6970
const {x, y} = scales;
@@ -104,6 +105,7 @@ export class Image extends Mark {
104105
.call(applyAttr, "href", S ? (i) => S[i] : this.src)
105106
.call(applyAttr, "preserveAspectRatio", this.preserveAspectRatio)
106107
.call(applyAttr, "crossorigin", this.crossOrigin)
108+
.call(applyAttr, "image-rendering", this.imageRendering)
107109
.call(applyChannelStyles, this, channels)
108110
)
109111
.node();

test/output/imagePixelated.svg

Lines changed: 19 additions & 0 deletions
Loading

test/plots/image-rendering.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as Plot from "@observablehq/plot";
2+
3+
export async function imagePixelated() {
4+
return Plot.image([0], {
5+
frameAnchor: "middle",
6+
src: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAYAAACddGYaAAAAAXNSR0IArs4c6QAAACNJREFUGFdjVBaX/s/TdotBLrqfgfHeXaP/Ek3PGGatfcEAAHifCmZc3SIiAAAAAElFTkSuQmCC",
7+
width: 300,
8+
height: 200,
9+
imageRendering: "pixelated"
10+
}).plot({width: 300, height: 200});
11+
}

test/plots/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ export * from "./federal-funds.js";
289289
export * from "./frame.js";
290290
export * from "./function-contour.js";
291291
export * from "./heatmap.js";
292+
export * from "./image-rendering.js";
292293
export * from "./legend-color.js";
293294
export * from "./legend-opacity.js";
294295
export * from "./legend-symbol.js";

0 commit comments

Comments
 (0)