Renderer.js 826 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. /**
  3. * The renderer is resposible for drawing the structure into the canvas element.
  4. *
  5. * Its also resposible for managing the canvas state.
  6. *
  7. * @class
  8. */
  9. function Renderer(canvas)
  10. {
  11. this.canvas = canvas;
  12. this.context = canvas.getContext("2d");
  13. }
  14. /**
  15. * Render the object using the viewport into a canvas element.
  16. */
  17. Renderer.prototype.render = function(object, viewport)
  18. {
  19. var context = this.context;
  20. // Clear canvas
  21. context.setTransform(1, 0, 0, 1, 0, 0);
  22. context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  23. // Update viewport transform matrix
  24. viewport.updateMatrix();
  25. // Render into the canvas
  26. object.traverse(function(child)
  27. {
  28. viewport.matrix.setContextTransform(context);
  29. child.updateMatrix();
  30. child.matrix.tranformContext(context);
  31. child.draw(context);
  32. });
  33. };