我嘗試通過拉斐爾框架的svg功能(它的路徑文檔是here)。我發現編寫路徑字符串非常乏味,因爲在創建新路徑或查看已寫入的路徑時,我必須不斷查找格式規範。有沒有簡化這個過程的框架?例如,而不是寫類似:更容易定義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;
}
是否像第二種方法任何東西存在流行的框架?我發現這種類型的路徑建設更加友好。
這是一個有趣的DIY方法。比我提出的更詳細一點,但肯定比目前提供的路徑語法更具信息性。 –