我在一个圆形中使用了简单的 linearGradient
。
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 300">
<linearGradient id="ct-svg-gradient" gradientTransform="rotate(65)">
<stop offset="0%" stop-color="red" />
<stop offset="100%" stop-color="yellow" />
</linearGradient>
<circle cx="100" cy="100" r="50" fill="url(#ct-svg-gradient)"/>
</svg>
如您所见,我正在尝试将渐变旋转65度。至少,这是文档所声称的。我尝试将此
rotate
设置为更大的数字,但结果并非预期。我了解到这个问题的原因在于
linearGradient
没有将其transform
原点设置为中心。我想,我必须使用linearGradient
的x1、y1、x2、y2属性,而不使用任何gradientTransform
。如果是这样,那么如果我只有应该将渐变旋转到的角度,最简单的获取这四个值的方法是什么?我有兴趣实现这样一个函数,我的输入是动态的。
function convert(angle_in_degrees) { // or radians, I'll take care of conversion
// the algorithm I'm interested in
return {
x1: ...,
y1: ...,
x2: ...,
y2: ...,
};
}
我非常感谢任何反馈和建议。
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 300">
<linearGradient id="ct-svg-gradient" gradientTransform="rotate(150)">
<stop offset="0%" stop-color="red" />
<stop offset="100%" stop-color="yellow" />
</linearGradient>
<circle cx="100" cy="100" r="50" fill="url(#ct-svg-gradient)"/>
</svg>