Pixel.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. var canvas = {};
  2. const dotProduct = function(U, V) {
  3. return U.x * V.x + U.y * V.y;
  4. };
  5. const isInside = function(vertex) {
  6. const AB = { x: canvas.vertex.b.x - canvas.vertex.a.x, y: canvas.vertex.b.y - canvas.vertex.a.y };
  7. const AM = { x: vertex.x - canvas.vertex.a.x, y: vertex.y - canvas.vertex.a.y };
  8. const BC = { x: canvas.vertex.c.x - canvas.vertex.b.x, y: canvas.vertex.c.y - canvas.vertex.b.y };
  9. const BM = { x: vertex.x - canvas.vertex.b.x, y: vertex.y - canvas.vertex.b.y };
  10. return (0 <= dotProduct(AB, AM) && dotProduct(AB, AM) <= dotProduct(AB, AB))
  11. && (0 <= dotProduct(BC, BM) && dotProduct(BC, BM) <= dotProduct(BC, BC));
  12. };
  13. const draw = function(ctx, source, coords) {
  14. if (!coords) {
  15. ctx.clearRect(0, 0, canvas.element.width, canvas.element.height);
  16. coords = { x: 0, y: 0 };
  17. canvas.quadrants.images = canvas.quadrants.images.filter(i => {
  18. return isInside({ x: i.x, y: i.y })
  19. || isInside({ x: (i.x + i.image.width) * 2, y: i.y })
  20. || isInside({ x: (i.x + i.image.width) * 2, y: (i.y + i.image.height) * 2 })
  21. || isInside({ x: i.x, y: (i.y + i.image.height) * 2 });
  22. });
  23. canvas.quadrants.images.forEach(i => i.drawn = false);
  24. }
  25. const imgQ1 = new Image();
  26. imgQ1.src = canvas.url + source + "00";
  27. const imgQ2 = new Image();
  28. imgQ2.src = canvas.url + source + "01";
  29. const imgQ3 = new Image();
  30. imgQ3.src = canvas.url + source + "10";
  31. const imgQ4 = new Image();
  32. imgQ4.src = canvas.url + source + "11";
  33. const imgIndex = canvas.quadrants.images.findIndex(i => imgQ1.src == i.image.src);
  34. var img = canvas.quadrants.images[imgIndex] || {};
  35. if (!!imgIndex || !img.naturalWidth) {
  36. imgQ1.onload = () => {
  37. ctx.drawImage(imgQ1, coords.x, coords.y);
  38. imgQ2.onload = () => ctx.drawImage(imgQ2, coords.x + imgQ1.width, coords.y);
  39. imgQ3.onload = () => ctx.drawImage(imgQ3, coords.x, coords.y + imgQ1.height);
  40. imgQ4.onload = () => ctx.drawImage(imgQ4, coords.x + imgQ1.width, coords.y + imgQ1.height);
  41. let newImg = {
  42. drawn: true,
  43. image: imgQ1,
  44. images: [imgQ1, imgQ2, imgQ3, imgQ4],
  45. x: coords.x,
  46. y: coords.y
  47. };
  48. canvas.quadrants.images.push(newImg);
  49. Object.assign(img, newImg);
  50. };
  51. } else {
  52. const vector = { x: canvas.mouse.x - canvas.mouse.prevX, y: canvas.mouse.y - canvas.mouse.prevY };
  53. ctx.drawImage(imgQ1, img.x + vector.x, img.y + vector.y);
  54. ctx.drawImage(imgQ2, img.x + imgQ1.width + vector.x, img.y + vector.y);
  55. ctx.drawImage(imgQ3, img.x, img.y + imgQ1.height + vector.y);
  56. ctx.drawImage(imgQ4, img.x + imgQ1.width + vector.x, img.y + imgQ1.height + vector.y);
  57. canvas.quadrants.images.splice(imgIndex, 1, Object.assign(img, {
  58. drawn: true,
  59. x: img.x + vector.x,
  60. y: img.x + vector.y
  61. }));
  62. }
  63. if (parseInt(source + "00", 2) >= 2 ** (canvas.level + 1) * 2) {
  64. draw(ctx, (parseInt(source, 2) - (2 ** (canvas.level + 1) * 2)).toString(2).padStart(source.length, 0),
  65. { x: img.x, y: img.y - img.height * 2 });
  66. }
  67. if ((parseInt(source + "00", 2) + 4) % (2 ** (canvas.level + 1) * 2)) {
  68. draw(ctx, (parseInt(source, 2) + 4).toString(2).padStart(source.length, 0),
  69. { x: img.x + img.width * 2, y: img.y });
  70. }// } else {
  71. // draw(ctx, (parseInt(source, 2) + 2 ** (canvas.level + 1) * 2).toString(2).padStart(source.length, 0),
  72. // { x: img.x + img.width * 2, y: img.y });
  73. // }
  74. if (parseInt(source + "00", 2) < 4 ** (canvas.level + 1) - 2 ** (canvas.level + 1) * 2) {
  75. draw(ctx, (parseInt(source, 2) + (2 ** (canvas.level + 1) * 2)).toString(2).padStart(source.length, 0),
  76. { x: img.x, y: img.y + img.height * 2 });
  77. }
  78. if (parseInt(source + "00", 2) % (2 ** (canvas.level + 1) * 2)) {
  79. draw(ctx, (parseInt(source, 2) - 4).toString(2).padStart(source.length, 0),
  80. { x: img.x - img.width * 2, y: img.y });
  81. }// } else {
  82. // draw(ctx, (parseInt(source, 2) + 2 ** (canvas.level + 1) * 2).toString(2).padStart(source.length, 0),
  83. // { x: img.x - img.width * 2, y: img.y });
  84. // }
  85. };
  86. const mouseMove = function(e) {
  87. canvas.mouse.x = e.clientX;
  88. canvas.mouse.y = e.clientY;
  89. console.log("Dragging: " + canvas.isDragging);
  90. console.log(canvas);
  91. if (canvas.isDragging) {
  92. draw(canvas.context, "");
  93. }
  94. canvas.mouse.prevX = e.clientX;
  95. canvas.mouse.prevY = e.clientY;
  96. };
  97. const mouseDown = function(e) {
  98. if (canvas.element === document.activeElement) {
  99. canvas.isDragging = true;
  100. }
  101. };
  102. const mouseUp = function(e) {
  103. canvas.isDragging = false;
  104. };
  105. const init = function() {
  106. canvas = {
  107. url: "http://localhost:5000/image/",
  108. element: document.getElementById("canvas"),
  109. get context() { return canvas.element.getContext("2d"); },
  110. isDragging: false,
  111. mouse: {
  112. x: 0,
  113. y: 0,
  114. prevX: 0,
  115. prevY: 0
  116. },
  117. get boundary() { return canvas.element.getBoundingClientRect(); },
  118. vertex: {
  119. get a() { return { x: canvas.boundary.top + window.scrollY, y: canvas.boundary.left + window.scrollX }; },
  120. get b() { return { x: canvas.boundary.top + window.scrollY, y: canvas.boundary.right }; },
  121. get c() { return { x: canvas.boundary.bottom, y: canvas.boundary.right }; },
  122. get d() { return { x: canvas.boundary.bottom, y: canvas.boundary.left + window.scrollX }; }
  123. },
  124. level: 0,
  125. quadrants: {
  126. width: 0,
  127. height: 0,
  128. images: []
  129. /*{
  130. drawn: false,
  131. image: null,
  132. images: [],
  133. x: 0,
  134. y: 0
  135. }*/
  136. }
  137. };
  138. canvas.element.setAttribute("width", 500); // window.getComputedStyle(document.body).getPropertyValue("width"));
  139. canvas.element.setAttribute("height", 500); // window.getComputedStyle(document.body).getPropertyValue("height"));
  140. document.addEventListener("mousemove", mouseMove);
  141. document.addEventListener("mouseup", mouseUp);
  142. document.addEventListener("mousedown", mouseDown);
  143. document.getElementById("closer").addEventListener("click", () => {
  144. canvas.level++;
  145. document.getElementById("level").innerText = canvas.level;
  146. });
  147. document.getElementById("farther").addEventListener("click", () => {
  148. if (canvas.level > 0)
  149. canvas.level--;
  150. document.getElementById("level").innerText = canvas.level;
  151. });
  152. draw(canvas.context, "");
  153. };