Skip to content

imageFilter (support for the CSS filter attribute) #409

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

Merged
merged 3 commits into from
May 8, 2023
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
1 change: 1 addition & 0 deletions docs/features/marks.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ All marks support the following style options:
* **strokeDashoffset** - the [stroke dash offset](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dashoffset) (typically in pixels)
* **opacity** - object opacity (a number between 0 and 1)
* **mixBlendMode** - the [blend mode](https://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode) (*e.g.*, *multiply*)
* **imageFilter** - a CSS [filter](https://developer.mozilla.org/en-US/docs/Web/CSS/filter) (*e.g.*, *blur(5px)*)
* **shapeRendering** - the [shape-rendering mode](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/shape-rendering) (*e.g.*, *crispEdges*)
* **paintOrder** - the [paint order](https://developer.mozilla.org/en-US/docs/Web/CSS/paint-order) (*e.g.*, *stroke*)
* **dx** - horizontal offset (in pixels; defaults to 0)
Expand Down
8 changes: 8 additions & 0 deletions src/mark.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,14 @@ export interface MarkOptions {
*/
mixBlendMode?: string;

/**
* A CSS [filter][1]; a constant string used to adjust the rendering of
* images, such as *blur(5px)*.
*
* [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/filter
*/
imageFilter?: string;

/**
* The [paint-order][1]; a constant string specifying the order in which the
* **fill**, **stroke**, and any markers are drawn; defaults to *normal*,
Expand Down
3 changes: 3 additions & 0 deletions src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function styles(
strokeDashoffset,
opacity,
mixBlendMode,
imageFilter,
paintOrder,
pointerEvents,
shapeRendering
Expand Down Expand Up @@ -135,6 +136,7 @@ export function styles(
mark.ariaHidden = string(ariaHidden);
mark.opacity = impliedNumber(copacity, 1);
mark.mixBlendMode = impliedString(mixBlendMode, "normal");
mark.imageFilter = impliedString(imageFilter, "none");
mark.paintOrder = impliedString(paintOrder, "normal");
mark.pointerEvents = impliedString(pointerEvents, "auto");
mark.shapeRendering = impliedString(shapeRendering, "auto");
Expand Down Expand Up @@ -368,6 +370,7 @@ export function applyIndirectStyles(selection, mark, dimensions, context) {
applyAttr(selection, "stroke-dasharray", mark.strokeDasharray);
applyAttr(selection, "stroke-dashoffset", mark.strokeDashoffset);
applyAttr(selection, "shape-rendering", mark.shapeRendering);
applyAttr(selection, "filter", mark.imageFilter);
applyAttr(selection, "paint-order", mark.paintOrder);
applyAttr(selection, "pointer-events", mark.pointerEvents);
}
Expand Down
60 changes: 60 additions & 0 deletions test/output/penguinSpeciesImageFilter.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions test/plots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ export * from "./penguin-quantile-unknown.js";
export * from "./penguin-sex-mass-culmen-species.js";
export * from "./penguin-sex.js";
export * from "./penguin-size-symbols.js";
export * from "./penguin-species-cheysson.js";
export * from "./penguin-species-gradient.js";
export * from "./penguin-species-group.js";
export * from "./penguin-species.js";
export * from "./penguin-species-island-relative.js";
export * from "./penguin-species-island-sex.js";
export * from "./penguin-species-island.js";
Expand Down
20 changes: 0 additions & 20 deletions test/plots/penguin-species-gradient.ts

This file was deleted.

13 changes: 0 additions & 13 deletions test/plots/penguin-species-group.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,51 @@ export async function penguinSpeciesCheysson() {
]
});
}

export async function penguinSpeciesGradient() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
return Plot.plot({
marks: [
() => svg`<defs>
<linearGradient id="gradient" gradientTransform="rotate(90)">
<stop offset="5%" stop-color="purple" />
<stop offset="75%" stop-color="red" />
<stop offset="100%" stop-color="gold" />
</linearGradient>
</defs>`,
Plot.barY(penguins, Plot.groupX({y: "count"}, {x: "species", fill: "url(#gradient)"})),
Plot.ruleY([0])
]
});
}

export async function penguinSpeciesGroup() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
return Plot.plot({
marks: [
Plot.barX(penguins, Plot.stackX(Plot.groupZ({x: "proportion"}, {fill: "species"}))),
Plot.text(penguins, Plot.stackX(Plot.groupZ({x: "proportion", text: "first"}, {z: "species", text: "species"}))),
Plot.ruleX([0, 1])
]
});
}

export async function penguinSpeciesImageFilter() {
const penguins = await d3.csv<any>("data/penguins.csv", d3.autoType);
return Plot.plot({
marks: [
Plot.barY(
penguins,
Plot.groupX(
{y: "count"},
{
x: "species",
fill: "species",
imageFilter: "drop-shadow(3px 3px red) sepia(40%) drop-shadow(-3px -3px currentColor)"
}
)
),
Plot.ruleY([0])
]
});
}