【发布时间】:2016-12-12 16:11:35
【问题描述】:
我正在使用d3 v4 来渲染 SVG 图形。我在一些<path> 元素上使用了clipPath。我在 rect 元素上有平移行为,并且 clipPath 有助于隐藏一些路径元素。在android中平移时。 clipPath 可以根据需要工作,但在 iOS 中平移时,绘图呈现时髦。见下文:
之前
我已经使用以下代码实现了 SVG 剪辑:
this.line = d3.line()
.curve(d3.curveMonotoneX)
.x((d) => this.xScale(this.getDate(d)))
.y((d) => this.yScale(d.kWh));
this.area = d3.area()
.curve(d3.curveMonotoneX)
.x((d) => {
return this.xScale(this.getDate(d))
})
.y0(this.height)
.y1((d) => this.yScale(d.kWh));
// Definition for clipPath
this.svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", this.width)
.attr('transform', 'translate(0,-20)')
.attr("height", this.height + 20);
// clipPath added to area
var areaPath = this.focus.append("path")
.datum(this.data)
.attr("class", "area")
.attr('height', this.height)
.attr('fill-opacity', .2)
.attr('clip-path', 'url(#clip)')
.attr("d", this.area)
.attr("transform", "translate(0," + 80 + ")")
.style("fill", "url(#gradient)");
// clipPath added to the line
var linePath = this.focus.append('path')
.datum(this.data)
.attr('class', 'line')
.attr('fill', 'none')
.attr('clip-path', 'url(#clip)')
.attr('stroke', '#31B5BB')
.attr('stroke-width', '2px')
.attr("transform", "translate(0," + 80 + ")")
.attr('d', this.line);
他是缩放时调用的缩放的摘录。
private zoomed = () => {
if (this.isMinZooming) return;
let diff,
domain,
minBuffer,
maxBuffer,
t;
t = d3.event.transform;
// loose mobile events
if (isNaN(t.k)) return;
this.xScale.domain(t.rescaleX(this.x2Scale).domain());
diff = this.daydiff(this.xScale.domain()[0], this.xScale.domain()[1]);
// Redraw Axis
this.xAxis = d3.axisBottom(this.xScale).tickSize(0).tickFormat(d3.timeFormat('%b'));
this.focus.select(".axis--x").call(this.xAxis);
// Redraw Paths. This is where the redraw function gets messy in the iOS webview.
this.focus.select(".area").attr("d", this.area);
this.focus.select('.line').attr('d', this.line);
...
}
有没有人在使用 clipPath 时遇到过同样的问题?
【问题讨论】:
-
我会说这是因为clipPath和你正在剪辑的元素没有在同一个坐标系中定义,也不受相同的变换stackoverflow.com/a/38088473/1160916
-
@Ashitaka 为什么这适用于除 IOS 之外的所有浏览器?它甚至可以在 Safari 中使用。你知道为什么吗?
标签: javascript ios d3.js svg