I have a class that I want to handle canvas
drawing things with. I have a constructor that declares properties like the canvas element and the context, and a drawing function that does the heavy lifting every set number of milliseconds, approximately.
However, none of the instance properties seem to be saved.
// Canvas stuff
class CanvasSingleton {
constructor() {
this.cvs = document.getElementById("rush_canvas");
this.c = this.cvs.getContext("2d");
this.acc = new Int8Array(64 * 36);
/* some preparation stuff that's irrelevant to the question */
this.ani = setInterval(this.aniStep, 100);
console.log(this.acc);
}
aniStep() {
if (!this.cvs) { throw new Error("Undefined cvs"); }
if (!this.c) { throw new Error("Undefined c"); }
if (!this.acc) { throw new Error("Undefined acc"); }
/* pixel manipulation stuff using the Int8Array */
}
end() {
clearTimeout(this.ani);
}
}
var r = new CanvasSingleton();
The issue is that none of CanvasSingleton.cvs
, CanvasSingleton.c
, and CanvasSingleton.acc
are defined within CanvasSingleton.aniStep()
.
I don't know what I'm declaring wrong, because I followed the examples on MDN closely. What am I doing wrong?