In a quick test, I wasn’t able to reproduce (but keep reading because I will reproduce in a moment). I can clearly see that the log switches from false to true when the mouse enters the circle’s large stroke:
var sprite = new Sprite();
sprite.x = 60;
sprite.y = 60;
sprite.graphics.lineStyle(100, 0xff0000);
sprite.graphics.drawCircle(100, 100, 100);
addChild(sprite);
stage.addEventListener(MouseEvent.MOUSE_MOVE, event -> {
trace(sprite.hitTestPoint(stage.mouseX, stage.mouseY, true));
});
It’s similar when I simply draw a line instead, like in the code that you shared (it writes true to the log when the mouse is inside the line’s bounds):
var sprite = new Sprite();
sprite.x = 60;
sprite.y = 60;
sprite.graphics.lineStyle(50, 0x0000ff);
sprite.graphics.moveTo(20, 320);
sprite.graphics.lineTo(300, 280);
addChild(sprite);
stage.addEventListener(MouseEvent.MOUSE_MOVE, event -> {
trace(sprite.hitTestPoint(stage.mouseX, stage.mouseY, true));
});
However, I tried drawing both a circle and a line, and that’s when things break down. In the log, hitTestPoint()
is returning true only when the mouse is inside the line’s stroke, and the circle’s stroke is not detected, for some reason. That’s definitely a bug!
var sprite = new Sprite();
sprite.x = 60;
sprite.y = 60;
sprite.graphics.lineStyle(100, 0xff0000);
sprite.graphics.drawCircle(100, 100, 100);
sprite.graphics.lineStyle(50, 0x0000ff);
sprite.graphics.moveTo(20, 320);
sprite.graphics.lineTo(300, 280);
addChild(sprite);
stage.addEventListener(MouseEvent.MOUSE_MOVE, event -> {
trace(sprite.hitTestPoint(stage.mouseX, stage.mouseY, true));
});
We use the isPointInStroke()
and isPointInFill()
methods from the CanvasRenderingContext2D
in JS (and similar methods from Cairo for native targets). Those should be very well tested in all browsers (and Cairo), so it’s got to be a bug in OpenFL’s usage of them somehow. I guess it may have been tested only with simpler drawings, and not necessarily more complex ones with multiple fills or strokes in the same Graphics
object. Someone will need to dive in and work out why it’s ignoring some of the strokes and/or fills.