生成带有虚线填充线的 SVG 矩形

cdu*_*dub 2 css jquery svg dimple.js

我有一个用dimpleJS 制作的矩形,需要用虚线而不是纯色填充框。这可以使用 svg、css 和/或 jquery 吗?矩形如下。

<rect id="dimple-not-working-on-an-associate-s-degree-2008-not-working-on-an-associate-s-degree---" class="dimple-series-0 dimple-bar dimple-not-working-on-an-associate-s-degree dimple-2008 dimple-not-working-on-an-associate-s-degree dimple-custom-series-bar dimple-custom-format-1" x="183.5" y="128.4" width="18" height="141.6" opacity="0.8" style="fill: rgb(92, 186, 230); stroke: rgb(77, 156, 192);" fill="#5cbae6" stroke="rgb(77, 156, 192)"></rect>
Run Code Online (Sandbox Code Playgroud)

Alv*_*oro 5

正如罗伯特在评论中提到的,您需要做的是使用 apattern来填充矩形。步骤很简单:

  • 在 SVG 中定义一个模式
  • 在图案内,“绘制”任何你想成为图案的东西。例如,你想要虚线,所以你会做一条只占图案一部分的线,所以当它重复时它会看起来是虚线。
  • 将属性设置patternUnits为“userSpaceOnUse”,这样模式将占据应用它的整个元素(就像背景重复一样)。
  • fill 通过引用其 id(例如fill="url(#pattern-id)")在属性中应用模式

在这里你可以看到一个演示工作:

<svg width="100" height="100" viewBox="0 0 100 100">
  <defs>
    <pattern id="lines" height="10" width="10" patternUnits="userSpaceOnUse">
      <line x1="0" y1="4" x2="5" y2="4" stroke-width="2" stroke="black"/>
    </pattern>
  </defs>
  <rect x="10" y="10" width="80" height="80" fill="url(#lines)" />
</svg>
Run Code Online (Sandbox Code Playgroud)