javascript - 如何在不改变圆内半径的情况下改变笔画宽度

标签 javascript css web svg

<分区>

我需要动态改变 stroke-width 并且我需要保持圆的内半径不变。

<g fill="none" :stroke-width="brightness * 60" ...

我怎样才能达到那种效果?

我使用的 svg 如下。我想创建彩虹圈,并且必须根据所选的亮度更改圈的宽度。

宽度变化正确,但我想保持内半径不变。

<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400" viewBox="-35 -35 270 270">
    <defs>
        <linearGradient id="redyel" gradientUnits="objectBoundingBox" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stop-color="#ff0000" :stop-opacity="saturation"/>
            <stop offset="100%" stop-color="#ffff00" :stop-opacity="saturation"/>
        </linearGradient>
        <linearGradient id="yelgre" gradientUnits="objectBoundingBox" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stop-color="#ffff00" :stop-opacity="saturation"/>
            <stop offset="100%" stop-color="#00ff00" :stop-opacity="saturation"/>
        </linearGradient>
        <linearGradient id="grecya" gradientUnits="objectBoundingBox" x1="1" y1="0" x2="0" y2="1">
            <stop offset="0%" stop-color="#00ff00" :stop-opacity="saturation"/>
            <stop offset="100%" stop-color="#00ffff" :stop-opacity="saturation"/>
        </linearGradient>
        <linearGradient id="cyablu" gradientUnits="objectBoundingBox" x1="1" y1="1" x2="0" y2="0">
            <stop offset="0%" stop-color="#00ffff" :stop-opacity="saturation"/>
            <stop offset="100%" stop-color="#0000ff" :stop-opacity="saturation"/>
        </linearGradient>
        <linearGradient id="blumag" gradientUnits="objectBoundingBox" x1="0" y1="1" x2="0" y2="0">
            <stop offset="0%" stop-color="#0000ff" :stop-opacity="saturation"/>
            <stop offset="100%" stop-color="#ff00ff" :stop-opacity="saturation"/>
        </linearGradient>
        <linearGradient id="magred" gradientUnits="objectBoundingBox" x1="0" y1="1" x2="1" y2="0">
            <stop offset="0%" stop-color="#ff00ff" :stop-opacity="saturation"/>
            <stop offset="100%" stop-color="#ff0000" :stop-opacity="saturation"/>
        </linearGradient>
     </defs>

     <g fill="none" :stroke-width="brightness * 60" transform="translate(100,100)">
         <path d="M 0,-100 A 100,100 0 0,1 86.6,-50" stroke="url(#redyel)"/>
         <path d="M 86.6,-50 A 100,100 0 0,1 86.6,50" stroke="url(#yelgre)"/>
         <path d="M 86.6,50 A 100,100 0 0,1 0,100" stroke="url(#grecya)"/>
         <path d="M 0,100 A 100,100 0 0,1 -86.6,50" stroke="url(#cyablu)"/>
         <path d="M -86.6,50 A 100,100 0 0,1 -86.6,-50" stroke="url(#blumag)"/>
         <path d="M -86.6,-50 A 100,100 0 0,1 0,-100" stroke="url(#magred)"/>
     </g>
     <circle cx="100" cy="100" r="65" :fill="hsla"/>
 </svg>

最佳答案

圆的笔划的内半径始终小于半径的笔划宽度的一半,因此要将内半径保持在同一位置,您必须将圆的半径增加其笔划的一半,例如:https://jsfiddle.net/f3wctoyL/1/

<svg width="400" height="400">
    <circle id="a" cx="200" cy="200" r="100" />
    <circle id="b" cx="200" cy="200" r="110" />
    <circle id="c" cx="200" cy="200" r="105" />
</svg>

圆 b 和 c 上的笔划的内半径为 100。

关于javascript - 如何在不改变圆内半径的情况下改变笔画宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55783336/

上一篇:html - 将内联样式转换为包含属性的 <font> 标签

下一篇:css - 图像的媒体查询以在桌面和移动设备上工作

相关文章:

javascript函数 "document.getElementById()"按预期工作

javascript - Lodash 拒绝点免费

javascript - 如何在正则表达式中使用特定网站

javascript - Web Audio API 附加/连接不同的 AudioBuffers 并将它们作为一首歌曲播放

html - 如何让我的导航栏加载到我的元素动画之上

IE9+ 中带填充的 jQuery UI .addClass() 动画

css - 图片右上角的按钮

php - 如何回显带单引号且不带反斜杠的字符串

python - Go Web 服务器请求产生自己的 goroutine?

javascript - 避免多次加载 javascript 文件