Pixel.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. };
  10. } else {
  11. ctx.drawImage(img, coords.x, coords.y);
  12. }
  13. };
  14. // const dotProduct = function(U, V) {
  15. // return U.x * V.x + U.y * V.y;
  16. // };
  17. const mouseMove = function(e) {
  18. canvas.mouse.x = e.clientX;
  19. canvas.mouse.y = e.clientY;
  20. // const AB = { x: canvas.vertex.b.x - canvas.vertex.a.x, y: canvas.vertex.b.y - canvas.vertex.a.y };
  21. // const AM = { x: canvas.mouse.x - canvas.vertex.a.x, y: canvas.mouse.y - canvas.vertex.a.y };
  22. // const BC = { x: canvas.vertex.c.x - canvas.vertex.b.x, y: canvas.vertex.c.y - canvas.vertex.b.y };
  23. // const BM = { x: canvas.mouse.x - canvas.vertex.b.x, y: canvas.mouse.y - canvas.vertex.b.y };
  24. // canvas.mouse.isInside = (0 <= dotProduct(AB, AM) && dotProduct(AB, AM) <= dotProduct(AB, AB))
  25. // && (0 <= dotProduct(BC, BM) && dotProduct(BC, BM) <= dotProduct(BC, BC));
  26. console.log("Dragging: " + canvas.isDragging);
  27. // console.log("Inside: " + canvas.mouse.isInside);
  28. if (canvas.isDragging) {
  29. draw(canvas.context, "https://snippets.cdn.mozilla.net/media/icons/56efef37-8e21-49df-9699-df9ae2c8a088.png", canvas.mouse);
  30. }
  31. };
  32. const mouseDown = function(e) {
  33. // if (canvas.mouse.isInside) {
  34. if (canvas.element === document.activeElement) {
  35. canvas.isDragging = true;
  36. }
  37. };
  38. const mouseUp = function(e) {
  39. canvas.isDragging = false;
  40. };
  41. const init = function() {
  42. canvas = {
  43. element: document.getElementById("canvas"),
  44. get context() { return canvas.element.getContext("2d"); },
  45. isDragging: false,
  46. mouse: {
  47. // isInside: false,
  48. x: 0,
  49. y: 0
  50. },
  51. // get boundary() { return canvas.element.getBoundingClientRect(); },
  52. // vertex: {
  53. // get a() { return { x: canvas.boundary.top + window.scrollY, y: canvas.boundary.left + window.scrollX }; },
  54. // get b() { return { x: canvas.boundary.top + window.scrollY, y: canvas.boundary.right }; },
  55. // get c() { return { x: canvas.boundary.bottom, y: canvas.boundary.right }; },
  56. // get d() { return { x: canvas.boundary.bottom, y: canvas.boundary.left + window.scrollX }; }
  57. // }
  58. fragments: []
  59. };
  60. canvas.element.setAttribute("width", 500); // window.getComputedStyle(document.body).getPropertyValue("width"));
  61. canvas.element.setAttribute("height", 500); // window.getComputedStyle(document.body).getPropertyValue("height"));
  62. document.addEventListener("mousemove", mouseMove);
  63. document.addEventListener("mouseup", mouseUp);
  64. document.addEventListener("mousedown", mouseDown);
  65. };