Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

drawing with mouse events

Posted on 2009-05-06
2
Medium Priority
?
887 Views
Last Modified: 2015-01-05
I have a div tag on my page with the following attributes:

<DIV onmouseup="toggleLoading(true);setEnd(event.offsetX, event.offsetY);drawPic();setData();"onmousedown="setStart(event.offsetX, event.offsetY);"
onmouseover="setIndex();setPoints(event.offsetX, event.offsetY);" id="divCanvas" >

The onmouseup function sets the end coordinates  and the onmousedown sets the start coordinates.  I want to use the onmouseover to capture all of the points inbetween.

I do that with creating two new arrays in my js file:
var Xpoints = new Array();
var YPoints = new Array();

THen in the setPoints function:

function setPoints(inpX, inpY) {

    if (CurrentTool == "DRAWPOLYLINE") {
        Xpoints[index] = inpX;
        YPoints[index] = inpY;
        index++;
    }
    else {
//do nothing
    }

THen after the mousup is called, I have something like this:
Here I want to draw each point saved in the arrays.
for (x in Xpoints)
             jg.freeform(Xpoints[x], YPoints[x], CurrentStroke, CurrentStroke);

I have two problems:

1) WIth my original div tag, I am getting an error with the mouseover event...that an object was expected.

2) When I draw the array I don't get anything...I suspect that is becuase the onmoueover is not working.  If I try to draw just one coordinate (the first or last, which I record with the mousedown and mouseup events, it draws the one point, but I want all of them drawn).

Thanks
0
Comment
Question by:wizkid2332
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 10

Expert Comment

by:bugada
ID: 24320020
You must use onmousemove event to capture each pixel while the mouse is moving. Mouseover only fires when your pointer enters the div.

A code like your is not crossbrowser but IE oriented. If this is not a problem, continue on this road, otherway try to change it like the following snippet

To get coords you must use pageX, pageY (FF/GC/Safari) or a more complex function for IE (see Mousecoords function).

This code is untested, it's just an example, may contain typos or bugs.

<script>
window.onload = function() {
   var div = document.getElementById("divCanvas");
   
   div.onmouseup = function(e) {
      e = e || window.event;
      toggleLoading(true);
      setEnd(event.offsetX, event.offsetY);
      drawPic();
      setData();
   };
   
   div.onmousedown = function(e) {
      e = e || window.event;
      var coords = mouseCoords(e);
      setStart(coords.x, coords );
   };
   
   div.onmousemove = function(e) {
      e = e || window.event;
      setIndex();
      var coords = mouseCoords(e);
      setPoints(coords.x, coords.y);
   }; 
   
   function mouseCoords(e){
      if (e.pageX || e.pageY) {
         return {x:e.pageX, y:e.pageY};
      }
      return {
         x:e.clientX + document.body.scrollLeft - document.body.clientLeft,
         y:e.clientY + document.body.scrollTop  - document.body.clientTop
      };
   };
};
 
function setPoints(inpX, inpY) {
 
    if (CurrentTool == "DRAWPOLYLINE") {
        Xpoints[index] = inpX;
        YPoints[index] = inpY;
        index++;
    } else {
//do nothing
    }
};
 
</script>
 
<div id="divcanvas"></div>

Open in new window

0
 
LVL 10

Accepted Solution

by:
bugada earned 2000 total points
ID: 24320036
sorry i've made some typos, try this code

<script>
window.onload = function() {
   var div = document.getElementById("divCanvas");
   
   div.onmouseup = function(e) {
      e = e || window.event;
      toggleLoading(true);
      var coords = mouseCoords(e);
      setEnd(coords.x, coords.y);
      drawPic();
      setData();
   };
   
   div.onmousedown = function(e) {
      e = e || window.event;
      var coords = mouseCoords(e);
      setStart(coords.x, coords.y);
   };
   
   div.onmousemove = function(e) {
      e = e || window.event;
      setIndex();
      var coords = mouseCoords(e);
      setPoints(coords.x, coords.y);
   }; 
   
   function mouseCoords(e){
      if (e.pageX || e.pageY) {
         return {x:e.pageX, y:e.pageY};
      }
      return {
         x:e.clientX + document.body.scrollLeft - document.body.clientLeft,
         y:e.clientY + document.body.scrollTop  - document.body.clientTop
      };
   };
};
</script>

Open in new window

0

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question