Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. function getEdges(tempcanv, colour, which, condition) {
  2. let ledge = [], redge = [], tedge = [], bedge = [];
  3. const canvwidth = tempcanv.width, canvheight = tempcanv.height;
  4. const contextd = tempcanv.getContext('2d');
  5. for (let y = 0; y < canvheight; y++) {//left edge
  6. for (let x = 0; x < canvwidth; x++) {
  7. const data = contextd.getImageData(x, y, 1, 1).data;
  8. if (condition(data[which], colour)) {
  9. ledge.push(x);
  10. break;
  11. }
  12. }
  13. }
  14. ledge = Math.min(...ledge);
  15. const llim = ledge === 0 ? ledge : ledge - 1;
  16. for (let x = llim; x < canvwidth; x++) {//top edge
  17. for (let y = 0; y < canvheight; y++) {
  18. const data = contextd.getImageData(x, y, 1, 1).data;
  19. if (condition(data[which], colour)) {
  20. tedge.push(y);
  21. break;
  22. }
  23. }
  24. }
  25. tedge = Math.min(...tedge);
  26. const tlim = tedge === 0 ? tedge : tedge - 1;
  27. for (let y = tlim; y < canvheight; y++) {//right edge
  28. for (let x = canvwidth - 1; x >= ledge; x--) {
  29. const data = contextd.getImageData(x, y, 1, 1).data;
  30. if (condition(data[which], colour)) {
  31. redge.push(x);
  32. break;
  33. }
  34. }
  35. }
  36. redge = Math.max(...redge);
  37. for (let x = llim; x <= redge; x++) {//bottom edge
  38. for (let y = canvheight - 1; y >= tedge; y--) {
  39. const data = contextd.getImageData(x, y, 1, 1).data;
  40. if (condition(data[which], colour)) {
  41. bedge.push(y);
  42. break;
  43. }
  44. }
  45. }
  46. return [ledge, tedge, redge + 1, Math.max(...bedge) + 1]
  47. }
  48.  
  49. //(canvas passed, colour threshold, R,G or B, greater or less than)
  50. getEdges(canvas, 180, 0, (a, b) => a > b))
  51.  
  52. function getEdges(tempcanv, colour, which, condition) {
  53. let ledge = -1, redge = -1, tedge = -1, bedge = -1;
  54. const canvwidth = tempcanv.width, canvheight = tempcanv.height;
  55. const contextd = tempcanv.getContext('2d');
  56. const imageData = contextd.getImageData(0, 0, canvwidth, canvheight).data; //gets all image data
  57. for (let x = 0; x < canvwidth; x++) {//left edge
  58. if(ledge > 0 ) {
  59. break;
  60. }
  61. for (let y = 0; y < canvheight; y++) {
  62. //(x*canvwidth+y)*3 is for getting the current location
  63. 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
  64. ledge = x;
  65. break;
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement