Pixel.js 8.5 KB

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