CanvasRenderer.js 646 B

12345678910111213141516171819202122232425262728
  1. import { ScaledCanvas } from './ScaledCanvas.js';
  2. export class CanvasRenderer {
  3. constructor() {
  4. this.context = null;
  5. }
  6. init(container, scene) {
  7. this.scaledCanvas = new ScaledCanvas(container);
  8. this.scaledCanvas.init();
  9. this.context = this.scaledCanvas.getContext();
  10. this.setScene(scene);
  11. }
  12. animate() {
  13. this.scaledCanvas.clearFrame();
  14. this.scene.draw(this.scaledCanvas.getContext(), this.scaledCanvas);
  15. requestAnimationFrame(this.animate.bind(this));
  16. }
  17. setScene(scene) {
  18. this.scene = scene;
  19. }
  20. start() {
  21. this.animate();
  22. }
  23. }