Skip to content

Commit

Permalink
fix(toPath): dont close path if last point doesnt match last moveTo p…
Browse files Browse the repository at this point in the history
…oint
  • Loading branch information
colinmeinke committed Jan 15, 2017
1 parent 194ad95 commit f143c3b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/toPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import toPoints from './toPoints'
const pointsToD = p => {
let d = ''
let i = 0

const firstPoint = p[ i ]
let firstPointInCurrentLine

for (let point of p) {
const isFirstPoint = i === 0
Expand All @@ -14,6 +13,7 @@ const pointsToD = p => {

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

if (isLastPoint && x === firstPoint.x && y === firstPoint.y) {
if (isLastPoint && x === firstPointInCurrentLine.x && y === firstPointInCurrentLine.y) {
d += 'Z'
}
} else if (isLastPoint && x === firstPoint.x && y === firstPoint.y) {
} else if (isLastPoint && x === firstPointInCurrentLine.x && y === firstPointInCurrentLine.y) {
d += 'Z'
} else if (x !== prevPoint.x && y !== prevPoint.y) {
d += `L${x},${y}`
Expand Down
15 changes: 15 additions & 0 deletions test/toPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,18 @@ test('should return correct paths from a group of points', () => {

expect(paths).toEqual(expectedPaths)
})

test('does not close path when last point does not match corresponding moveTo point', () => {
const points = [
{ x: 40, y: 80, moveTo: true },
{ x: 50, y: 60 },
{ x: 30, y: 40, moveTo: true },
{ x: 40, y: 80 }
]

const expectedPath = 'M40,80L50,60M30,40L40,80'

const path = toPath(points)

expect(path).toEqual(expectedPath)
})

0 comments on commit f143c3b

Please sign in to comment.