如果我们知道角度,计算 SVG 线性渐变属性 x1 y1 x2 y2?
正如我们所知,在 SVG 中,角度线性渐变是通过设置属性 x1,x2,y1,y2 来实现的。但是,如果我们只得到角度,
1.如何计算x1,y1,x2,y2的结果?
2.这个公式 tan (角度) = (y2-y1)/(x2-x1) 是否正确?我怎样才能计算所有参数。
As we know in SVG the angular linear gradient is via setting the attribute x1,x2,y1,y2. However, If we only get the angle,
1.how to calculate the result of x1,y1,x2,y2?
2.is it correct for this formula tan (angle ) = (y2-y1)/(x2-x1)? how can I calculate the all the parameters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下内容应该可以满足您的需求,或者接近您的需求。要点是它在您的旋转区域内创建一个起点和终点,结果将是一组您可以使用的单位向量(即在 0.0 和 1.0 之间)。其中 inputAngle 是您希望渐变的角度。
不确定线性渐变值如何用于 SVG,但您可能需要乘以元素大小......
The following should get you what you need, or close to it. The gist is that it creates a start and end point within your area of rotation, the result will be a set of unit vectors that you can use (i.e. between 0.0 and 1.0). Where inputAngle is the angle you want your gradient to be.
Not sure how the linear gradient values are used for SVG, but you may need to multiply by your elements size...
基于 JT- 的答案,这里有一个函数可以完全满足您在 Javascript 中的需要。只需调用此函数,将元素和度数作为整数传递即可。我还添加了“左”、“右”、“上”、“下”作为可选参数。
building off of JT-'s answer, here is a function that will do exactly what you need in Javascript. Just call this function passing the element and degrees as an integer. I've also added "left","right","up","down" as optional arguments.
将
x_i
、y_i
设置为0度,并通过gradientTransform属性应用旋转(gradientTransform="rotate(angle)"
),Set
x_i
,y_i
as if the angle were 0 deg and apply a rotation by means of the gradientTransform attribute (gradientTransform="rotate(angle)"
),