CanvasRenderer.js 760 B

12345678910111213141516171819202122232425262728293031323334
  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.scaledCanvas.setImageSmoothing(false)
  10. this.context = this.scaledCanvas.getContext()
  11. this.setScene(scene)
  12. }
  13. animate() {
  14. this.scaledCanvas.clearFrame()
  15. let canvas = this.getCanvas()
  16. this.scene.draw(canvas.getContext(), canvas)
  17. requestAnimationFrame(this.animate.bind(this))
  18. }
  19. setScene(scene) {
  20. this.scene = scene
  21. }
  22. start() {
  23. this.animate()
  24. }
  25. getCanvas() {
  26. return this.scaledCanvas
  27. }
  28. }