I’m new to JavaScript and D3. Looking for some direction on clipping paths.
The clipping in my masked scatter plot:
looks to work on Observable if viewed in browser (Chrome, Firefox). But when I view the same link on my iPhone the clipping mask fails and the scatter plots spill over the axes.
I used the ES module and embedded the plot in my website following Jeremy Ashkenas post and code. The clipping seems to work as intended irrespective of if I view from PC or on iPhone?.
I’m guessing I’m missing something fundamental in how I’ve coded the clip path?
Do I need to get my head around DOM.uid and defs? Or is the fix simpler?
I looked at:
https://beta.observablehq.com/@mbostock/svg-clipping-test. But not really getting it yet?
So while I don’t really understand my own question - is the IOS treating the local IRI of my Observable notebook post as local-to-the-SVG regardless of a base URL, but the ES module isn’t?
I used D3 margin-convention to define the chart-area (so is a g node within the SVG):
chartArea = svg.append("g").attr("transform","translate(" + margin.left + ", " + margin.top + ")")
I then appended a clip-path to the chart-area and grouped all the plot circles within the clip-path:
allCircles = chartArea.append("g")
.attr("id", "circles")
.attr("clip-path", "url(#chart-area)") // the line where I think I get structure wrong?
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr("r", 2)
.attr("fill", "black")
The subsequent transform is applied to each plot circle (‘this’ in the code below) grouped in the clip path.
d3.select(this)
.transition()
.delay(i * 25)
.duration(2000)
.ease(d3.easeElasticOut)
.style("fill", colors[colorIndex])
.style("stroke", colors[colorIndex2])
.attr("stroke-width", "2.0px")
.attr("r", 15);