Advertisement
sourav8256

Untitled

Aug 3rd, 2023 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1.  
  2.  
  3. >>>>>>>>>>>>>>>>>>>>>>>>>>>> Use this code as it is in a new file <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  4.  
  5. // Variables to store mouse coordinates
  6. let isDrawing = false;
  7. let startX, startY, endX, endY;
  8.  
  9.  
  10. // Event listener for mouse down
  11. canvas.addEventListener('mousedown', (e) => {
  12. startX = e.clientX - canvas.offsetLeft;
  13. startY = e.clientY - canvas.offsetTop;
  14. isDrawing = true;
  15. });
  16.  
  17. // Event listener for mouse move
  18. canvas.addEventListener('mousemove', (e) => {
  19. if (!isDrawing) return;
  20. endX = e.clientX - canvas.offsetLeft;
  21. endY = e.clientY - canvas.offsetTop;
  22.  
  23. // context.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
  24. clearCanvas();
  25. drawLineH(startX, startY, endX,endY); // Draw the line
  26. drawLineV(startX, startY, endX,endY);
  27. });
  28.  
  29. // Event listener for mouse up
  30. canvas.addEventListener('mouseup', () => {
  31. isDrawing = false;
  32. });
  33.  
  34. // Function to draw a line between two points (restricted to horizontal and vertical directions)
  35. function drawLineH(x1, y1, x2,y2) {
  36. context.beginPath();
  37. context.moveTo(x1, y2);
  38.  
  39. context.lineTo(x2, y2);
  40.  
  41. context.stroke();
  42. }
  43.  
  44. function drawLineV(x1, y1,x2,y2) {
  45. context.beginPath();
  46. context.moveTo(x1, y1);
  47.  
  48. context.lineTo(x1, y2);
  49.  
  50. context.stroke();
  51. }
  52.  
  53.  
  54. ========================================================================================================================
  55.  
  56. <!DOCTYPE html>
  57. <html>
  58. <title>Online HTML Editor</title>
  59. <head><!DOCTYPE html>
  60. <html>
  61. <head>
  62. <title>Mouse Drag Line Drawing</title>
  63. </head>
  64. <body>
  65. <canvas id="myCanvas" width="500" height="500" style="border: 1px solid black;"></canvas>
  66.  
  67. <script>
  68. // Variables to store mouse coordinates
  69. let isDrawing = false;
  70. let startX, startY, endX, endY;
  71.  
  72. const canvas = document.getElementById('myCanvas');
  73. const ctx = canvas.getContext('2d');
  74.  
  75. // Event listener for mouse down
  76. canvas.addEventListener('mousedown', (e) => {
  77. startX = e.clientX - canvas.offsetLeft;
  78. startY = e.clientY - canvas.offsetTop;
  79. isDrawing = true;
  80. });
  81.  
  82. // Event listener for mouse move
  83. canvas.addEventListener('mousemove', (e) => {
  84. if (!isDrawing) return;
  85. endX = e.clientX - canvas.offsetLeft;
  86. endY = e.clientY - canvas.offsetTop;
  87.  
  88. ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
  89. drawLineH(startX, startY, endX,endY); // Draw the line
  90. drawLineV(startX, startY, endX,endY);
  91. });
  92.  
  93. // Event listener for mouse up
  94. canvas.addEventListener('mouseup', () => {
  95. isDrawing = false;
  96. });
  97.  
  98. // Function to draw a line between two points (restricted to horizontal and vertical directions)
  99. function drawLineH(x1, y1, x2,y2) {
  100. ctx.beginPath();
  101. ctx.moveTo(x1, y2);
  102.  
  103. ctx.lineTo(x2, y2);
  104.  
  105. ctx.stroke();
  106. }
  107.  
  108. function drawLineV(x1, y1,x2,y2) {
  109. ctx.beginPath();
  110. ctx.moveTo(x1, y1);
  111.  
  112. ctx.lineTo(x1, y2);
  113.  
  114. ctx.stroke();
  115. }
  116. </script>
  117. </body>
  118. </html>
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement