javascript - 如何在 SVG 中绘制清晰的 1px 线条(使用 js)?
问题描述
我想在 SVG 中绘制 1px 的线,水平、垂直或对角线,但由于某种原因,如果我将笔触宽度设置为 1 并将笔触颜色设置为黑色,它会显示为 2px 灰线。有人建议使用 shape-rendering="crispEdges",但我认为没有效果。有人可以帮忙吗?下面是一个三行的jsfiddle:
https://jsfiddle.net/3vr51tkj/
<svg shape-rendering="crispEdges" style="position: absolute; left: calc(26vw + 3px); top: 10; width: calc(2.1vw + 1px); height: 12cm;">
<line xmlns="http://www.w3.org/2000/svg" x1="0" y1="120.94488341347886" x2="36" y2="120.94488341347886" stroke="black" shape-rendering="crispEdges"></line>
<line xmlns="http://www.w3.org/2000/svg" x1="0" y1="166.29921469353343" x2="36" y2="11.645684467356698" stroke="black" shape-rendering="crispEdges"></line>
<line xmlns="http://www.w3.org/2000/svg" x1="0" y1="347.7165398137517" x2="36" y2="347.7165398137517" stroke="red" stroke-width="0.5" shape-rendering="crispEdges"></line>
</svg>
如您所见,描边宽度为 0.5 的红线实际上具有所需的 1px 宽度,但颜色变淡了。
解决方案
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<line x1="0" y1="0" x2="0" y2="100" style="stroke:#540CA0;" stroke-width="1px">
</svg>
试试这个.. 您可以更改坐标以使其垂直水平和对角线。
推荐阅读
- java - 代码覆盖 Kotlin 和 Java 一起使用 Maven - Jacoco
- r - 使用接受-拒绝方法模拟随机变量
- firebase - 如何修复“超出最大调用堆栈大小”错误
- regex - RegEX 用于查找从右侧开始的随机小写字母数字字符串
- android - 为什么我在 ProgressUpdate 上收到 NetworkOnMainThread 异常错误
- python - 如何在每个步骤中从 for 循环中获取结果?
- google-cloud-platform - 有没有办法在会话结束后清除对话历史记录
- android - 如何使用 React Native 构建 64 位和 32 位 APK
- gzip - 从 Flink BucketingSink 生成的有效长度文件中恢复压缩文件
- java - 如何使用 Maven 来利用现有 Spring Boot 应用程序的属性文件?