Skip to content

Commit d5184cb

Browse files
committed
optimise coordinates check
prevent going through all single polygons, but first checking the bounding box
1 parent 6333157 commit d5184cb

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

examples/shapes/js/entities/entities.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ game.ShapeObject = me.Entity.extend({
3232
pointerMove: function (event) {
3333
this.hover = false;
3434

35-
// calculate the final coordinates, as the move event is global (viewport);
36-
var parentPos = this.ancestor.getBounds().pos;
37-
var x = event.gameX - this.pos.x - parentPos.x;
38-
var y = event.gameY - this.pos.y - parentPos.y;
39-
40-
// the pointer event system will use the object bounding rect, check then with with all defined shapes
41-
for (var i = this.body.shapes.length, shape; i--, (shape = this.body.shapes[i]);) {
42-
if (shape.containsPoint(x, y)) {
43-
this.hover = true;
44-
break;
35+
// move event is global (relative to the viewport)
36+
if (this.getBounds().containsPoint(event.gameX, event.gameY)) {
37+
// calculate the final coordinates
38+
var parentPos = this.ancestor.getBounds().pos;
39+
var x = event.gameX - this.pos.x - parentPos.x;
40+
var y = event.gameY - this.pos.y - parentPos.y;
41+
42+
// the pointer event system will use the object bounding rect, check then with with all defined shapes
43+
for (var i = this.body.shapes.length, shape; i--, (shape = this.body.shapes[i]);) {
44+
if (shape.containsPoint(x, y)) {
45+
this.hover = true;
46+
break;
47+
}
4548
}
4649
}
4750

0 commit comments

Comments
 (0)