|
@@ -91,17 +91,6 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
|
|
|
// =================================================================================================================
|
|
|
// 绘制图形
|
|
|
- /**
|
|
|
- * 设置裁剪路径
|
|
|
- *
|
|
|
- * @param path 裁剪路径
|
|
|
- */
|
|
|
- setClip(path: Path2D): void {
|
|
|
- this.setMatrix();
|
|
|
- // this._canvas.stroke(path);
|
|
|
- this._canvas.fill(path);
|
|
|
- this._canvas.clip();
|
|
|
- } // Function setClip()
|
|
|
|
|
|
/**
|
|
|
* 清空矩形区域
|
|
@@ -119,13 +108,12 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
* @param rect 矩形
|
|
|
*/
|
|
|
drawRect(rect: SRect): void {
|
|
|
- this.setMatrix();
|
|
|
- this.setPen();
|
|
|
- this.setBrush();
|
|
|
- this.setComposite();
|
|
|
- this.setShadow();
|
|
|
- this._canvas.fillRect(rect.x, rect.y, rect.width, rect.height);
|
|
|
- this._canvas.strokeRect(rect.x, rect.y, rect.width, rect.height);
|
|
|
+ this.init(() => {
|
|
|
+ this._canvas.beginPath();
|
|
|
+ this._canvas.rect(rect.x, rect.y, rect.width, rect.height);
|
|
|
+ this._canvas.stroke();
|
|
|
+ this._canvas.fill();
|
|
|
+ });
|
|
|
} // Function drawRect()
|
|
|
|
|
|
/**
|
|
@@ -136,15 +124,12 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
* @param r 圆半径
|
|
|
*/
|
|
|
drawCircle(cx: number, cy: number, r: number): void {
|
|
|
- this.setMatrix();
|
|
|
- this.setPen();
|
|
|
- this.setBrush();
|
|
|
- this.setComposite();
|
|
|
- this.setShadow();
|
|
|
- this._canvas.beginPath();
|
|
|
- this._canvas.arc(cx, cy, r, 0, 2 * Math.PI, true);
|
|
|
- this._canvas.fill();
|
|
|
- this._canvas.stroke();
|
|
|
+ this.init(() => {
|
|
|
+ this._canvas.beginPath();
|
|
|
+ this._canvas.arc(cx, cy, r, 0, 2 * Math.PI, true);
|
|
|
+ this._canvas.fill();
|
|
|
+ this._canvas.stroke();
|
|
|
+ });
|
|
|
} // Function drawCircle()
|
|
|
|
|
|
/**
|
|
@@ -156,13 +141,12 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
* @param ry 垂直半径
|
|
|
*/
|
|
|
drawEllipse(cx: number, cy: number, rx: number, ry: number): void {
|
|
|
- this.setMatrix();
|
|
|
- this.setPen();
|
|
|
- this.setBrush();
|
|
|
- this.setComposite();
|
|
|
- this.setShadow();
|
|
|
- this._canvas.beginPath();
|
|
|
- this._canvas.ellipse(cx, cy, rx, ry, 0.5, 0, 2 * Math.PI, true);
|
|
|
+ this.init(() => {
|
|
|
+ this._canvas.beginPath();
|
|
|
+ this._canvas.ellipse(cx, cy, rx, ry, 0.5, 0, 2 * Math.PI, true);
|
|
|
+ this._canvas.fill();
|
|
|
+ this._canvas.stroke();
|
|
|
+ });
|
|
|
} // Function drawEllipse()
|
|
|
|
|
|
/**
|
|
@@ -171,15 +155,12 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
* @param line 线段
|
|
|
*/
|
|
|
drawLine(line: SLine): void {
|
|
|
- this.setMatrix();
|
|
|
- this.setPen();
|
|
|
- this.setBrush();
|
|
|
- this.setComposite();
|
|
|
- this.setShadow();
|
|
|
- this._canvas.beginPath();
|
|
|
- this._canvas.moveTo(line.x1, line.y1);
|
|
|
- this._canvas.lineTo(line.x2, line.y2);
|
|
|
- this._canvas.stroke();
|
|
|
+ this.init(() => {
|
|
|
+ this._canvas.beginPath();
|
|
|
+ this._canvas.moveTo(line.x1, line.y1);
|
|
|
+ this._canvas.lineTo(line.x2, line.y2);
|
|
|
+ this._canvas.stroke();
|
|
|
+ });
|
|
|
} // Function drawLine()
|
|
|
|
|
|
/**
|
|
@@ -193,19 +174,14 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- this.setMatrix();
|
|
|
- this.setPen();
|
|
|
- this.setBrush();
|
|
|
- this.setComposite();
|
|
|
- this.setShadow();
|
|
|
-
|
|
|
- this._canvas.beginPath();
|
|
|
- this._canvas.moveTo(points[0].x, points[0].y);
|
|
|
- for (let p of points) {
|
|
|
- this._canvas.lineTo(p.x, p.y);
|
|
|
- }
|
|
|
-
|
|
|
- this._canvas.stroke();
|
|
|
+ this.init(() => {
|
|
|
+ this._canvas.beginPath();
|
|
|
+ this._canvas.moveTo(points[0].x, points[0].y);
|
|
|
+ for (let p of points) {
|
|
|
+ this._canvas.lineTo(p.x, p.y);
|
|
|
+ }
|
|
|
+ this._canvas.stroke();
|
|
|
+ });
|
|
|
} // Function drawPolyline()
|
|
|
|
|
|
/**
|
|
@@ -219,21 +195,17 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- this.setMatrix();
|
|
|
- this.setPen();
|
|
|
- this.setBrush();
|
|
|
- this.setComposite();
|
|
|
- this.setShadow();
|
|
|
-
|
|
|
- this._canvas.beginPath();
|
|
|
- this._canvas.moveTo(points[0].x, points[0].y);
|
|
|
- for (let p of points) {
|
|
|
- this._canvas.lineTo(p.x, p.y);
|
|
|
- }
|
|
|
- this._canvas.closePath();
|
|
|
+ this.init(() => {
|
|
|
+ this._canvas.beginPath();
|
|
|
+ this._canvas.moveTo(points[0].x, points[0].y);
|
|
|
+ for (let p of points) {
|
|
|
+ this._canvas.lineTo(p.x, p.y);
|
|
|
+ }
|
|
|
+ this._canvas.closePath();
|
|
|
|
|
|
- this._canvas.fill();
|
|
|
- this._canvas.stroke();
|
|
|
+ this._canvas.fill();
|
|
|
+ this._canvas.stroke();
|
|
|
+ });
|
|
|
} // Function drawPolygon()
|
|
|
|
|
|
/**
|
|
@@ -242,61 +214,9 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
* @param path 路径
|
|
|
*/
|
|
|
drawPath(path: SPath): void {
|
|
|
- path.cmdList.forEach(cmd => {
|
|
|
- if (cmd.command == "L") {
|
|
|
- this._canvas.lineTo(cmd.args[0], cmd.args[1]);
|
|
|
- } else if (cmd.command == "M") {
|
|
|
- this._canvas.moveTo(cmd.args[0], cmd.args[1]);
|
|
|
- } else if (cmd.command == "Z") {
|
|
|
- this._canvas.closePath();
|
|
|
- } else if (cmd.command == "Q") {
|
|
|
- this._canvas.quadraticCurveTo(
|
|
|
- cmd.args[0],
|
|
|
- cmd.args[1],
|
|
|
- cmd.args[2],
|
|
|
- cmd.args[3]
|
|
|
- );
|
|
|
- } else if (cmd.command == "T") {
|
|
|
- // @ts-ignore
|
|
|
- this._canvas.bezierCurveTo(
|
|
|
- cmd.args[0],
|
|
|
- cmd.args[1],
|
|
|
- cmd.args[2],
|
|
|
- cmd.args[3],
|
|
|
- cmd.args[4]
|
|
|
- );
|
|
|
- } else if (cmd.command == "Ellipse") {
|
|
|
- this._canvas.ellipse(
|
|
|
- cmd.args[0],
|
|
|
- cmd.args[1],
|
|
|
- cmd.args[2],
|
|
|
- cmd.args[3],
|
|
|
- 0,
|
|
|
- 0,
|
|
|
- Math.PI * 2
|
|
|
- );
|
|
|
- } else if (cmd.command == "Arc") {
|
|
|
- this._canvas.arc(
|
|
|
- cmd.args[0],
|
|
|
- cmd.args[1],
|
|
|
- cmd.args[2],
|
|
|
- cmd.args[3],
|
|
|
- cmd.args[4],
|
|
|
- cmd.args[5] == 1
|
|
|
- );
|
|
|
- } else if (cmd.command == "ArcTo") {
|
|
|
- this._canvas.arcTo(
|
|
|
- cmd.args[0],
|
|
|
- cmd.args[1],
|
|
|
- cmd.args[2],
|
|
|
- cmd.args[3],
|
|
|
- cmd.args[4]
|
|
|
- );
|
|
|
- }
|
|
|
+ this.init(() => {
|
|
|
+ this.drawWay(path);
|
|
|
});
|
|
|
-
|
|
|
- this._canvas.fill();
|
|
|
- this._canvas.stroke();
|
|
|
} // Function drawPath()
|
|
|
|
|
|
/**
|
|
@@ -308,18 +228,14 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
* @param maxWidth 最大宽度
|
|
|
*/
|
|
|
drawText(text: string, x: number, y: number, maxWidth?: number): void {
|
|
|
- this.setMatrix();
|
|
|
- this.setPen();
|
|
|
- this.setBrush();
|
|
|
- this.setFont();
|
|
|
- this.setComposite();
|
|
|
- this.setShadow();
|
|
|
-
|
|
|
- if (maxWidth == undefined) {
|
|
|
- this._canvas.fillText(text, x, y);
|
|
|
- } else {
|
|
|
- this._canvas.fillText(text, x, y, maxWidth);
|
|
|
- }
|
|
|
+ this.init(() => {
|
|
|
+ this.setFont();
|
|
|
+ if (maxWidth == undefined) {
|
|
|
+ this._canvas.fillText(text, x, y);
|
|
|
+ } else {
|
|
|
+ this._canvas.fillText(text, x, y, maxWidth);
|
|
|
+ }
|
|
|
+ });
|
|
|
} // Function drawText()
|
|
|
|
|
|
/**
|
|
@@ -338,16 +254,17 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
width?: number,
|
|
|
height?: number
|
|
|
): void {
|
|
|
- this.setMatrix();
|
|
|
- try {
|
|
|
- if (width == undefined) {
|
|
|
- this._canvas.drawImage(img, x, y);
|
|
|
- } else {
|
|
|
- this._canvas.drawImage(img, x, y, width, height as number);
|
|
|
+ this.init(() => {
|
|
|
+ try {
|
|
|
+ if (width == undefined) {
|
|
|
+ this._canvas.drawImage(img, x, y);
|
|
|
+ } else {
|
|
|
+ this._canvas.drawImage(img, x, y, width, height as number);
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ console.log(e);
|
|
|
}
|
|
|
- } catch (e) {
|
|
|
- console.log(e);
|
|
|
- }
|
|
|
+ });
|
|
|
} // Function drawImage()
|
|
|
|
|
|
/**
|
|
@@ -400,38 +317,35 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
* @param r 导角半径
|
|
|
*/
|
|
|
drawRoundRect(rect: SRect, r: number): void {
|
|
|
- this.setMatrix();
|
|
|
- this.setPen();
|
|
|
- this.setBrush();
|
|
|
- this.setComposite();
|
|
|
- this.setShadow();
|
|
|
- this._canvas.beginPath();
|
|
|
- this._canvas.moveTo(rect.x, rect.y + r);
|
|
|
- this._canvas.lineTo(rect.x, rect.bottom - r);
|
|
|
- this._canvas.quadraticCurveTo(
|
|
|
- rect.x,
|
|
|
- rect.bottom,
|
|
|
- rect.x + r,
|
|
|
- rect.bottom
|
|
|
- );
|
|
|
- this._canvas.lineTo(rect.right - r, rect.bottom);
|
|
|
- this._canvas.quadraticCurveTo(
|
|
|
- rect.right,
|
|
|
- rect.bottom,
|
|
|
- rect.right,
|
|
|
- rect.bottom - r
|
|
|
- );
|
|
|
- this._canvas.lineTo(rect.right, rect.y + r);
|
|
|
- this._canvas.quadraticCurveTo(
|
|
|
- rect.right,
|
|
|
- rect.y,
|
|
|
- rect.right - r,
|
|
|
- rect.y
|
|
|
- );
|
|
|
- this._canvas.lineTo(rect.x + r, rect.y);
|
|
|
- this._canvas.quadraticCurveTo(rect.x, rect.y, rect.x, rect.y + r);
|
|
|
- this._canvas.fill();
|
|
|
- this._canvas.stroke();
|
|
|
+ this.init(() => {
|
|
|
+ this._canvas.beginPath();
|
|
|
+ this._canvas.moveTo(rect.x, rect.y + r);
|
|
|
+ this._canvas.lineTo(rect.x, rect.bottom - r);
|
|
|
+ this._canvas.quadraticCurveTo(
|
|
|
+ rect.x,
|
|
|
+ rect.bottom,
|
|
|
+ rect.x + r,
|
|
|
+ rect.bottom
|
|
|
+ );
|
|
|
+ this._canvas.lineTo(rect.right - r, rect.bottom);
|
|
|
+ this._canvas.quadraticCurveTo(
|
|
|
+ rect.right,
|
|
|
+ rect.bottom,
|
|
|
+ rect.right,
|
|
|
+ rect.bottom - r
|
|
|
+ );
|
|
|
+ this._canvas.lineTo(rect.right, rect.y + r);
|
|
|
+ this._canvas.quadraticCurveTo(
|
|
|
+ rect.right,
|
|
|
+ rect.y,
|
|
|
+ rect.right - r,
|
|
|
+ rect.y
|
|
|
+ );
|
|
|
+ this._canvas.lineTo(rect.x + r, rect.y);
|
|
|
+ this._canvas.quadraticCurveTo(rect.x, rect.y, rect.x, rect.y + r);
|
|
|
+ this._canvas.fill();
|
|
|
+ this._canvas.stroke();
|
|
|
+ });
|
|
|
} // Function drawRoundRect()
|
|
|
|
|
|
/**
|
|
@@ -448,6 +362,24 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
// 私有函数
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置整体绘制状态
|
|
|
+ *
|
|
|
+ * fn 绘制图形的具体函数
|
|
|
+ * */
|
|
|
+ private init(fn: Function): void {
|
|
|
+ this.setMatrix();
|
|
|
+ this.setPen();
|
|
|
+ this.setBrush();
|
|
|
+ this.setComposite();
|
|
|
+ this.setShadow();
|
|
|
+ this.setClip();
|
|
|
+ fn();
|
|
|
+ this._canvas.fill();
|
|
|
+ this._canvas.stroke();
|
|
|
+ } // Function init()
|
|
|
+
|
|
|
/**
|
|
|
* 设置画笔
|
|
|
*/
|
|
@@ -649,4 +581,77 @@ export class SCanvasPaintEngine extends SPaintEngine {
|
|
|
this.state.matrix.f
|
|
|
);
|
|
|
} // Function setMatrix()
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置裁剪路径
|
|
|
+ *
|
|
|
+ */
|
|
|
+ setClip(): void {
|
|
|
+ if (this.state.clip) {
|
|
|
+ this.drawWay(this.state.clip);
|
|
|
+ this._canvas.clip();
|
|
|
+ }
|
|
|
+ } // Function setClip()
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 绘制路径
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ private drawWay(path: SPath): void {
|
|
|
+ this._canvas.beginPath();
|
|
|
+ path.cmdList.forEach(cmd => {
|
|
|
+ if (cmd.command == "L") {
|
|
|
+ this._canvas.lineTo(cmd.args[0], cmd.args[1]);
|
|
|
+ } else if (cmd.command == "M") {
|
|
|
+ this._canvas.moveTo(cmd.args[0], cmd.args[1]);
|
|
|
+ } else if (cmd.command == "Z") {
|
|
|
+ this._canvas.closePath();
|
|
|
+ } else if (cmd.command == "Q") {
|
|
|
+ this._canvas.quadraticCurveTo(
|
|
|
+ cmd.args[0],
|
|
|
+ cmd.args[1],
|
|
|
+ cmd.args[2],
|
|
|
+ cmd.args[3]
|
|
|
+ );
|
|
|
+ } else if (cmd.command == "T") {
|
|
|
+ // @ts-ignore
|
|
|
+ this._canvas.bezierCurveTo(
|
|
|
+ cmd.args[0],
|
|
|
+ cmd.args[1],
|
|
|
+ cmd.args[2],
|
|
|
+ cmd.args[3],
|
|
|
+ cmd.args[4]
|
|
|
+ );
|
|
|
+ } else if (cmd.command == "Ellipse") {
|
|
|
+ this._canvas.ellipse(
|
|
|
+ cmd.args[0],
|
|
|
+ cmd.args[1],
|
|
|
+ cmd.args[2],
|
|
|
+ cmd.args[3],
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ Math.PI * 2
|
|
|
+ );
|
|
|
+ } else if (cmd.command == "Arc") {
|
|
|
+ this._canvas.arc(
|
|
|
+ cmd.args[0],
|
|
|
+ cmd.args[1],
|
|
|
+ cmd.args[2],
|
|
|
+ cmd.args[3],
|
|
|
+ cmd.args[4],
|
|
|
+ cmd.args[5] == 1
|
|
|
+ );
|
|
|
+ } else if (cmd.command == "ArcTo") {
|
|
|
+ this._canvas.arcTo(
|
|
|
+ cmd.args[0],
|
|
|
+ cmd.args[1],
|
|
|
+ cmd.args[2],
|
|
|
+ cmd.args[3],
|
|
|
+ cmd.args[4]
|
|
|
+ );
|
|
|
+ }
|
|
|
+ });
|
|
|
+ this._canvas.fill();
|
|
|
+ this._canvas.stroke();
|
|
|
+ } // Function drawWay()
|
|
|
} // class SPaintEngine
|