Pixel.js 8.1 KB

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