Home Reference Source

cables_dev/cables_ui/src/ui/gldraw/glcanvas.js

  1. import { Op, Patch, utils } from "cables";
  2. import GlPatch from "../glpatch/glpatch.js";
  3. import GlPatchAPI from "../glpatch/patchapi.js";
  4. import { gui } from "../gui.js";
  5. export default class GlCanvas
  6. {
  7. static ZPOSDIV = 10000;
  8. _firstTime = true;
  9. _inited = false;
  10. _lastTime = 0;
  11. _mouseX = 0;
  12. _mouseY = 0;
  13. _moveover = true;
  14. clear = true;
  15. height = 0;
  16. loaded = false;
  17. width = 0;
  18. /**
  19. * @param {Patch} _patch
  20. * @param {HTMLElement} parentEle
  21. */
  22. constructor(_patch, parentEle)
  23. {
  24. document.body.style["touch-action"] = "none";
  25. this.canvas = document.createElement("canvas");
  26. this.canvas.id = "glGuiCanvas-" + utils.uuid();
  27. this.canvas.style.border = "0px solid white";
  28. this.canvas.style.outline = "0";
  29. this.canvas.style.position = "absolute";
  30. this.canvas.setAttribute("tabindex", "0");
  31. this._parentEle = parentEle;
  32. if (parentEle)parentEle.appendChild(this.canvas);
  33. else document.body.appendChild(this.canvas);
  34. this.patch = new Patch({
  35. "glCanvasId": this.canvas.id,
  36. "glCanvasResizeToParent": false,
  37. "glCanvasResizeToWindow": false,
  38. "canvas": { "alpha": true, "premultipliedAlpha": true, "antialias": true }
  39. });
  40. this.cgl = this.patch.cgl;
  41. if (!this.cgl)
  42. {
  43. console.error("no cgl in glcanvas constructor");
  44. }
  45. this.cgl.pixelDensity = window.devicePixelRatio || 1;
  46. this.cgl.updateSize();
  47. gui.on("uiIdleStart", () =>
  48. {
  49. this.patch.pause();
  50. });
  51. gui.on("uiIdleEnd", () =>
  52. {
  53. this.patch.resume();
  54. });
  55. this.canvas.addEventListener("touchmove",
  56. (e) =>
  57. {
  58. if (e.touches.length > 1) e.preventDefault();
  59. });
  60. this.canvas.addEventListener("pointermove", (e) =>
  61. {
  62. this.activityHigh();
  63. this._mouseX = e.offsetX;
  64. this._mouseY = e.offsetY;
  65. }, { "passive": true });
  66. this.canvas.addEventListener("pointerdown", (_e) =>
  67. {
  68. this.activityHigh();
  69. }, { "passive": true });
  70. this.canvas.addEventListener("pointerup", (_e) =>
  71. {
  72. this.activityHigh();
  73. }, { "passive": true });
  74. this.canvas.addEventListener("pointerleave", (_e) =>
  75. {
  76. this.activityMedium();
  77. }, { "passive": true });
  78. this.canvas.addEventListener("pointerenter", (_e) =>
  79. {
  80. this.activityHigh();
  81. }, { "passive": true });
  82. }
  83. get element()
  84. {
  85. return this.canvas;
  86. }
  87. /**
  88. * @param {number} w
  89. * @param {number} h
  90. */
  91. setSize(w, h)
  92. {
  93. this.width = w;
  94. this.height = h;
  95. // this.canvas.style.width = this.width + "px";
  96. // this.canvas.style.height = this.height + "px";
  97. // this.canvas.width = this.width * window.devicePixelRatio;
  98. // this.canvas.height = this.height * window.devicePixelRatio;
  99. this.cgl.pixelDensity = window.devicePixelRatio;
  100. if (this.patch.isPlaying()) this.cgl.setSize(this.width, this.height);
  101. }
  102. setSizeCss(w, h)
  103. {
  104. this.width = w;
  105. this.height = h;
  106. // this.canvas.style.width = this.width + "px";
  107. // this.canvas.style.height = this.height + "px";
  108. // this.canvas.width = this.width * window.devicePixelRatio;
  109. // this.canvas.height = this.height * window.devicePixelRatio;
  110. this.cgl.pixelDensity = window.devicePixelRatio;
  111. if (this.patch.isPlaying()) this.cgl.setSize(this.width / this.cgl.pixelDensity, this.height / this.cgl.pixelDensity);
  112. }
  113. dispose()
  114. {
  115. this.disposed = true;
  116. this.patch.pause();
  117. this.patch.dispose();
  118. this.canvas.remove();
  119. }
  120. pause()
  121. {
  122. this.patch.pause();
  123. }
  124. resume()
  125. {
  126. this.patch.resume();
  127. }
  128. activityIdle()
  129. {
  130. this._targetFps = 10;
  131. }
  132. activityHigh()
  133. {
  134. this._targetFps = 0;
  135. clearTimeout(this._activityTimeout);
  136. this._activityTimeout = setTimeout(() => { this.activityMedium(); }, 40000);
  137. }
  138. activityMedium()
  139. {
  140. this._targetFps = 30;
  141. clearTimeout(this._activityTimeout);
  142. this._activityTimeout = setTimeout(() => { this.activityIdle(); }, 30000);
  143. }
  144. }