Skip to content

Commit 8347fea

Browse files
committed
leaner (one filter is shared by all lines)
1 parent 55e577a commit 8347fea

6 files changed

Lines changed: 295 additions & 227 deletions

File tree

src/marks/halo.js

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,22 @@ function getHaloId() {
1010
}
1111

1212
export function applyHalo(selection, {halo}) {
13-
if (!halo) return;
13+
if (!halo) return null;
1414
const {color, radius} = halo;
15-
const filters = new WeakMap();
16-
selection.attr("filter", function () {
17-
const id = getHaloId();
18-
filters.set(this, id);
19-
return `url(#${id})`;
20-
});
21-
selection
22-
.append("filter")
23-
.attr("id", function () {
24-
return filters.get(this.parentNode);
25-
})
26-
.call((filter) =>
27-
filter
28-
.append("feMorphology")
29-
.attr("in", "SourceAlpha")
30-
.attr("result", "dilated")
31-
.attr("operator", "dilate")
32-
.attr("radius", radius)
33-
)
34-
.call((filter) => filter.append("feFlood").style("flood-color", color))
35-
.call((filter) => filter.append("feComposite").attr("in2", "dilated").attr("operator", "in"))
36-
.append("feMerge")
37-
.call((merge) => {
38-
merge.append("feMergeNode");
39-
merge.append("feMergeNode").attr("in", "SourceGraphic");
40-
});
15+
const id = getHaloId();
16+
const filter = selection.append("filter").attr("id", id);
17+
filter
18+
.append("feMorphology")
19+
.attr("in", "SourceAlpha")
20+
.attr("result", "dilated")
21+
.attr("operator", "dilate")
22+
.attr("radius", radius);
23+
filter.append("feFlood").style("flood-color", color);
24+
filter.append("feComposite").attr("in2", "dilated").attr("operator", "in");
25+
const merge = filter.append("feMerge");
26+
merge.append("feMergeNode");
27+
merge.append("feMergeNode").attr("in", "SourceGraphic");
28+
return `url(#${id})`;
4129
}
4230

4331
export function maybeHalo(halo, color, radius) {

src/marks/line.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class Line extends Mark {
6060

6161
// When adding a halo to multiple series, nest by series so each
6262
// gets its own halo filter; otherwise render paths directly into g.
63+
const filter = applyHalo(g, this);
6364
const segments = groupIndex(index, [X, Y], this, channels);
6465
(this.halo && Z
6566
? g
@@ -69,7 +70,7 @@ export class Line extends Mark {
6970
.append("g")
7071
: g.datum([, segments])
7172
)
72-
.call(applyHalo, this)
73+
.attr("filter", filter)
7374
.selectAll()
7475
.data(([, d]) => d)
7576
.enter()

test/output/lineHalo.svg

Lines changed: 13 additions & 49 deletions
Loading

test/output/lineHaloSingle.svg

Lines changed: 214 additions & 0 deletions
Loading

test/output/lineHaloStyles.svg

Lines changed: 41 additions & 149 deletions
Loading

0 commit comments

Comments
 (0)