JS:
(function(win, doc) {
"use strict";
class Matrix {
constructor(){
this.canvas = new Canvas();
}
run(){
let matrixInterval;
if(typeof matrixInterval != 'undefined'){
clearInterval(matrixInterval);
}
matrixInterval = setInterval(this.canvas.draw, 33);
}
}
class Canvas {
constructor() {
this.matrix = document.getElementById('matrix');
this.width = matrix.width;
this.height = matrix.height;
this.ctx = matrix.getContext('2d');
this.positionsY = Array(300).join(0).split('');
}
initialize() {
this.ctx.fillStyle='rgba(0,0,0,.05)';
this.ctx.fillRect(0, 0, this.width, this.height);
this.ctx.fillStyle = '#0F0';
this.ctx.font = '10pt Georgia';
}
draw() {
this.initialize();
this.positionsY.map((y, index) => {
let text = Math.round(Math.random());
let x = (index * 10) + 10;
this.ctx.fillText(text, x, y);
if(y > 100 + Math.random() * 1e4){
this.positionsY[index] = 0;
}else{
this.positionsY[index] = y + 10;
}
});
}
}
function main () {
const matrix = new Matrix();
matrix.run();
}
main();
})(this, document);
HTML:
<canvas id="matrix"></canvas>
Возникает ошибка:
this.initialize is not a function at draw
Вроде вызываю метод initialize() в методе draw() правильно. Думаю ошибка где-то при создании экземляра Canvas в классе Matrix, но не понимаю в чем дело. Весь код запускаю в codepen.