Source: extern/container.js

  1. import register from "../util/register.js";
  2. import m_cont_fact from "../intern/container.js";
  3. import m_print_fact from "../intern/print.js";
  4. /**
  5. * Provides access to the 3D canvas element and its container.
  6. * @module container
  7. */
  8. function Container(ns, exports) {
  9. var m_cont = m_cont_fact(ns);
  10. var m_print = m_print_fact(ns);
  11. /**
  12. * Returns the 3D canvas element.
  13. * @method module:container.get_canvas
  14. * @returns {HTMLElement} Canvas element
  15. */
  16. exports.get_canvas = m_cont.get_canvas;
  17. /**
  18. * Returns the HUD element.
  19. * @method module:container.get_canvas_hud
  20. * @returns {HTMLElement} Canvas hud element
  21. */
  22. exports.get_canvas_hud = m_cont.get_canvas_hud;
  23. /**
  24. * Returns the HTML element which contains the 3D canvas.
  25. * @method module:container.get_container
  26. * @returns {HTMLElement} Canvas container element
  27. */
  28. exports.get_container = m_cont.get_container;
  29. /**
  30. * Inserts the DOM element to the container.
  31. * @method module:container.insert_to_container
  32. * @param {HTMLElement} elem Inserted DOM element.
  33. * @param {string} stack_order Inserted DOM element stack order (one of "FIRST",
  34. * "JUST_BEFORE_CANVAS", "JUST_AFTER_CANVAS", "LAST").
  35. */
  36. exports.insert_to_container = function(elem, stack_order) {
  37. if (arguments.length != 2) {
  38. m_print.error("insert_to_container(): two arguments required");
  39. return;
  40. }
  41. if (!elem || !stack_order)
  42. return;
  43. m_cont.insert_to_container(elem, stack_order);
  44. }
  45. /**
  46. * Set left/top offsets (relative to browser window) for the canvas.
  47. * Can be useful in case of scrolling/DOM-manipulations, when the canvas
  48. * position has been changed.
  49. * @method module:container.set_canvas_offsets
  50. * @param {number} left Left offset for the container
  51. * @param {number} top Top offset for the container
  52. * @deprecated Not needed anymore.
  53. */
  54. exports.set_canvas_offsets = function(left, top) {
  55. m_print.error_once("container.set_canvas_offsets() deprecated. " +
  56. "Not needed anymore. Use the container.client_to_canvas_coords method.");
  57. return m_cont.set_canvas_offsets(left, top);
  58. }
  59. /**
  60. * Update canvas left/top offsets (relative to browser window).
  61. * Can be useful in case of scrolling/DOM-manipulations, when the canvas
  62. * position has been changed.
  63. * @method module:container.update_canvas_offsets
  64. * @deprecated Not needed anymore.
  65. */
  66. exports.update_canvas_offsets = function() {
  67. m_print.error_once("container.update_canvas_offsets() deprecated. " +
  68. "Not needed anymore. Use the container.client_to_canvas_coords method.");
  69. m_cont.update_canvas_offsets();
  70. }
  71. /**
  72. * Convert client(e.clientX/e.clientY) CSS coordinates to CSS coordinates
  73. * relative to the Canvas.
  74. * @method module:container.client_to_canvas_coords
  75. * @param {number} x X client coordinate.
  76. * @param {number} y Y client coordinate.
  77. * @param {Vec2} [dest=Float32Array(2)] Destination vector.
  78. * @returns {Vec2} CSS coordinates relative to the Canvas.
  79. */
  80. exports.client_to_canvas_coords = function(x, y, dest) {
  81. if (!dest)
  82. dest = new Float32Array(2);
  83. return m_cont.client_to_canvas_coords(x, y, dest);
  84. }
  85. /**
  86. * Convert client(e.clientX/e.clientY) CSS coordinates to CSS coordinates
  87. * relative to the HTML element.
  88. * @method module:container.client_to_element_coords
  89. * @param {number} x X client coordinate.
  90. * @param {number} y Y client coordinate.
  91. * @param {HTMLElement} element HTML element.
  92. * @param {Vec2} [dest=Float32Array(2)] Destination vector.
  93. * @returns {Vec2} CSS coordinates relative to the Canvas.
  94. */
  95. exports.client_to_element_coords = function(x, y, element, dest) {
  96. if (!dest)
  97. dest = new Float32Array(2);
  98. return m_cont.client_to_element_coords(x, y, element, dest);
  99. }
  100. /**
  101. * Get CSS coordinates from the given MouseEvent or TouchEvent transformed into
  102. * the space of its target element.
  103. * @param {MouseEvent|TouchEvent} event An event to get values from.
  104. * @param {boolean} [use_target_touches=false] For TouchEvent use only those
  105. * touches that were started on the event target element (the targetTouches
  106. * property).
  107. * @param {Vec2} [dest=Float32Array(2)] Destination vector.
  108. * @returns {Vec2} CSS coordinates relative to the Canvas.
  109. * @example
  110. * var m_cont = require("container");
  111. * var m_input = require("input");
  112. * var _vec2_tmp = new Float32Array(2);
  113. *
  114. * var canvas = m_cont.get_canvas();
  115. * m_input.add_click_listener(canvas, function(event) {
  116. * var coords = m_cont.get_coords_target_space(event, false, _vec2_tmp);
  117. * });
  118. */
  119. exports.get_coords_target_space = function(event, use_target_touches, dest) {
  120. if (!dest)
  121. dest = new Float32Array(2);
  122. return m_cont.get_coords_target_space(event, use_target_touches, dest);
  123. }
  124. /**
  125. * Update canvas offsets on the next request.
  126. * @method module:container.force_offsets_updating
  127. * @deprecated Not needed anymore.
  128. */
  129. exports.force_offsets_updating = function() {
  130. m_print.error_once("container.force_offsets_updating() deprecated. " +
  131. "Not needed anymore. Use the container.client_to_canvas_coords method.");
  132. m_cont.force_offsets_updating();
  133. }
  134. /**
  135. * Resize the rendering canvas.
  136. * @method module:container.resize
  137. * @param {number} width New canvas width
  138. * @param {number} height New canvas height
  139. * @param {boolean} [update_canvas_css=true] Change canvas CSS width/height
  140. */
  141. exports.resize = function(width, height, update_canvas_css) {
  142. m_cont.resize(width, height, update_canvas_css);
  143. }
  144. /**
  145. * Fit canvas elements to match the size of container element.
  146. * @method module:container.resize_to_container
  147. * @param {boolean} [force=false] Resize canvas element even in case of
  148. * matching of canvas and container size.
  149. */
  150. exports.resize_to_container = function(force) {
  151. force = force || false;
  152. m_cont.resize_to_container(force);
  153. }
  154. }
  155. var container_factory = register("container", Container);
  156. export default container_factory;