I have a draw function that's drawing a colored rectangle over the whole canvas by calling the fillRect()
function of its context, then calls a method within an object that was handed the same context by its constructor, that's drawing a rectangle at this object's x and y coordinates. The draw function is looped.
I would expect to see the following:
fillRect()
overwrites the whole canvasrect()
(called as an object's method) draws a rectangle at the specified locationfillRect()
overwrites the whole canvas, including the drawn rectanglerect()
(called as an object's method) draws a rectangle at the specified location...
Sadly the fillRect()
never actually draws "above" the rectangles drawn at the object position from within those objects' methods.
The code to reproduce this unexpected behaviour is about 60 lines long, so I'll link a jsfiddle instead of including it in here. By clicking the canvas you can create new objects to be drawn. They wander down the screen (leaving a trace, that's the problem) and are removed when out of screen (yet their trace will forever be visible). The only time where their traces can't be seen is when the draw method from within the objects is not called (no objects on screen), however as soon as a new object is created, ALL previous traces re-appear.
I am using something similar to a c-style linked list (I know I shouldn't) to store the objects that are supposed to be drawn.
jsfiddle linked again, for convenience.