get click event of each rectangle inside canvas?

后端 未结 5 1132
说谎
说谎 2020-12-01 08:31

I dont know how to register click event on each rectangle.

here is the sample:

http://jsfiddle.net/9WWqG/1/

5条回答
  •  借酒劲吻你
    2020-12-01 08:55

    Please use below function if you want to support more than one rectangle in canvas and handle its click event..... modified logic given by Matt King.

        function collides(myRect, x, y) {
        var isCollision = false;
        for (var i = 0, len = myRect.length; i < len; i++) {
            var left = myRect[i].x, right = myRect[i].x+myRect[i].w;
            var top = myRect[i].y, bottom = myRect[i].y+myRect[i].h;
           if ((left + right) >= x
                && left <= x
                && (top +bottom) >= y
                && top <= y) {
                isCollision = json.Major[i];
            }
            }
        }
        return isCollision;
    } 
    

提交回复
热议问题