Recent

Author Topic: position of mouse pointer  (Read 2114 times)

krolikbest

  • Sr. Member
  • ****
  • Posts: 250
position of mouse pointer
« on: May 03, 2023, 04:09:13 pm »
 Hi,
lets say I have html file with 'canvas' element and I'd like to know the mouse position over this canvas. So far the html looks like:
Code: Pascal  [Select][+][-]
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1">
  6.    <title>Project1</title>
  7.   <script src="project1.js"></script>
  8.  
  9. <style type="text/css">
  10. body{
  11. padding: 1%
  12. }
  13. #canv1 {
  14.  position : fixed;
  15.  left: 30px;
  16.  border:1px solid #000000;
  17.  box-shadow: 5px 10px 10px #888888;
  18.  top : 300px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="parag1">
  24. <p id="p1"></p>
  25. </div>
  26. <canvas id="canv1" width="400" height="300"></canvas>
  27.  
  28.   <script>
  29.   rtl.run();
  30.   </script>
  31. </body>
  32. </html>

and the pas2js code:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}
  4.  
  5. uses
  6.   JS, Classes, SysUtils, Web;
  7.  
  8. var
  9.   canv1 : TJSHTMLCanvasElement;
  10.   canv1_ctx : TJSCanvasRenderingContext2D;
  11.   cx, cy, x,y, r : Integer;
  12.   mouse1 : TJSMouseEvent;
  13.   event1 : TJSEvent;
  14.  
  15. procedure mouseMove;
  16. begin
  17.  //document.getElementById('p1').innerHTML:=inttostr(round(canv1.getBoundingClientRect.x));
  18.  //str1:=floattostr(mouse1.clientX);
  19.  document.getElementById('p1').innerHTML:= //X, Y position of the mouse
  20. end;
  21.  
  22. begin
  23.   canv1:=TJSHTMLCanvasElement(document.getElementById('canv1'));
  24.   canv1_ctx:=canv1.getContextAs2DContext('2d');
  25.   document.querySelector('#canv1').addEventListener('mousemove', @mouseMove);
  26. end.                                            
  27.  

I don't want to use WCL elements from Lazarus this time. Maybe it is simple to solve but I'm stuck.. Simply saying I don't know how to use TJSMouseEvent, or maybe TJSEvent to grab mouse position??

krolikbest

  • Sr. Member
  • ****
  • Posts: 250
Re: position of mouse pointer
« Reply #1 on: May 03, 2023, 06:16:40 pm »
Ok, it was really simple. Procedure mouseMove should be defined as mouseMove(evt : TJSMouseMove) so it looks like:
Code: Pascal  [Select][+][-]
  1. procedure mouseMove(evt : TJSMouseMove);
  2. begin
  3.  //document.getElementById('p1').innerHTML:=inttostr(round(canv1.getBoundingClientRect.x));
  4.  document.getElementById('p1').innerHTML:=evt.X;
  5. end;
  6.  

 

TinyPortal © 2005-2018