Skip to content
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

stable clip paths #1624

Merged
merged 5 commits into from
Aug 5, 2024
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
69 changes: 40 additions & 29 deletions src/style.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {geoPath, group, namespaces} from "d3";
import {geoPath, group, namespaces, select} from "d3";
import {create} from "./context.js";
import {defined, nonempty} from "./defined.js";
import {formatDefault} from "./format.js";
Expand Down Expand Up @@ -302,43 +302,25 @@ export function* groupIndex(I, position, mark, channels) {
}
}

// TODO avoid creating a new clip-path each time?
// Note: may mutate selection.node!
function applyClip(selection, mark, dimensions, context) {
let clipUrl;
const {clip = context.clip} = mark;
switch (clip) {
case "frame": {
const {width, height, marginLeft, marginRight, marginTop, marginBottom} = dimensions;
const id = getClipId();
clipUrl = `url(#${id})`;
selection = create("svg:g", context)
.call((g) =>
g
.append("svg:clipPath")
.attr("id", id)
.append("rect")
.attr("x", marginLeft)
.attr("y", marginTop)
.attr("width", width - marginRight - marginLeft)
.attr("height", height - marginTop - marginBottom)
)
.each(function () {
this.appendChild(selection.node());
selection.node = () => this; // Note: mutation!
});
// Wrap the G element with another (untransformed) G element, applying the
// clip to the parent G element so that the clip path is not affected by
// the mark’s transform. To simplify the adoption of this fix, mutate the
// passed-in selection.node to return the parent G element.
selection = create("svg:g", context).each(function () {
this.appendChild(selection.node());
selection.node = () => this; // Note: mutation!
});
clipUrl = getFrameClip(context, dimensions);
break;
}
case "sphere": {
const {projection} = context;
if (!projection) throw new Error(`the "sphere" clip option requires a projection`);
const id = getClipId();
clipUrl = `url(#${id})`;
selection
.append("clipPath")
.attr("id", id)
.append("path")
.attr("d", geoPath(projection)({type: "Sphere"}));
clipUrl = getProjectionClip(context);
break;
}
}
Expand All @@ -351,6 +333,35 @@ function applyClip(selection, mark, dimensions, context) {
applyAttr(selection, "clip-path", clipUrl);
}

function memoizeClip(clip) {
const cache = new WeakMap();
return (context, dimensions) => {
let url = cache.get(context);
if (!url) {
const id = getClipId();
select(context.ownerSVGElement).append("clipPath").attr("id", id).call(clip, context, dimensions);
cache.set(context, (url = `url(#${id})`));
}
return url;
};
}

const getFrameClip = memoizeClip((clipPath, context, dimensions) => {
const {width, height, marginLeft, marginRight, marginTop, marginBottom} = dimensions;
clipPath
.append("rect")
.attr("x", marginLeft)
.attr("y", marginTop)
.attr("width", width - marginRight - marginLeft)
.attr("height", height - marginTop - marginBottom);
});

const getProjectionClip = memoizeClip((clipPath, context) => {
const {projection} = context;
if (!projection) throw new Error(`the "sphere" clip option requires a projection`);
clipPath.append("path").attr("d", geoPath(projection)({type: "Sphere"}));
});

// Note: may mutate selection.node!
export function applyIndirectStyles(selection, mark, dimensions, context) {
applyClip(selection, mark, dimensions, context);
Expand Down
16 changes: 5 additions & 11 deletions test/output/aaplBollingerCandlestick.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 4 additions & 7 deletions test/output/aaplCloseClip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 4 additions & 7 deletions test/output/armadillo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/bandClip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/bandClip2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/contourVapor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/differenceFilterX.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/differenceFilterY1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/differenceFilterY2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/differenceX.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/differenceY.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/differenceY1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 7 additions & 13 deletions test/output/differenceYClip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading