How do I go from drawing detached pixels on the canvas to rendering interactive entities?
My best guess is by adding event listeners to the constructors of self drawing objects, which cleverly add and remove further event listeners as necessary to any structure(s) on the screen.
I've tried the first step of this below, getting a single functioning even listener into the constructor, but it throws an exception.
As you may be able to determine yourself, I'd like this example to produce one or more entities in the canvas which can be clicked to log some kind of stat update message to the console. I can probably take it from there.
var game = document.getElementById( 'game' ),
draw = game.getContext( '2d' );
var Hero = function( ) {
this.model = { pos: { x: 50, y: 50 }, dim: { x: 4, y: 4 } };
this.draw_self = function( ) {
draw.fillRect( this.model.pos.x, this.model.pos.y, this.model.dim.x, this.model.dim.y );
};
this.update_self = function( ) {
/*...*/
}
this.draw_self( );
this.update_self( );
// this.model.addEventListener( 'click', function( e ) { /*...*/ console.log( user + ' restored health.' ); } );
};
var players = [ ];
players.push( new Hero( ) );
canvas { box-shadow: 0 0 2px 0px black; }
<canvas id="game"></canvas>