41 lines
923 B
JavaScript
41 lines
923 B
JavaScript
|
|
export default class Sketch {
|
|
constructor(settings) {
|
|
this.lastNow = null;
|
|
|
|
['getContext', 'onResize', 'render'].map(method => this[method] = this[method].bind(this));
|
|
|
|
this.draw = settings.draw || function() {}; // () => {};
|
|
this.container = settings.element || document.body;
|
|
|
|
window.addEventListener('optimizedResize', this.onResize);
|
|
|
|
this.canvas = document.createElement('canvas');
|
|
this.canvas.width = this.container.offsetWidth;
|
|
this.canvas.height = this.container.offsetHeight;
|
|
|
|
this.context = this.canvas.getContext('2d');
|
|
|
|
this.container.appendChild(this.canvas);
|
|
|
|
requestAnimationFrame(this.render);
|
|
}
|
|
|
|
getContext() { return this.context; }
|
|
|
|
onResize(event) {
|
|
console.log('sketch - onResize', arguments);
|
|
}
|
|
|
|
render(now) {
|
|
this.context.now = now;
|
|
this.context.lastNow = this.lastNow;
|
|
|
|
this.draw(this.context);
|
|
|
|
this.lastNow = now;
|
|
requestAnimationFrame(this.render);
|
|
}
|
|
}
|
|
|