You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
915 B
JavaScript

'use strict';
const getImageData = require("./get-image-data");
const scanRow = require("./scan-row");
function findEdge(canvas, firstRow, lastRow, step) {
let imageData = getImageData(canvas).data;
let valuesPerRow = canvas.width * 4;
let hitEnd = false;
if (step === 0) {
throw new Error("Step cannot be 0");
}
let row = firstRow;
while(!hitEnd) {
let highestValue = scanRow(imageData, (row * valuesPerRow), canvas.width);
/* 240 is a somewhat randomly picked value to deal with anti-aliasing. */
if (highestValue > 240) {
return row;
}
row += step;
if (step > 0) {
hitEnd = (row > lastRow);
} else if (step < 0) {
hitEnd = (row < lastRow);
}
}
}
module.exports = {
lowest: function findLowestEdge(canvas) {
return findEdge(canvas, canvas.height - 1, 0, -1);
},
highest: function findHighestEdge(canvas) {
return findEdge(canvas, 0, canvas.height - 1, 1);
}
};