我正在通过 Raphael 框架尝试 svg 功能(它的路径文档在这里)。我发现编写路径字符串非常乏味,因为在创建新路径或查看已编写的路径时,我必须不断查找格式规范。是否有简化此过程的框架?例如,不必编写如下内容:
function pathStringForBoxWithQuad(x1, y1, x2, y2) {
var edgeLength = 8;
var str = "M " + (x2 + edgeLength) + " " + y1; // move to top right
str += " L " + x1 + " " + y1; // line to top left
str += " L " + x1 + " " + y2; // line to bottom left
str += " L " + (x2 + edgeLength) + " " + y2; // line to bottom right
str += " Q " + x2 + " " + (y1 + (y2 - y1) / 2) + " " + (x2 + edgeLength) + " " + y1; // quadratic back to top right
str += " Z";
return str;
}
您可以编写如下内容,但它会返回相同的字符串:
function pathStringForBoxWithQuad(x1, y1, x2, y2) {
var edgeLength = 8;
var str = new SVGPathString()
.moveTo(x2 + edgeLength, y1)
.lineTo(x1, y1)
.lineTo(x1, y2)
.lineTo(x2 + edgeLength, y2)
.quadTo(x2, (y1 + (y2 - y1) / 2), x2 + edgeLength, y1 );
return str;
}
流行的框架中是否存在类似于第二种方法的东西?我发现这种类型的路径结构对读者更友好。