Guest User

Untitled

a guest
Jan 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. /**
  2. * get a rectangle around color
  3. * @param {...} ctx 2dCanvasObject to be scanned
  4. * @return {Object} object storing the rectangle's data (x, y, w(idth), h(eight))
  5. */
  6. function getColorBoundsRect(ctx) {
  7. /**
  8. * the canvas' context's data property (shorthand)
  9. * @type {...}
  10. */
  11. var data = ctx.data,
  12. /**
  13. * counter variable
  14. * @type {Number}
  15. */
  16. i = 0,
  17. /**
  18. * the "leftest" pixel that is not black (starts right, as we check if currently looped pixel (that is not black) is "lefter" than the current outerLeftPixel)
  19. * @type {Number}
  20. */
  21. outerLeftPixel = w-1,
  22. /**
  23. * the "rightest" pixel that is not black (starts left, as we check if currently looped pixel (that is not black) is "righter" than the current outerRightPixel)
  24. * @type {Number}
  25. */
  26. outerRightPixel = 0,
  27. /**
  28. * the "toppest" pixel that is not black (starts at bottom, as we check if currently looped pixel (that is not black) is "topper" than the current outerTopPixel)
  29. * @type {Number}
  30. */
  31. outerTopPixel = h-1,
  32. /**
  33. * the "bottomest" pixel that is not black (starts at top, as we check if currently looped pixel (that is not black) is "bottomer" than the current outerBottomPixel)
  34. * @type {Number}
  35. */
  36. outerBottomPixel = 0,
  37. /**
  38. * x coordinate of currently looped pixel
  39. * @type {Number}
  40. */
  41. x,
  42. /**
  43. * y coordinate of currently looped pixel
  44. * @type {Number}
  45. */
  46. y;
  47.  
  48. // loop through all pixels
  49. // i equals the i'th pixel (0 is the upper left pixel, w*h is the bottom right pixel)
  50. while (i < (data.length / 4)) {
  51. // check if currently looped pixel is anything else than black --> color
  52. if ((data[i*4] + data[i*4+1] + data[i*4+2]) > 0) {
  53. // set coordinates for the currently looped pixel
  54. x = i % w; // if one row has 10px and i = 35, the x coordinate of the current pixel is 35 % 10 = 5
  55. y = Math.floor(i / w); // if one row has 10px and i=35, the y coordinate of the current pixel is 35/10 = 3.5 (--> rounded off = 3)
  56.  
  57. // if the x coordinate of the current (colored) pixel is smaller than the current "leftest" pixel, set the x coordinate as new "leftest pixel"
  58. // same procedure for the other values
  59. if (x < outerLeftPixel) {
  60. outerLeftPixel = x;
  61. }
  62. if (x > outerRightPixel) {
  63. outerRightPixel = x;
  64. }
  65. if (y < outerTopPixel) {
  66. outerTopPixel = y;
  67. }
  68. if (y > outerBottomPixel) {
  69. outerBottomPixel = y;
  70. }
  71. }
  72. ++i;
  73. }
  74.  
  75. // if there is color on the canvas, the outer[Right|Left|Bottom|Top]Pixel properties should have been updated accordingly and the following condition should be true
  76. if (outerRightPixel > outerLeftPixel && outerBottomPixel > outerTopPixel) {
  77. return {
  78. x: outerLeftPixel,
  79. y: outerTopPixel,
  80. w: outerRightPixel - outerLeftPixel,
  81. h: outerBottomPixel - outerTopPixel
  82. };
  83. }
  84. // if there is no color on the canvas, return false, as there is no rectangle
  85. else {
  86. return false;
  87. }
  88. }
Add Comment
Please, Sign In to add comment