Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top