Pixel.js 3.7 KB

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