Thursday, November 29, 2012

Canvas drawn objects mouse events

I have the following code:

public void draw(Context2d context) {

context.rect(x, y, width, height);


I would like to add a mouse event for when the mouse moves over the rectangle.  What's the best way to do this?  I was able to do it if I in the mouse move event for the canvas and with an if statement I checked to see if the mouse moved within the coordinates of the rectangle.  

canvas.addMouseMoveHandler(new MouseMoveHandler() {

      public void onMouseMove(MouseMoveEvent event) {

        mouseX = event.getRelativeX(canvas.getElement());        
        mouseY = event.getRelativeY(canvas.getElement());

        if(insideRectangle(mouseX, mouseY)) {

            // fire mouse event for rectangle




However, I am worry about working with a bunch of objects and speed doing it this way.  Is there a way to add a mouseMoveHandler directly to the rectangle?



You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment