Skip to content

Instantly share code, notes, and snippets.

@eamonnmcevoy
Last active September 9, 2018 18:26
Show Gist options
  • Save eamonnmcevoy/d1be9539a3be1e7d22bb349d25b822a6 to your computer and use it in GitHub Desktop.
Save eamonnmcevoy/d1be9539a3be1e7d22bb349d25b822a6 to your computer and use it in GitHub Desktop.
class Grid {
constructor(width, height, blockarea, margin, ctx) {
...
this.generateDataSet();
}
generateDataSet() {
this.dataset = [];
for (let x = 0; x < width; x++) {
this.dataset[x] = [height];
for (let y = 0; y < height; y++) {
this.setPoint({ x, y }, 'off');
}
}
}
setPoint(point, state) {
this.dataset[point.x][point.y] = state;
}
getPoint(point) {
return this.dataset[point.x][point.y];
}
render() {
...
const point = {x,y};
const state = this.getPoint(point);
this.ctx.fillStyle = this.getFillStyle(state);
...
}
getFillStyle(state) {
switch(state) {
case 'on':
return 'green';
case 'off':
return 'lightgrey';
default:
return 'lightgrey';
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment