|
@@ -26,6 +26,7 @@
|
|
|
|
|
|
import { SPathCommand } from "./SPathCommand";
|
|
|
import { SPoint } from "./types/SPoint";
|
|
|
+import { SStringBuilder } from "@saga-web/base/lib";
|
|
|
|
|
|
/**
|
|
|
* Path 对象
|
|
@@ -36,6 +37,23 @@ export class SPath {
|
|
|
/** 命令集合 */
|
|
|
cmdList: SPathCommand[] = [];
|
|
|
|
|
|
+ /** 可以转成 svg 命令的命令名称 */
|
|
|
+ static svgCommandList = ["M", "L", "C", "Q", "Z"];
|
|
|
+
|
|
|
+ /*
|
|
|
+ SVG 命令解释:
|
|
|
+ M = moveto 参数:(x y) √
|
|
|
+ L = lineto 参数:(x y) √
|
|
|
+ H = horizontal lineto 参数:(x)
|
|
|
+ V = vertical lineto 参数:(y)
|
|
|
+ C = curveto 参数:(x1 y1 x2 y2 x y) √
|
|
|
+ S = smooth curveto 参数:(x2 y2 x y)
|
|
|
+ Q = quadratic Belzier curve 参数:(x1 y1 x y) √
|
|
|
+ T = smooth quadratic Belzier curveto 参数:(x y)
|
|
|
+ A = elliptical Arc 参数:(rx ry x-axis-rotation large-arc-flag sweep-flag x y)
|
|
|
+ Z = closepath 参数(none) √
|
|
|
+ */
|
|
|
+
|
|
|
/**
|
|
|
* 添加 path 命令
|
|
|
*
|
|
@@ -127,7 +145,7 @@ export class SPath {
|
|
|
y2: number,
|
|
|
radius: number
|
|
|
): void {
|
|
|
- this.addCommand("ArcTo", x1, y1, x2, y2, radius);
|
|
|
+ this.addCommand("A", x1, y1, x2, y2, radius);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -197,12 +215,37 @@ export class SPath {
|
|
|
* @param pList 点集数组
|
|
|
*/
|
|
|
polygon(pList: SPoint[]): void {
|
|
|
+ // 多边形校验,点需要在3个及其以上
|
|
|
if (pList.length > 2) {
|
|
|
this.addCommand("M", pList[0].x, pList[0].y);
|
|
|
+ // 遍历点列表,依次生成命令
|
|
|
for (let i = 1; i < pList.length; i++) {
|
|
|
this.addCommand("L", pList[i].x, pList[i].y);
|
|
|
}
|
|
|
this.addCommand("Z");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * canvas 命令列表转成 svg 命令
|
|
|
+ *
|
|
|
+ * @return 生成的 svg 命令
|
|
|
+ */
|
|
|
+ toSvgPath(): string {
|
|
|
+ let strBuilder = new SStringBuilder();
|
|
|
+
|
|
|
+ // 遍历当前路径的所有命令
|
|
|
+ this.cmdList.forEach((t: SPathCommand): void => {
|
|
|
+ // 当前支持转化的命令列表包含此命令
|
|
|
+ if (SPath.svgCommandList.includes(t.command)) {
|
|
|
+ strBuilder.append(t.command);
|
|
|
+ // 遍历该命令的参数 依次拼接
|
|
|
+ t.args.forEach((item): void => {
|
|
|
+ strBuilder.append(item + " ");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return strBuilder.toString();
|
|
|
+ }
|
|
|
}
|