space.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { view } from "./view";
  2. import { cx_ui } from "./cx-ui.js";
  3. import { selector } from "./selector.js";
  4. import { phrase } from "./phrase.js";
  5. import { space_scene } from "./space-scene.js";
  6. export class space extends view {
  7. #manager;
  8. #canvas;
  9. #context;
  10. #scene;
  11. #on_resize;
  12. constructor(manager) {
  13. super();
  14. this.#manager = manager;
  15. }
  16. get canvas() {
  17. return this.#canvas;
  18. }
  19. get context() {
  20. return this.#context;
  21. }
  22. #init_canvas() {
  23. this.#context = this.canvas.getContext("webgl2");
  24. this.#context = this.#context || this.canvas.getContext("webgl");
  25. if (this.#context) {
  26. return;
  27. }
  28. throw new MediaError("WebGL is not supported by browser");
  29. }
  30. update_size() {
  31. this.#scene.update_size(window.innerWidth, window.innerHeight);
  32. }
  33. show() {
  34. const ui = cx_ui.div("ui");
  35. const render = cx_ui.div("render");
  36. const back = new phrase("selector.return");
  37. const return_button = cx_ui.push("return", back, () => {
  38. this.#manager.view = new selector(this.#manager);
  39. });
  40. this.#canvas = cx_ui.canvas("space-render");
  41. ui.appendChild(return_button);
  42. render.appendChild(this.canvas);
  43. try {
  44. this.#init_canvas();
  45. this.#scene = new space_scene(this.canvas, this.context);
  46. this.update_size();
  47. this.#scene.start();
  48. } catch (fail) {
  49. const error = cx_ui.div("error");
  50. const code = cx_ui.p("code", new phrase("error.webgl-support"));
  51. error.appendChild(code);
  52. render.appendChild(error);
  53. this.canvas.remove();
  54. console.error(fail);
  55. }
  56. this.#on_resize = () => { this.update_size(); };
  57. window.addEventListener("resize", this.#on_resize);
  58. return cx_ui.div("container", null, (container) => {
  59. container.appendChild(render);
  60. container.appendChild(ui);
  61. });
  62. }
  63. destroy() {
  64. this.#scene.stop();
  65. window.removeEventListener("resize", this.#on_resize);
  66. }
  67. }