0

If I draw a shape on a GWT canvas (rectangle, circle, whatever), how can I add a EventListener like MouseClick, MouseOver etc to that drawing?

   Canvas canvas = Canvas.createIfSupported();
   Context2d context = canvas.getContext2d();

   context.beginPath();
   context.moveTo(..;
   context.lineTo(..);
   //...
   context.stroke();       
   context.fill(); 

How can I detect clicks only on this drawing?

Jarrod
  • 9,349
  • 5
  • 58
  • 73
membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

2

Canvas provides raster graphics and does not know anything about your figures. So you have two options:

  1. Add event listener to the whole canvas and use some function to determine if (x; y) event point belongs to your figure.

  2. Use SVG instead. With SVG you can create vector figures and add listeners to them.

Andrey Minogin
  • 4,521
  • 6
  • 38
  • 60