javascript - 如何开始和结束打圈的路径
问题描述
我想停止并开始圈外的路径。我可以想象有很多方法可以得到我想要的结果:
- 我可以在圆圈中添加白色背景以隐藏路径
- 我可以计算路径退出圆圈的位置并将其用作起点
但是这两种解决方案都有问题(当有另一种背景而不是一种颜色时(1)或在做“懒惰”数学时稍微错误的起点和终点(2))
我也可以想象 SVG 解决方案,但我不知道如何在 google 上搜索它们,因此我问你:
- 以特定像素(圆的半径)提前开始和结束路径
- 使用 SVG 蒙版剪切路径(注意我的圆圈有透明或无填充)
svg
{
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
height: 100%;
width: 100%;
z-index: 100;
}
circle
{
position: absolute;
stroke: black;
fill: transparent;
stroke-width: 2;
cursor: pointer;
}
path
{
fill:none;
stroke-width:2;
stroke:black;
}
<svg>
<circle
r="40"
cx="187.33333333333331"
cy="149"
></circle>
<circle
r="40"
cx="374.66666666666663"
cy="149"
></circle>
<path
d="
M 187.33333333333331 149
Q 281 92.80000000000001 374.66666666666663 149
"
></path>
</svg>
解决方案
您可以使用破折号来隐藏线条的第一部分和最后一部分 - 由于线条是弯曲的,这并不完全精确。
var path = document.getElementById("path")
var l = path.getTotalLength()
var r = 40
path.setAttribute("stroke-dasharray","0,"+r+","+(l-2*r)+","+r)
svg
{
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
height: 100%;
width: 100%;
z-index: 100;
}
circle
{
position: absolute;
stroke: black;
fill: transparent;
stroke-width: 2;
cursor: pointer;
}
path
{
fill:none;
stroke-width:2;
stroke:black;
}
<svg><circle r="40" cx="187.33333333333331" cy="149"></circle><circle r="40" cx="374.66666666666663" cy="149"></circle><path id=path d="M 187.33333333333331 149 Q 281 92.80000000000001 374.66666666666663 149"></path></svg>
推荐阅读
- c# - 将 NewWindow3 添加到 WebBrowser 控件
- visual-studio-2015 - 如何在 Visual Studio 2015 中使用 NuGet 4.x?
- objective-c - 比较 NSInteger 和 NSUInteger
- ember.js - Ember Mirage 没有将模型作为 ember 对象传递
- azure - 将文件(主要是 pdf,但也有一些 jpeg、doc、excel、html ..)从 Unix 迁移/复制到 Azure。
- java - 没有这样的方法错误:
- javascript - 打开之前检查 window.open() 的内容?
- uitableview - 如何对从 tableview 单元格中的 coredata 调用的 2 个属性进行排序(swfit4)
- python - 使用 Django 处理类似许可证的系统
- sql-server - 分区查询返回重复结果