Skip to content

Instantly share code, notes, and snippets.

@raduGaspar
Created October 24, 2016 17:15
Show Gist options
  • Save raduGaspar/88aff5cb5bccf133747944c2e2dab03f to your computer and use it in GitHub Desktop.
Save raduGaspar/88aff5cb5bccf133747944c2e2dab03f to your computer and use it in GitHub Desktop.
import { Keyboard } from '../';
let canvasInstance;
class CanvasSingleton {
constructor() {
if(!canvasInstance) {
console.log('Canvas instance created');
let canv = document.createElement('canvas');
this.canvas = canv;
this.ctx = canv.getContext('2d');
document.body.appendChild(canv);
window.addEventListener('resize', this.resize.bind(this));
this.resize();
canvasInstance = this;
}
return canvasInstance;
}
resize() {
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
}
};
export default class Canvas extends Keyboard {
constructor() {
super();
canvasInstance = new CanvasSingleton();
this.canvas = canvasInstance.canvas;
this.ctx = canvasInstance.ctx;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment