Skip to content

Commit

Permalink
feat: remove addPoint methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kmkzt committed May 16, 2020
1 parent c264b2d commit b6ec4a1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/drawing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions src/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
})
Expand Down
55 changes: 24 additions & 31 deletions src/svg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
)
})
Expand Down
24 changes: 11 additions & 13 deletions src/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit b6ec4a1

Please sign in to comment.