I thought I fixed it but looks like not. Heres whats happening:
The canvas.mousemove
event is handled by viewport.onMouseMove.bind(viewport)
function (viewport is an instance of a class).
At the end of the onMouseMove
function it calls this.Draw()
(referring to viewport.Draw()
function).
viewport.Draw()
loops through all the items and calls Items[i].Draw(ctx)
on each of them where ctx
is a back buffer canvas context.
Now if If the item that is being drawn goes ahead and uses the ctx to draw something right there and then (in its Draw function), using this to refer to itself, everything works fine. For example
this.Draw = function(ctx) {
ctx.beginPath();
ctx.moveTo(this.x1, this.y1);
ctx.lineTo(this.x2, this.y2);
ctx.lineWidth = 1;
ctx.strokeStyle = "#000000";
ctx.stroke();
};
However, if the object is a container that has items in itself and tries to loop and draw them like this
this.Draw = function(ctx) {
for (j = 0; j < this.Items.length; j++) {
this.Items[j].Draw(ctx);
}
};
When it gets into the Items[j].Draw
, "this" loses all meaning. alert(this)
produces "object object" and I cant figure out what its referring to (it's not the viewport nor the container nor the item it needs to be). Also another weird thing - I had to change the container object loop to use j
instead of i
because otherwise it would create a perpetual loop (like the i's of the viewport.draw
and item[i].draw
were the same).