Skip to content

Commit f0e62be

Browse files
mymattcarrollagilgur5
authored andcommitted
(fix): last line of pixels should not be cut off
- off by one difference error caused the last line to be cut off in all situations - e.g. a 5px signature would end up returning a 4px one - also fix some formatting (extra tabs removed)
1 parent 04b29a7 commit f0e62be

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

index.es6

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ export default function trimCanvas (canvas) {
1212
let cropLeft = scanX(true, imgWidth, imgHeight, imgData)
1313
let cropRight = scanX(false, imgWidth, imgHeight, imgData)
1414

15-
let cropXDiff = cropRight - cropLeft
16-
let cropYDiff = cropBottom - cropTop
17-
15+
// + 1 is needed because this is a difference, there are n + 1 pixels in
16+
// between the two numbers inclusive
17+
let cropXDiff = (cropRight - cropLeft) + 1
18+
let cropYDiff = (cropBottom - cropTop) + 1
19+
1820
// get the relevant data from the calculated coordinates
1921
let trimmedData = context.getImageData(cropLeft, cropTop, cropXDiff,
2022
cropYDiff)
@@ -55,8 +57,8 @@ function scanY (fromTop, imgWidth, imgHeight, imgData) {
5557
for (var x = 0; x < imgWidth; x++) {
5658
// if not white, return col
5759
if (getAlpha(x, y, imgWidth, imgData)) {
58-
return y
59-
}
60+
return y
61+
}
6062
}
6163
}
6264

@@ -75,8 +77,8 @@ function scanX (fromLeft, imgWidth, imgHeight, imgData) {
7577
for (var y = 0; y < imgHeight; y++) {
7678
// if not white, return row
7779
if (getAlpha(x, y, imgWidth, imgData)) {
78-
return x
79-
}
80+
return x
81+
}
8082
}
8183
}
8284

0 commit comments

Comments
 (0)