These forums are currently read-only due to receiving more spam than actual discussion. Sorry.

It is currently Sat Dec 02, 2017 4:10 pm Advanced search

Problem in getting mouse position relative to canvas

Here you can discuss things related to HTML and the Web in general that do not fit in to other categories.

Problem in getting mouse position relative to canvas

Postby priyaa2002 » Thu Feb 17, 2011 7:00 pm

I'm having trouble in getting the mouse position w.r.t canvas. I would appreciate your insight.
http://hpportfolio.zymichost.com/Portfo ... anvas.html

HTML
Code: Select all
<canvas id="myCanvas" width="400" height="144" onmousemove="showCoordinates(event)"
   onmouseout="writeCoordinateDisplay('Mouseover here!')" >
                   <script type="text/javascript">
                     $(document).ready(function() {
                          init();
                     });
               </script>
</canvas>


JS
Code: Select all
function init() {
   canvas=document.getElementById("myCanvas");
   context=canvas.getContext("2d");
   var coordinateDisplay = "Mouseover here!";
   writeCoordinateDisplay(coordinateDisplay);
}

function showCoordinates(e) {
   var mouseX = e.clientX;
   var mouseY = e.clientY;
   var coordinateDisplay = "x=" + mouseX +
      ", y=" + mouseY;
   writeCoordinateDisplay(coordinateDisplay);
}

function writeCoordinateDisplay(coordinateDisplay) {
   clearCanvas();
   context.font="18pt Calibri";
   context.fillText(coordinateDisplay, 110, 75);
}

function clearCanvas() {
   context.clearRect(0,0,canvas.width, canvas.height);
   context.beginPath();
}

priyaa2002
<h6>
 
Posts: 3
Joined: Thu Feb 03, 2011 7:42 pm

Trouble in getting mouse position

Postby priyaa2002 » Thu Feb 17, 2011 10:49 pm

Why is it not giving the starting co-ordinates (0,0)..???
priyaa2002
<h6>
 
Posts: 3
Joined: Thu Feb 03, 2011 7:42 pm

tracking mouse position

Postby priyaa2002 » Tue Feb 22, 2011 10:43 pm

If there is no div element surrounding the canvas div then I was able to get the mouse position. http://hpportfolio.zymichost.com/Portfolio/portfolio_html5/canvas4.html
Code: Select all
<body>
   <div id="canvasDiv"></div>
      <script type="text/javascript">
            $(document).ready(function() {
                prepareCanvas(1);
            });
        </script>
</body>

Code: Select all

var canvas;
var context;
var canvasWidth = 490;
var canvasHeight = 220;

var coordinateDisplay;
var tmp = 0;

function prepareCanvas(tmp)
{
   // Create the canvas (Neccessary for IE because it doesn't know what a canvas element is)
   var canvasDiv = document.getElementById('canvasDiv');
   canvas = document.createElement('canvas');
   canvas.setAttribute('width', canvasWidth);
   canvas.setAttribute('height', canvasHeight);
   canvas.setAttribute('id', 'canvas');
   canvasDiv.appendChild(canvas);
   if(typeof G_vmlCanvasManager != 'undefined') { //since canvas element is dynamically it will not have the getContext method
   //added to the element. To get it working you need to call initElement on the G_vmlCanvasManager object.
      canvas = G_vmlCanvasManager.initElement(canvas);
   }
   context = canvas.getContext("2d");
   
   // Add mouse events
   // ----------------
   if (tmp == 1){
      tmpMouseMove();
   } else{
      $('#canvas').mousemove(function(e){
           alert("I'm working");
      });
   }

}

function tmpMouseMove(){
   $('#canvas').mousemove(function(e){
      var x = e.pageX - this.offsetLeft;
      var y = e.pageY - this.offsetTop;
      var coordinateDisplay = "x=" + x + ", y=" + y;
      writeCoordinateDisplay(coordinateDisplay);
   });
}

function writeCoordinateDisplay(coordinateDisplay) {
   clearCanvas();
   context.font="18pt Calibri";
   context.fillText(coordinateDisplay, 200, 100);
}

function clearCanvas() {
   context.clearRect(0,0,canvas.width, canvas.height);
   context.beginPath();
}


Canvas enclosed in div
Code: Select all
    <div id="wrapper">
         <div class="project" id="">
           <div class="primary">
               <span class="section_title">Tracking Mouse Position</span>

                   <div id="canvasDiv"></div>
               <script type="text/javascript">
                  $(document).ready(function() {
                      prepareCanvas(1);
                  });
        </script>

            </div> <!-- end primary-->
            <div class="secondary">
                <div class="thumb">
               </div>  <!-- end thumb-->
           </div>  <!-- end secondary-->
      </div> <!--end -->
    </div><!-- End wrapper -->


But when I try to enclose canvas inside another div element, then I was not able to get the mouse co-ordinates.
http://hpportfolio.zymichost.com/Portfolio/portfolio_html5/canvas2.html

Can anybody pls help me with this?? Thanks
priyaa2002
<h6>
 
Posts: 3
Joined: Thu Feb 03, 2011 7:42 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests