Pixel.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. const kronecker = function(target, mask) {
  2. const result = [[Math.ceil(target[0] / mask[0]), Math.ceil(target[1] / mask[1])],
  3. [((target[0] - 1) % mask[0]) + 1, ((target[1] - 1) % mask[1]) + 1]];
  4. var quadrant = result[0].join("");
  5. switch (quadrant) {
  6. case "11": quadrant = "00"; break;
  7. case "12": quadrant = "01"; break;
  8. case "21": quadrant = "10"; break;
  9. case "22": quadrant = "11"; break;
  10. }
  11. if (mask[0] == 1)
  12. return quadrant;
  13. return (quadrant + kronecker(result[1], [mask[0] / 2, mask[1] / 2]));
  14. }
  15. const dotProduct = function(U, V) {
  16. return U.x * V.x + U.y * V.y;
  17. };
  18. const isInside = function(vertex, square) {
  19. const AB = { x: square.b.x - square.a.x, y: square.b.y - square.a.y };
  20. const AM = { x: vertex.x - square.a.x, y: vertex.y - square.a.y };
  21. const BC = { x: square.c.x - square.b.x, y: square.c.y - square.b.y };
  22. const BM = { x: vertex.x - square.b.x, y: vertex.y - square.b.y };
  23. return (0 <= dotProduct(AB, AM) && dotProduct(AB, AM) <= dotProduct(AB, AB))
  24. && (0 <= dotProduct(BC, BM) && dotProduct(BC, BM) <= dotProduct(BC, BC));
  25. };
  26. const isInsideSquare = function(vertex, square, isize) {
  27. return isInside(vertex, square)
  28. || isInside({x: vertex.x + isize.width, y: vertex.y}, square)
  29. || isInside({x: vertex.x, y: vertex.y + isize.height}, square)
  30. || isInside({x: vertex.x + isize.width, y: vertex.y + isize.height}, square);
  31. };
  32. const toBinary = function(decimal, padding) {
  33. return decimal.toString(2).padStart(padding, 0);
  34. };
  35. const getId = function (source) {
  36. return /[01]+$/.exec(source)[0];
  37. };
  38. const canvas = {
  39. get boundary() { return document.getElementById("canvas").getBoundingClientRect() },
  40. vertices: {
  41. get a() { return { x: 0, y: 0 }; },
  42. get b() { return { x: 0, y: canvas.boundary.width }; },
  43. get c() { return { x: canvas.boundary.height, y: canvas.boundary.width }; },
  44. get d() { return { x: canvas.boundary.height, y: 0 }; }
  45. },
  46. url: "http://127.0.0.1:5000/image/",
  47. width: 0,
  48. height: 0,
  49. images: [],
  50. level: 0
  51. };
  52. const mouse = {
  53. x: 0,
  54. y: 0,
  55. prevX: 0,
  56. prevY: 0
  57. };
  58. const draw = function(ctx, csize, isize, url, level, images, coords) {
  59. ctx.clearRect(0, 0, csize.width, csize.height);
  60. const seedCoord = images[0] ? {x : coords.x - coords.prevX, y: coords.y - coords.prevY} : coords;
  61. const seedImage = images[0] ? images.find((i) => isInsideSquare({x: seedCoord.x + i.x, y: seedCoord.y + i.y}, canvas.vertices, isize)) || {pos: [1,1], x: 0, y: 0} : {pos: [1,1], x: 0, y: 0};
  62. return draw_(ctx, isize, url, level, images.map((i) => {
  63. return Object.assign(i, {drawn: false});
  64. }), seedImage.pos, {x: seedCoord.x + seedImage.x, y: seedCoord.y + seedImage.y});
  65. }
  66. const draw_ = function(ctx, isize, url, level, images, pos, coords) {
  67. if (level != canvas.level || pos.some((i) => i < 1 || i > (2 ** (level + 1)))) {
  68. return images;
  69. }
  70. const id = kronecker(pos, [2 ** (level + 1), 2 ** (level + 1)]);
  71. const index = images.findIndex((i) => (url + id) == i.image.src);
  72. if (!isInsideSquare(coords, canvas.vertices, isize)) {
  73. if (index != -1) {
  74. images.splice(index);
  75. }
  76. return images;
  77. } else {
  78. const target = images[index];
  79. if (target) {
  80. if (target.drawn) {
  81. return images;
  82. } else {
  83. if (target.image.naturalWidth) {
  84. ctx.drawImage(target.image, coords.x, coords.y);
  85. } else {
  86. target.image.onload = () => ctx.drawImage(target.image, coords.x, coords.y);
  87. }
  88. images.push({
  89. image: target.image,
  90. x: coords.x,
  91. y: coords.y,
  92. drawn: true,
  93. pos: pos
  94. });
  95. images.splice(index);
  96. }
  97. } else {
  98. const image = new Image();
  99. image.src = url + id;
  100. image.onload = () => ctx.drawImage(image, coords.x, coords.y);
  101. images.push({
  102. image: image,
  103. x: coords.x,
  104. y: coords.y,
  105. drawn: true,
  106. pos: pos
  107. });
  108. }
  109. images = draw_(ctx, isize, url, level, images, [pos[0] - 1, pos[1]], { x: coords.x, y: coords.y - isize.height });
  110. images = draw_(ctx, isize, url, level, images, [pos[0], pos[1] + 1], { x: coords.x + isize.width, y: coords.y });
  111. images = draw_(ctx, isize, url, level, images, [pos[0] + 1, pos[1]], { x: coords.x, y: coords.y + isize.height });
  112. images = draw_(ctx, isize, url, level, images, [pos[0], pos[1] - 1], { x: coords.x - isize.width, y: coords.y });
  113. return images;
  114. }
  115. };
  116. const init = function(c) {
  117. const image = new Image();
  118. image.src = canvas.url + "0".repeat((canvas.level + 1) * 2)
  119. image.onload = () => {
  120. canvas.width = image.width;
  121. canvas.height = image.height;
  122. canvas.images = draw(c.getContext("2d"), canvas.boundary, canvas,
  123. canvas.url, canvas.level, canvas.images, {x: 0, y: 0});
  124. }
  125. }
  126. const main = function() {
  127. const cbox = document.getElementById("canvas-box");
  128. const c = document.getElementById("canvas");
  129. c.setAttribute("width", window.getComputedStyle(cbox).getPropertyValue("width"));
  130. c.setAttribute("height", window.getComputedStyle(cbox).getPropertyValue("height"));
  131. document.addEventListener("mousedown", (e) => {
  132. if (c === document.activeElement && e.button === 0) {
  133. mouse.prevX = e.clientX;
  134. mouse.prevY = e.clientY;
  135. }
  136. });
  137. document.addEventListener("mouseup", (e) => {
  138. if (c === document.activeElement && e.button === 0) {
  139. mouse.x = e.clientX;
  140. mouse.y = e.clientY;
  141. canvas.images = draw(c.getContext("2d"), canvas.boundary, canvas,
  142. canvas.url, canvas.level, canvas.images, mouse);
  143. }
  144. });
  145. document.addEventListener("wheel", (e) => {
  146. if (c === document.activeElement) {
  147. if (e.deltaY > 0) {
  148. canvas.level += 1;
  149. } else {
  150. if (canvas.level > 0) {
  151. canvas.level += -1;
  152. }
  153. }
  154. document.getElementById("level").innerText = canvas.level;
  155. init(c);
  156. }
  157. });
  158. init(c);
  159. };