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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top