Good news is, I have done most of the code, and it works.. 90%. The problem is masking the context using the context.clip() function. It's masking some objects and not the cone. Look at the picture for an explanation
In my version, the yellow lines get cut nicely, however, the green cone protrudes outside the map!
Here is my code for the draw section of the HUD_Item:
draw: function (context) {context.fillStyle = "rgba(0, 0, 0, 0.5)";context.fillRect(this.pos.x, this.pos.y, this.rect.width, this.rect.height);context.rect(this.pos.x, this.pos.y, this.rect.width, this.rect.height);context.clip();var xOffset = this.centrePoint.x - (this.focusedEntity.pos.x / me.game.currentLevel.tilewidth);var yOffset = this.centrePoint.y - (this.focusedEntity.pos.y / me.game.currentLevel.tileheight);context.fillStyle = "#FFF000";context.fillRect(this.focusedEntity.pos.x / me.game.currentLevel.tilewidth + xOffset, this.focusedEntity.pos.y / me.game.currentLevel.tileheight + yOffset, 1, 1);for (var j = 0; j < this.entities.length; j++) {var x = this.entities[j].pos.x / me.game.currentLevel.tilewidth + xOffset;var y = this.entities[j].pos.y / me.game.currentLevel.tileheight + yOffset;context.fillStyle = "#FFF000";context.fillRect(x, y, 1, 1);if (this.entities[j].faceDirection) {var grd = context.createRadialGradient(x, y, 0, x, y, 80);grd.addColorStop(0, "rgba(152,212,23,0.6)");grd.addColorStop(1, "rgba(152,212,23,0)");context.beginPath();context.arc(x, y, 70, this.entities[j].faceDirection - 1, this.entities[j].faceDirection + 1, false);context.lineTo(x, y);context.closePath();context.fillStyle = grd;context.fill();}}context.clearRect(this.pos.x, this.pos.y, this.rect.width, this.rect.height);var collisionlayer = me.game.currentLevel.getLayerByName("collision").layerData;for (var i = 0; i < collisionlayer.length; i++) {for (var j = 0; j < collisionlayer[i].length; j++) {if (collisionlayer[i][j] != null) {context.fillStyle = "#FFF000";context.fillRect(i + xOffset, j + yOffset, 1, 1);}}}this.parent(context);},});
Thanks a bunch!