DOM.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. /**
  4. * A DOM object transformed using CSS3D to be included in the scene.
  5. *
  6. * DOM objects always stay on top or bellow (depending on the DOM parent placement) of everything else. It is not possible to layer these object with regular canvas objects.
  7. *
  8. * By default mouse events are not supported for these objects (it does not implement pointer collision checking). Use the DOM events for interaction with these types of objects.
  9. *
  10. * @class
  11. * @param {string} type Type of the DOM element (e.g. "div", "p", ...)
  12. * @extends {Object2D}
  13. */
  14. function DOM(type)
  15. {
  16. Object2D.call(this);
  17. /**
  18. * Parent element that contains this DOM object.
  19. *
  20. * The DOM parent element if not set manually is automatically set to the parent of the drawing canvas.
  21. *
  22. * @type {Element}
  23. */
  24. this.parentElement = null;
  25. /**
  26. * DOM element contained by this object.
  27. *
  28. * By default it has the pointerEvents style set to none. In order to use any DOM event with this object first you have to set the element.style.pointerEvents to "auto".
  29. *
  30. * @type {Element}
  31. */
  32. this.element = document.createElement(type || "div");
  33. this.element.style.transformStyle = "preserve-3d";
  34. this.element.style.position = "absolute";
  35. this.element.style.top = "0px";
  36. this.element.style.bottom = "0px";
  37. this.element.style.transformOrigin = "0px 0px";
  38. this.element.style.overflow = "auto";
  39. this.element.style.pointerEvents = "none";
  40. /**
  41. * Size of the DOM element, in world coordinates.
  42. *
  43. * Size is used to set the width and height of the DOM element.
  44. *
  45. * @type {Vector2}
  46. */
  47. this.size = new Vector2(100, 100);
  48. }
  49. DOM.prototype = Object.create(Object2D.prototype);
  50. DOM.prototype.constructor = DOM;
  51. DOM.prototype.type = "DOM";
  52. Object2D.register(DOM, "DOM");
  53. /**
  54. * DOM object implements onAdd() method to automatically attach the DOM object to the DOM tree.
  55. */
  56. DOM.prototype.onAdd = function()
  57. {
  58. if(this.parentElement !== null)
  59. {
  60. this.parentElement.appendChild(this.element);
  61. }
  62. };
  63. /**
  64. * DOM object implements onRemove() method to automatically remove the DOM object to the DOM tree.
  65. */
  66. DOM.prototype.onRemove = function()
  67. {
  68. if(this.parentElement !== null)
  69. {
  70. this.parentElement.removeChild(this.element);
  71. }
  72. };
  73. DOM.prototype.transform = function(context, viewport, canvas, renderer)
  74. {
  75. // Check if the DOM element parent is null
  76. if(this.parentElement === null)
  77. {
  78. this.parentElement = renderer.getDomContainer();
  79. this.parentElement.appendChild(this.element);
  80. }
  81. // CSS transformation matrix
  82. if(this.ignoreViewport)
  83. {
  84. this.element.style.transform = this.globalMatrix.cssTransform();
  85. }
  86. else
  87. {
  88. var projection = viewport.matrix.clone();
  89. projection.multiply(this.globalMatrix);
  90. this.element.style.transform = projection.cssTransform();
  91. }
  92. // Size of the element
  93. this.element.style.width = this.size.x + "px";
  94. this.element.style.height = this.size.y + "px";
  95. // Visibility
  96. this.element.style.display = this.visible ? "block" : "none";
  97. };
  98. DOM.prototype.serialize = function(recursive)
  99. {
  100. var data = Object2D.prototype.serialize.call(this, recursive);
  101. data.size = this.size.toArray();
  102. data.element = this.element.outerHTML;
  103. return data;
  104. };
  105. DOM.prototype.parse = function(data, root)
  106. {
  107. Object2D.prototype.parse.call(this, data, root);
  108. this.size.fromArray(data.size);
  109. var parser = new DOMParser();
  110. var doc = parser.parseFromString(this.element.outerHTML, 'text/html');
  111. this.element = doc.body.children[0];
  112. };
  113. export {DOM};