Pixel.js 7.8 KB

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