DOM.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. /**
  4. * A DOM object transformed using CSS3D to ver included in the graph.
  5. *
  6. * DOM objects always stay on top of everything else, mouse events are not supported for these.
  7. *
  8. * Use the normal DOM events for interaction.
  9. *
  10. * @class
  11. * @param parentDOM Parent DOM element that contains the drawing canvas.
  12. * @param 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. this.parentDOM = parentDOM;
  22. /**
  23. * DOM element contained by this object.
  24. *
  25. * Bye default it has the pointerEvents style set to none.
  26. */
  27. this.element = document.createElement("div");
  28. this.element.style.transformStyle = "preserve-3d";
  29. this.element.style.position = "absolute";
  30. this.element.style.top = "0px";
  31. this.element.style.bottom = "0px";
  32. this.element.style.transformOrigin = "0px 0px";
  33. this.element.style.overflow = "auto";
  34. this.element.style.pointerEvents = "none";
  35. /**
  36. * Size of the DOM element (in world coordinates).
  37. */
  38. this.size = new Vector2(100, 100);
  39. }
  40. DOM.prototype = Object.create(Object2D.prototype);
  41. DOM.prototype.onAdd = function()
  42. {
  43. this.parentDOM.appendChild(this.element);
  44. };
  45. DOM.prototype.onRemove = function()
  46. {
  47. this.parentDOM.removeChild(this.element);
  48. };
  49. DOM.prototype.transform = function(context, viewport, canvas)
  50. {
  51. // CSS transformation matrix
  52. if(this.ignoreViewport)
  53. {
  54. this.element.style.transform = this.globalMatrix.cssTransform();
  55. }
  56. else
  57. {
  58. var projection = viewport.matrix.clone();
  59. projection.multiply(this.globalMatrix);
  60. this.element.style.transform = projection.cssTransform();
  61. }
  62. // Size of the element
  63. this.element.style.width = this.size.x + "px";
  64. this.element.style.height = this.size.y + "px";
  65. // Visibility
  66. this.element.style.display = this.visible ? "block" : "none";
  67. };
  68. export {DOM};