diff --git a/src/drawing.ts b/src/drawing.ts index 7ec1501bd..715411e1e 100644 --- a/src/drawing.ts +++ b/src/drawing.ts @@ -113,13 +113,13 @@ export class SvgDrawing extends Renderer { private _drawingMove({ x, y }: { x: number; y: number }): void { const po = this._createDrawPoint({ x, y }) - this.addPoint(po) + this.addCommand(po) if ( !this._drawPath || this._drawPath.strokeWidth !== this.penWidth || this._drawPath.stroke !== this.penColor ) { - this._drawPath = this._createDrawPath().addPoint(po) + this._drawPath = this._createDrawPath().addCommand(po) this.addPath(this._drawPath) } this._updateRender() diff --git a/src/renderer.test.ts b/src/renderer.test.ts index 0572dad6e..49afc92d5 100644 --- a/src/renderer.test.ts +++ b/src/renderer.test.ts @@ -10,18 +10,18 @@ describe('renderer', () => { .addPath( new Path({ circuler: true, close: false }) - .addPoint(new Point(0, 0)) - .addPoint(new Point(1, 1)) - .addPoint(new Point(2, 1)) - .addPoint(new Point(3, 0)) + .addCommand(new Point(0, 0)) + .addCommand(new Point(1, 1)) + .addCommand(new Point(2, 1)) + .addCommand(new Point(3, 0)) .formatCommand() ) .addPath( new Path({ circuler: false, close: true }) - .addPoint(new Point(4, 4)) - .addPoint(new Point(9, 4)) - .addPoint(new Point(9, 8)) - .addPoint(new Point(3, 0)) + .addCommand(new Point(4, 4)) + .addCommand(new Point(9, 4)) + .addCommand(new Point(9, 8)) + .addCommand(new Point(3, 0)) .formatCommand() ) }) diff --git a/src/svg.test.ts b/src/svg.test.ts index f4bf02470..33d489fda 100644 --- a/src/svg.test.ts +++ b/src/svg.test.ts @@ -93,22 +93,15 @@ describe('svg', () => { let path: Path beforeEach(() => { path = new Path({ strokeWidth: 1 }) - .addPoint(new Point(1, 1)) - .addPoint(new Point(2, 2)) + .addCommand(new Point(1, 1)) + .addCommand(new Point(2, 2)) }) - it('addPoint', () => { + it('addCommand', () => { expect(path.commands.length).toBe(2) expect(path.commands[0].type).toBe(CommandType.MOVE) expect(path.commands[0].point.x).toBe(1) expect(path.commands[0].point.y).toBe(1) }) - it('addCommand', () => { - path.addCommand(new Command(CommandType.LINE, new Point(1, 1))) - expect(path.commands.length).toBe(3) - expect(path.commands[2].type).toBe(CommandType.LINE) - expect(path.commands[2].point.x).toBe(1) - expect(path.commands[2].point.y).toBe(1) - }) it('scale', () => { path.scale(2) expect(path.strokeWidth).toBe(2) @@ -117,8 +110,8 @@ describe('svg', () => { expect(path.commands[0].point.y).toBe(2) }) it('clone', () => { - const origin = new Path({ strokeWidth: 1 }).addPoint(new Point(1, 1)) - const clone = origin.clone().addPoint(new Point(2, 2)) + const origin = new Path({ strokeWidth: 1 }).addCommand(new Point(1, 1)) + const clone = origin.clone().addCommand(new Point(2, 2)) clone.commands[0].point.x = 3 expect(origin.commands.length).toBe(1) expect(clone.commands.length).toBe(2) @@ -127,10 +120,10 @@ describe('svg', () => { }) describe('toJson and toElement', () => { const path = new Path({ circuler: true, close: false }) - .addPoint(new Point(0, 0)) - .addPoint(new Point(1, 1)) - .addPoint(new Point(2, 1)) - .addPoint(new Point(3, 0)) + .addCommand(new Point(0, 0)) + .addCommand(new Point(1, 1)) + .addCommand(new Point(2, 1)) + .addCommand(new Point(3, 0)) .formatCommand() it('toJson', () => { expect(path.toJson()).toMatchSnapshot() @@ -142,9 +135,9 @@ describe('svg', () => { describe('formatCommand and getCommandString', () => { describe('Line', () => { const path = new Path({ circuler: false }) - .addPoint(new Point(0, 0)) - .addPoint(new Point(1, 1)) - .addPoint(new Point(-1, -1)) + .addCommand(new Point(0, 0)) + .addCommand(new Point(1, 1)) + .addCommand(new Point(-1, -1)) it('Normal', () => { path.close = false expect(path.formatCommand().commands).toMatchSnapshot() @@ -158,10 +151,10 @@ describe('svg', () => { }) describe('Circuler', () => { const path = new Path({ circuler: true }) - .addPoint(new Point(0, 0)) - .addPoint(new Point(1, 1)) - .addPoint(new Point(2, 1)) - .addPoint(new Point(3, 0)) + .addCommand(new Point(0, 0)) + .addCommand(new Point(1, 1)) + .addCommand(new Point(2, 1)) + .addCommand(new Point(3, 0)) it('Normal', () => { path.close = false expect(path.formatCommand().commands).toMatchSnapshot() @@ -181,18 +174,18 @@ describe('svg', () => { svg = new Svg({ width: 500, height: 500 }) .addPath( new Path({ circuler: true, close: false }) - .addPoint(new Point(0, 0)) - .addPoint(new Point(1, 1)) - .addPoint(new Point(2, 1)) - .addPoint(new Point(3, 0)) + .addCommand(new Point(0, 0)) + .addCommand(new Point(1, 1)) + .addCommand(new Point(2, 1)) + .addCommand(new Point(3, 0)) .formatCommand() ) .addPath( new Path({ circuler: false, close: true }) - .addPoint(new Point(4, 4)) - .addPoint(new Point(9, 4)) - .addPoint(new Point(9, 8)) - .addPoint(new Point(3, 0)) + .addCommand(new Point(4, 4)) + .addCommand(new Point(9, 4)) + .addCommand(new Point(9, 8)) + .addCommand(new Point(3, 0)) .formatCommand() ) }) diff --git a/src/svg.ts b/src/svg.ts index cfffcbaee..82c682b3b 100644 --- a/src/svg.ts +++ b/src/svg.ts @@ -150,17 +150,15 @@ export class Path { return this } - public addPoint(po: Point): this { - this.commands.push( - new Command( - this.commands.length === 0 ? CommandType.MOVE : CommandType.LINE, - po - ) - ) - return this - } - public addCommand(c: Command): this { - this.commands.push(c) + public addCommand(param: Command | Point): this { + const cmd = + param instanceof Point + ? new Command( + this.commands.length === 0 ? CommandType.MOVE : CommandType.LINE, + param + ) + : param + this.commands.push(cmd) return this } @@ -322,10 +320,10 @@ export class Svg { return this._paths.map(p => p.clone()) } - public addPoint(po: Point): this { + public addCommand(po: Point): this { if (this._paths.length === 0) return this const updateIndex = this._paths.length - 1 - this._paths[updateIndex].addPoint(po) + this._paths[updateIndex].addCommand(po) return this }