|
| 1 | +export default function trimCanvas (canvas) { |
| 2 | + let context = canvas.getContext('2d') |
| 3 | + |
| 4 | + let imgWidth = canvas.width |
| 5 | + let imgHeight = canvas.height |
| 6 | + |
| 7 | + let imgData = context.getImageData(0, 0, imgWidth, imgHeight).data |
| 8 | + |
| 9 | + // get the corners of the relevant content (everything that's not white) |
| 10 | + let cropTop = scanY(true, imgWidth, imgHeight, imgData) |
| 11 | + let cropBottom = scanY(false, imgWidth, imgHeight, imgData) |
| 12 | + let cropLeft = scanX(true, imgWidth, imgHeight, imgData) |
| 13 | + let cropRight = scanX(false, imgWidth, imgHeight, imgData) |
| 14 | + |
| 15 | + let cropXDiff = cropRight - cropLeft |
| 16 | + let cropYDiff = cropBottom - cropTop |
| 17 | + |
| 18 | + // get the relevant data from the calculated coordinates |
| 19 | + let trimmedData = context.getImageData(cropLeft, cropTop, cropXDiff, |
| 20 | + cropYDiff) |
| 21 | + |
| 22 | + // set the trimmed width and height |
| 23 | + canvas.width = cropXDiff |
| 24 | + canvas.height = cropYDiff |
| 25 | + // clear the canvas |
| 26 | + context.clearRect(0, 0, cropXDiff, cropYDiff) |
| 27 | + // place the trimmed data into the cleared canvas to create |
| 28 | + // a new, trimmed canvas |
| 29 | + context.putImageData(trimmedData, 0, 0) |
| 30 | + return canvas // for chaining |
| 31 | +} |
| 32 | + |
| 33 | +// returns the RGBA values of an x, y coord of imgData |
| 34 | +function getRGBA (x, y, imgWidth, imgData) { |
| 35 | + return { |
| 36 | + red: imgData[(imgWidth * y + x) * 4], |
| 37 | + green: imgData[(imgWidth * y + x) * 4 + 1], |
| 38 | + blue: imgData[(imgWidth * y + x) * 4 + 2], |
| 39 | + alpha: imgData[(imgWidth * y + x) * 4 + 3] |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function getAlpha (x, y, imgWidth, imgData) { |
| 44 | + return getRGBA(x, y, imgWidth, imgData).alpha |
| 45 | +} |
| 46 | + |
| 47 | +// finds the first y coord in imgData that is not white |
| 48 | +function scanY (fromTop, imgWidth, imgHeight, imgData) { |
| 49 | + let offset = fromTop ? 1 : -1 |
| 50 | + let firstCol = fromTop ? 0 : imgHeight - 1 |
| 51 | + |
| 52 | + // loop through each row |
| 53 | + for (var y = firstCol; fromTop ? (y < imgHeight) : (y > -1); y += offset) { |
| 54 | + // loop through each column |
| 55 | + for (var x = 0; x < imgWidth; x++) { |
| 56 | + // if not white, return col |
| 57 | + if (getAlpha(x, y, imgWidth, imgData)) { |
| 58 | + return y |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // the whole image is white already |
| 64 | + return null |
| 65 | +} |
| 66 | + |
| 67 | +// finds the first x coord in imgData that is not white |
| 68 | +function scanX (fromLeft, imgWidth, imgHeight, imgData) { |
| 69 | + let offset = fromLeft ? 1 : -1 |
| 70 | + let firstRow = fromLeft ? 0 : imgWidth - 1 |
| 71 | + |
| 72 | + // loop through each column |
| 73 | + for (var x = firstRow; fromLeft ? (x < imgWidth) : (x > -1); x += offset) { |
| 74 | + // loop through each row |
| 75 | + for (var y = 0; y < imgHeight; y++) { |
| 76 | + // if not white, return row |
| 77 | + if (getAlpha(x, y, imgWidth, imgData)) { |
| 78 | + return x |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // the whole image is white already |
| 84 | + return null |
| 85 | +} |
0 commit comments