SVG straight path with clip-path not visible in Ch

2019-08-07 06:27发布

I believe that I've found a bug in Chrome's SVG rendering however I'd like to know if there are any workarounds.

With code like this:

<html>
  <body>
    <svg>
      <path clip-path="url(#clip)" d="M 0,100 H 1000 V 100" style="stroke: #000; stroke-width: 2px"></path>
      <defs>
        <clipPath id="clip">
          <rect width="400" height="400"></rect>
        </clipPath>
      </defs>
    </svg>
  </body>
</html>

View in browser

There should be a 1000px long horizontal line (path) clipped by a 400px × 400px rect.

The line is visible and correctly clipped in Firefox and Internet Explorer (10) but it is completely invisible in Chrome. There problem only exists if the path is completely horizontal or vertical as shown in this example.

It is a requirement that the d attribute of the path is an equivelent which D3.js can generate.

2条回答
趁早两清
2楼-- · 2019-08-07 06:57

This is indeed a Chrome bug. If you reverse the order of <defs> and the <path> element, then it works. This is basically the same issue as with the <use> element.

查看更多
贪生不怕死
3楼-- · 2019-08-07 07:24

A workaround which I've made work adds an extra moveto command to the beginning of the d attribute:

<path clip-path="url(#clip)" d="M -10,-10 M 0,100 H 1000 V 100" style="stroke: #000; stroke-width: 2px"></path>

View in browser

The extra moveto has no visible effect but means that the path is not technically straight. It is visible and clipped correctly in Chrome.

查看更多
登录 后发表回答