如何修改SVG图形的坡度,使其从中间位置起始?
如何让SVG图形的渐变从中间位置起始
要实现从图形中心起始的渐变效果,你需要在SVG的<defs>中定义径向渐变,并将填充属性指向该渐变。以下是修改后的完整代码:
<svg xmlns="http://www.w3.org/2000/svg" fill="none"> <g filter="url(#filter0_b_1_2556)"> <path d="M176.291 1H1V115C1 120.523 5.47716 125 11 125H331C336.523 125 341 120.523 341 115V37.4083H213.78C210.647 37.4083 207.695 35.9399 205.805 33.4411L184.266 4.96721C182.376 2.46844 179.424 1 176.291 1Z" fill="url(#centerGradient)" /> <path d="M176.291 1H1V115C1 120.523 5.47716 125 11 125H331C336.523 125 341 120.523 341 115V37.4083H213.78C210.647 37.4083 207.695 35.9399 205.805 33.4411L184.266 4.96721C182.376 2.46844 179.424 1 176.291 1Z" stroke="white" strokeOpacity="0.9" /> </g> <defs> <!-- 新增从中心起始的径向渐变 --> <radialGradient id="centerGradient" cx="171" cy="63" r="170" gradientUnits="userSpaceOnUse"> <stop offset="0%" stop-color="#5B85A4" stop-opacity="0.8"/> <stop offset="100%" stop-color="#5B85A4" stop-opacity="0.4"/> </radialGradient> <filter id="filter0_b_1_2556" x="-53.5" y="-53.5" width="449" height="233" filterUnits="userSpaceOnUse" colorInterpolationFilters="sRGB" > <feFlood floodOpacity="0" result="BackgroundImageFix" /> <feGaussianBlur in="BackgroundImageFix" stdDeviation="27" /> <feComposite in2="SourceAlpha" operator="in" result="effect1_backgroundBlur_1_2556" /> <feBlend mode="normal" in="SourceGraphic" in2="effect1_backgroundBlur_1_2556" result="shape" /> </filter> </defs> </svg>
关键修改说明:
- 在
<defs>中添加了<radialGradient>元素,cx="171"和cy="63"对应你图形的中心坐标(原图形x范围1-341,y范围1-125,取中间值) r="170"设置渐变半径,确保覆盖整个图形- 通过
<stop>节点定义渐变过渡:中心(offset=0%)使用原颜色和透明度,边缘(offset=100%)降低透明度(或替换为其他颜色),实现从中心向外的渐变效果 - 将原路径的
fill="#5B85A4"替换为fill="url(#centerGradient)",让路径使用定义好的渐变
如果需要线性渐变(比如从中心向左右/上下双向过渡),可以改用<linearGradient>,设置x1="0.5"、y1="0"、x2="0.5"、y2="1"(上下方向)或x1="0"、y1="0.5"、x2="1"、y2="0.5"(左右方向),并调整stop节点的颜色即可。
内容的提问来源于stack exchange,提问作者Rural Smack
相关产品推荐
相关产品推荐

