【问题标题】:IOS app webview SVG ClipPath IssueIOS 应用程序 webview SVG ClipPath 问题
【发布时间】: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


【解决方案1】:

我发现了这个问题。这是一个如此令人讨厌的错误。我在我的 css 文件中的某个地方定义:

.area{
   clip-path: url(#clip);
}

在所有浏览器中,这对<rect><path> 都有效,但对于iOS webview,它会呈现上述显示错误。

我在一个单独的 CSS 文件中定义了它,而是为我想要应用它的所有 SVG Object 内联定义它:

  var areaPath = this.focus.append("path")
        ...
        .attr('clip-path', 'url(#clip)') //defining it inline

这个错误让我发疯了,但我很高兴我找到了解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-17
    • 2014-08-18
    • 2014-02-21
    • 2020-08-14
    • 1970-01-01
    • 2019-12-23
    • 2017-11-27
    • 1970-01-01
    相关资源
    最近更新 更多