Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getEdges(tempcanv, colour, which, condition) {
- let ledge = [], redge = [], tedge = [], bedge = [];
- const canvwidth = tempcanv.width, canvheight = tempcanv.height;
- const contextd = tempcanv.getContext('2d');
- for (let y = 0; y < canvheight; y++) {//left edge
- for (let x = 0; x < canvwidth; x++) {
- const data = contextd.getImageData(x, y, 1, 1).data;
- if (condition(data[which], colour)) {
- ledge.push(x);
- break;
- }
- }
- }
- ledge = Math.min(...ledge);
- const llim = ledge === 0 ? ledge : ledge - 1;
- for (let x = llim; x < canvwidth; x++) {//top edge
- for (let y = 0; y < canvheight; y++) {
- const data = contextd.getImageData(x, y, 1, 1).data;
- if (condition(data[which], colour)) {
- tedge.push(y);
- break;
- }
- }
- }
- tedge = Math.min(...tedge);
- const tlim = tedge === 0 ? tedge : tedge - 1;
- for (let y = tlim; y < canvheight; y++) {//right edge
- for (let x = canvwidth - 1; x >= ledge; x--) {
- const data = contextd.getImageData(x, y, 1, 1).data;
- if (condition(data[which], colour)) {
- redge.push(x);
- break;
- }
- }
- }
- redge = Math.max(...redge);
- for (let x = llim; x <= redge; x++) {//bottom edge
- for (let y = canvheight - 1; y >= tedge; y--) {
- const data = contextd.getImageData(x, y, 1, 1).data;
- if (condition(data[which], colour)) {
- bedge.push(y);
- break;
- }
- }
- }
- return [ledge, tedge, redge + 1, Math.max(...bedge) + 1]
- }
- //(canvas passed, colour threshold, R,G or B, greater or less than)
- getEdges(canvas, 180, 0, (a, b) => a > b))
- function getEdges(tempcanv, colour, which, condition) {
- let ledge = -1, redge = -1, tedge = -1, bedge = -1;
- const canvwidth = tempcanv.width, canvheight = tempcanv.height;
- const contextd = tempcanv.getContext('2d');
- const imageData = contextd.getImageData(0, 0, canvwidth, canvheight).data; //gets all image data
- for (let x = 0; x < canvwidth; x++) {//left edge
- if(ledge > 0 ) {
- break;
- }
- for (let y = 0; y < canvheight; y++) {
- //(x*canvwidth+y)*3 is for getting the current location
- if (condition(imageData[(y*canvwidth+x)*3+which], colour)) { //might need to switch x and y? I am not sure in which order getImageData is
- ledge = x;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement