DOM.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. this.size = new Vector2(100, 100);
  44. }
  45. DOM.prototype = Object.create(Object2D.prototype);
  46. DOM.prototype.constructor = DOM;
  47. DOM.prototype.type = "DOM";
  48. /**
  49. * DOM object implements onAdd() method to automatically attach the DOM object to the DOM tree.
  50. */
  51. DOM.prototype.onAdd = function()
  52. {
  53. if(this.parentElement !== null)
  54. {
  55. this.parentElement.appendChild(this.element);
  56. }
  57. };
  58. /**
  59. * DOM object implements onRemove() method to automatically remove the DOM object to the DOM tree.
  60. */
  61. DOM.prototype.onRemove = function()
  62. {
  63. if(this.parentElement !== null)
  64. {
  65. this.parentElement.removeChild(this.element);
  66. }
  67. };
  68. DOM.prototype.transform = function(context, viewport, canvas)
  69. {
  70. // Check if the DOM element parent is null
  71. if(this.parentElement === null)
  72. {
  73. this.parentElement = canvas.parentElement;
  74. this.parentElement.appendChild(this.element);
  75. }
  76. // CSS transformation matrix
  77. if(this.ignoreViewport)
  78. {
  79. this.element.style.transform = this.globalMatrix.cssTransform();
  80. }
  81. else
  82. {
  83. var projection = viewport.matrix.clone();
  84. projection.multiply(this.globalMatrix);
  85. this.element.style.transform = projection.cssTransform();
  86. }
  87. // Size of the element
  88. this.element.style.width = this.size.x + "px";
  89. this.element.style.height = this.size.y + "px";
  90. // Visibility
  91. this.element.style.display = this.visible ? "block" : "none";
  92. };
  93. DOM.prototype.serialize = function(recursive)
  94. {
  95. var data = Object2D.prototype.serialize.call(this, recursive);
  96. data.size = this.size.toArray();
  97. data.element = this.element.outerHTML;
  98. return data;
  99. };
  100. DOM.prototype.parse = function(data)
  101. {
  102. Object2D.prototype.parse.call(this, data);
  103. this.size.fromArray(data.size);
  104. var parser = new DOMParser();
  105. var doc = parser.parseFromString(this.element.outerHTML, 'text/html');
  106. this.element = doc.body.children[0];
  107. };
  108. export {DOM};