Skip to content

Commit

Permalink
fix(toPath): should close any line where first and last points match
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmeinke committed Jan 15, 2017
1 parent f143c3b commit 89c5a3f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/toPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import toPoints from './toPoints'
const pointsToD = p => {
let d = ''
let i = 0
let firstPointInCurrentLine
let firstPoint

for (let point of p) {
const isFirstPoint = i === 0
const isLastPoint = i === p.length - 1
const prevPoint = isFirstPoint ? null : p[ i - 1 ]
const { curve = false, moveTo, x, y } = point
const isFirstPoint = i === 0 || moveTo
const isLastPoint = i === p.length - 1 || p[ i + 1 ].moveTo
const prevPoint = i === 0 ? null : p[ i - 1 ]

if (isFirstPoint) {
firstPoint = point

if (moveTo || isFirstPoint) {
if (!isLastPoint) {
firstPointInCurrentLine = point
d += `M${x},${y}`
}
} else if (curve) {
Expand All @@ -32,10 +33,10 @@ const pointsToD = p => {
break
}

if (isLastPoint && x === firstPointInCurrentLine.x && y === firstPointInCurrentLine.y) {
if (isLastPoint && x === firstPoint.x && y === firstPoint.y) {
d += 'Z'
}
} else if (isLastPoint && x === firstPointInCurrentLine.x && y === firstPointInCurrentLine.y) {
} else if (isLastPoint && x === firstPoint.x && y === firstPoint.y) {
d += 'Z'
} else if (x !== prevPoint.x && y !== prevPoint.y) {
d += `L${x},${y}`
Expand Down
19 changes: 19 additions & 0 deletions test/toPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,22 @@ test('does not close path when last point does not match corresponding moveTo po

expect(path).toEqual(expectedPath)
})

test('closes any line where first and last points match', () => {
const points = [
{ x: 10, y: 10, moveTo: true },
{ x: 10, y: 100 },
{ x: 100, y: 10, moveTo: true },
{ x: 200, y: 100 },
{ x: 100, y: 100 },
{ x: 100, y: 10 },
{ x: 200, y: 10, moveTo: true },
{ x: 10, y: 10 }
]

const expectedPath = 'M10,10V100M100,10L200,100H100ZM200,10H10'

const path = toPath(points)

expect(path).toEqual(expectedPath)
})

0 comments on commit 89c5a3f

Please sign in to comment.