DOM.js 1.8 KB

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