SVG:如何绘制笔触和矩形角?

是否可以在SVG中将矩形修圆,同时确保笔划遵循拐角的圆度?下面的代码不起作用。

无中风:

stroke-width="0px"

<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
    <rect width="100%" height="100%" stroke="red" stroke-width="0px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>

中风:

stroke-width="10px"

<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
    <rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>

笔触似乎遵循原始的尖角,而不是圆角。

blueshower 回答:SVG:如何绘制笔触和矩形角?

<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
    <rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>

宽字符串超出了画布SVG的svg边界。因此,字符串被部分裁剪。

enter image description here

您必须减小矩形的大小,以使该行可见,并向右x="5"y="5"向下移动矩形的左上角

<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
    <rect x="5" y="5" width="90" height="90" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>

更新

添加了ViewBox。矩形的xy的坐标增加了,SVG包装在容器中,并且可以作为单独的块嵌入到HTML页面中。 自适应应用

.container {
width:30%;
height:30%
}
<div class="container">
<svg  viewBox="0 0 110 110" xmlns="http://www.w3.org/2000/svg">
    <rect x="10" y="10" width="90" height="90" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
</div>

enter image description here

从图片中可以看到,一个宽笔画的正方形完全位于SVG画布内部

,

第一个简单的解决方案是使溢出可见,并增加一些余量来纠正此问题

android studio terminal or command prompt
svg {
  overflow:visible;
  margin:5px; /*half the stroke*/
}

或者您使用<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> <rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" /> </svg> <svg width="150" height="80" xmlns="http://www.w3.org/2000/svg"> <rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" /> </svg> <svg width="100" height="200" xmlns="http://www.w3.org/2000/svg"> <rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" /> </svg>如下:

calc()
svg rect{
  height:calc(100% - 10px);
  width:calc(100% - 10px);
  x:5px;
  y:5px;
}

也可以用作背景:

<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
    <rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>

<svg width="150" height="80" xmlns="http://www.w3.org/2000/svg">
    <rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>

<svg width="100" height="200" xmlns="http://www.w3.org/2000/svg">
    <rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
.box {
    background:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg"  ><rect x="5" y="5" width="100%" height="100%" style="height:calc(100% - 10px);width:calc(100% - 10px)" stroke="red" stroke-width="10" rx="10" ry="10" stroke-linejoin="round" /></svg>');
    color: #fff;
    padding:25px;
    display:inline-block;
    margin: 75px 0;
}

,

<svg width="400" height="180">
    <rect x="50" y="20" rx="20" ry="20" width="150" height="150"
          style="fill:red;stroke:black;stroke-width:5;opacity:0.5" />
</svg>

本文链接:https://www.f2er.com/3076970.html

大家都在问