DOM.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 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 {Element} parentDOM Parent DOM element that contains the drawing canvas.
  12. * @param {string} type Type of the DOM element (e.g. "div", "p", ...)
  13. * @extends {Object2D}
  14. */
  15. function DOM(parentDOM, type)
  16. {
  17. Object2D.call(this);
  18. /**
  19. * Parent element that contains this DOM div.
  20. *
  21. * @type {Element}
  22. */
  23. this.parentDOM = parentDOM;
  24. /**
  25. * DOM element contained by this object.
  26. *
  27. * 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".
  28. *
  29. * @type {Element}
  30. */
  31. this.element = document.createElement(type || "div");
  32. this.element.style.transformStyle = "preserve-3d";
  33. this.element.style.position = "absolute";
  34. this.element.style.top = "0px";
  35. this.element.style.bottom = "0px";
  36. this.element.style.transformOrigin = "0px 0px";
  37. this.element.style.overflow = "auto";
  38. this.element.style.pointerEvents = "none";
  39. /**
  40. * Size of the DOM element (in world coordinates).
  41. */
  42. this.size = new Vector2(100, 100);
  43. }
  44. DOM.prototype = Object.create(Object2D.prototype);
  45. /**
  46. * DOM object implements onAdd() method to automatically attach the DOM object to the DOM tree.
  47. */
  48. DOM.prototype.onAdd = function()
  49. {
  50. this.parentDOM.appendChild(this.element);
  51. };
  52. /**
  53. * DOM object implements onAdd() method to automatically remove the DOM object to the DOM tree.
  54. */
  55. DOM.prototype.onRemove = function()
  56. {
  57. this.parentDOM.removeChild(this.element);
  58. };
  59. DOM.prototype.transform = function(context, viewport, canvas)
  60. {
  61. // CSS transformation matrix
  62. if(this.ignoreViewport)
  63. {
  64. this.element.style.transform = this.globalMatrix.cssTransform();
  65. }
  66. else
  67. {
  68. var projection = viewport.matrix.clone();
  69. projection.multiply(this.globalMatrix);
  70. this.element.style.transform = projection.cssTransform();
  71. }
  72. // Size of the element
  73. this.element.style.width = this.size.x + "px";
  74. this.element.style.height = this.size.y + "px";
  75. // Visibility
  76. this.element.style.display = this.visible ? "block" : "none";
  77. };
  78. export {DOM};