Pixel.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var canvas = {};
  2. const fetchImage = function(focus) {
  3. };
  4. const draw = function(ctx, source, coords = { x: 0, y: 0 }) {
  5. ctx.clearRect(0, 0, canvas.element.width, canvas.element.height);
  6. canvas.quadrants.images.forEach((i) => {
  7. draw(ctx, { x: i.image.width, y:i.image.height });
  8. });
  9. const img = new Image();
  10. img.src = source;
  11. if (!img.naturalWidth) {
  12. img.onload = function() {
  13. ctx.drawImage(img, coords.x, coords.y);
  14. canvas.quadrants.images.push({
  15. image = img,
  16. x = coords.x,
  17. y = coords.y
  18. });
  19. };
  20. } else {
  21. canvas.quadrants.images.map((i) => {
  22. const vector = { x: canvas.mouse.x - canvas.mouse.prevX, y: canvas.mouse.y - canvas.mouse.prevY };
  23. ctx.drawImage(img, i.x + vector.x, i.y + vector.y);
  24. // i.image = img;
  25. i.x += vector.x;
  26. i.y += vector.y;
  27. });
  28. }
  29. };
  30. // const dotProduct = function(U, V) {
  31. // return U.x * V.x + U.y * V.y;
  32. // };
  33. const mouseMove = function(e) {
  34. canvas.mouse.x = e.clientX;
  35. canvas.mouse.y = e.clientY;
  36. // const AB = { x: canvas.vertex.b.x - canvas.vertex.a.x, y: canvas.vertex.b.y - canvas.vertex.a.y };
  37. // const AM = { x: canvas.mouse.x - canvas.vertex.a.x, y: canvas.mouse.y - canvas.vertex.a.y };
  38. // const BC = { x: canvas.vertex.c.x - canvas.vertex.b.x, y: canvas.vertex.c.y - canvas.vertex.b.y };
  39. // const BM = { x: canvas.mouse.x - canvas.vertex.b.x, y: canvas.mouse.y - canvas.vertex.b.y };
  40. // canvas.mouse.isInside = (0 <= dotProduct(AB, AM) && dotProduct(AB, AM) <= dotProduct(AB, AB))
  41. // && (0 <= dotProduct(BC, BM) && dotProduct(BC, BM) <= dotProduct(BC, BC));
  42. console.log("Dragging: " + canvas.isDragging);
  43. // console.log("Inside: " + canvas.mouse.isInside);
  44. if (canvas.isDragging) {
  45. draw(canvas.context);
  46. }
  47. canvas.mouse.prevX = e.clientX;
  48. canvas.mouse.prevY = e.clientY;
  49. };
  50. const mouseDown = function(e) {
  51. // if (canvas.mouse.isInside) {
  52. if (canvas.element === document.activeElement) {
  53. canvas.isDragging = true;
  54. }
  55. };
  56. const mouseUp = function(e) {
  57. canvas.isDragging = false;
  58. };
  59. const init = function() {
  60. canvas = {
  61. element: document.getElementById("canvas"),
  62. get context() { return canvas.element.getContext("2d"); },
  63. isDragging: false,
  64. mouse: {
  65. // isInside: false,
  66. x: 0,
  67. y: 0,
  68. prevX: 0,
  69. prevY: 0
  70. },
  71. // get boundary() { return canvas.element.getBoundingClientRect(); },
  72. // vertex: {
  73. // get a() { return { x: canvas.boundary.top + window.scrollY, y: canvas.boundary.left + window.scrollX }; },
  74. // get b() { return { x: canvas.boundary.top + window.scrollY, y: canvas.boundary.right }; },
  75. // get c() { return { x: canvas.boundary.bottom, y: canvas.boundary.right }; },
  76. // get d() { return { x: canvas.boundary.bottom, y: canvas.boundary.left + window.scrollX }; }
  77. // }
  78. level: 0,
  79. quadrants: {
  80. width: 0,
  81. height: 0,
  82. images: []
  83. /*{
  84. image: null,
  85. x: 0,
  86. y: 0
  87. }*/
  88. }
  89. };
  90. canvas.element.setAttribute("width", 500); // window.getComputedStyle(document.body).getPropertyValue("width"));
  91. canvas.element.setAttribute("height", 500); // window.getComputedStyle(document.body).getPropertyValue("height"));
  92. document.addEventListener("mousemove", mouseMove);
  93. document.addEventListener("mouseup", mouseUp);
  94. document.addEventListener("mousedown", mouseDown);
  95. };