This answer is deprecated, see accepted answer above
Create a simple Point class:
class Point { final int x; final int y; Point(this.x, this.y); }
Import dart: html library:
import 'dart:html';
First step: get a bounding click for the canvas:
Point clientBoundingRect; Future<html.ElementRect> futureRect = ctx.canvas.rect; futureRect.then((html.ElementRect rect) { clientBoundingRect = new Point(rect.bounding.left, rect.bounding.top); });
Then determine the algorithm for shifting from window to canvas.
Point getXandY(e) { num x = e.clientX - clientBoundingRect.x; num y = e.clientY - clientBoundingRect.y; return new Point(x, y); }
Then attach the click event handler to the canvas:
ctx.canvas.on.click.add((e) { click = getXandY(e); });
source share