正如我们在SVG中所知,角度线性渐变是通过设置属性x1,x2,y1,y2。但是,如果我们只获得角度,
1.如何计算x1,y1,x2,y2的结果?
2.这个公式tan(角度)=(y2-y1)/(x2-x1)是否正确?我该如何计算所有参数。
答案 0 :(得分:6)
建立JT的答案,这是一个能完全满足您在Javascript中所需要的功能。只需调用此函数,将元素和度数作为整数传递。我还添加了“left”,“right”,“up”,“down”作为可选参数。
function svg_linear_gradient_direction(element, direction){
if(direction === "left"){
element.setAttributeNS(null, "x1", "100%");
element.setAttributeNS(null, "y1", "0%");
element.setAttributeNS(null, "x2", "0%");
element.setAttributeNS(null, "y2", "0%");
} else if(direction === "right"){
element.setAttributeNS(null, "x1", "0%");
element.setAttributeNS(null, "y1", "0%");
element.setAttributeNS(null, "x2", "100%");
element.setAttributeNS(null, "y2", "0%");
} else if(direction === "down"){
element.setAttributeNS(null, "x1", "0%");
element.setAttributeNS(null, "y1", "0%");
element.setAttributeNS(null, "x2", "0%");
element.setAttributeNS(null, "y2", "100%");
} else if(direction === "up"){
element.setAttributeNS(null, "x1", "0%");
element.setAttributeNS(null, "y1", "100%");
element.setAttributeNS(null, "x2", "0%");
element.setAttributeNS(null, "y2", "0%");
} else if(typeof direction === "number"){
var pointOfAngle = function(a) {
return {
x:Math.cos(a),
y:Math.sin(a)
};
}
var degreesToRadians = function(d) {
return ((d * Math.PI) / 180);
}
var eps = Math.pow(2, -52);
var angle = (direction % 360);
var startPoint = pointOfAngle(degreesToRadians(180 - angle));
var endPoint = pointOfAngle(degreesToRadians(360 - angle));
if(startPoint.x <= 0 || Math.abs(startPoint.x) <= eps)
startPoint.x = 0;
if(startPoint.y <= 0 || Math.abs(startPoint.y) <= eps)
startPoint.y = 0;
if(endPoint.x <= 0 || Math.abs(endPoint.x) <= eps)
endPoint.x = 0;
if(endPoint.y <= 0 || Math.abs(endPoint.y) <= eps)
endPoint.y = 0;
element.setAttributeNS(null, "x1", startPoint.x);
element.setAttributeNS(null, "y1", startPoint.y);
element.setAttributeNS(null, "x2", endPoint.x);
element.setAttributeNS(null, "y2", endPoint.y);
}
}
答案 1 :(得分:5)
以下内容可为您提供所需或接近的内容。要点是它在您的旋转区域内创建一个起点和终点,结果将是一组您可以使用的单位矢量(即介于0.0和1.0之间)。其中inputAngle是您希望渐变的角度。
function pointOfAngle(a) {
return {x:Math.cos(a),
y:Math.sin(a)};
}
function degreesToRadians(d) {
return ((d * Math.PI) / 180);
}
var eps = Math.pow(2, -52);
var inputAngle = 45;
var angle = (inputAngle % 360);
var startPoint = pointOfAngle(degreesToRadians(180 - angle));
var endPoint = pointOfAngle(degreesToRadians(360 - angle));
// if you want negative values you can remove the following checks
// but most likely it will produce undesired results
if(startPoint.x <= 0 || Math.abs(startPoint.x) <= eps)
startPoint.x = 0;
if(startPoint.y <= 0 || Math.abs(startPoint.y) <= eps)
startPoint.y = 0;
if(endPoint.x <= 0 || Math.abs(endPoint.x) <= eps)
endPoint.x = 0;
if(endPoint.y <= 0 || Math.abs(endPoint.y) <= eps)
endPoint.y = 0;
不确定线性渐变值如何用于SVG,但您可能需要乘以元素大小......
x1 = startPoint.x * width
y1 = startPoint.y * height
x2 = endPoint.x * width
y2 = endPoint.y * height
答案 2 :(得分:1)
设置x_i
,y_i
,好像角度为0度,并通过gradientTransform属性(gradientTransform="rotate(angle)"
)应用旋转,