html.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { getHeight, getWidth } from '../../util/common';
  2. import GuideBase from './base';
  3. function getOffsetFromAlign(alignX, alignY, width, height) {
  4. var result = [];
  5. if (alignX === 'left' && alignY === 'top') {
  6. result[0] = 0;
  7. result[1] = 0;
  8. } else if (alignX === 'right' && alignY === 'top') {
  9. result[0] = -width;
  10. result[1] = 0;
  11. } else if (alignX === 'left' && alignY === 'bottom') {
  12. result[0] = 0;
  13. result[1] = Math.floor(-height);
  14. } else if (alignX === 'right' && alignY === 'bottom') {
  15. result[0] = Math.floor(-width);
  16. result[1] = Math.floor(-height);
  17. } else if (alignX === 'right' && alignY === 'middle') {
  18. result[0] = Math.floor(-width);
  19. result[1] = Math.floor(-height / 2);
  20. } else if (alignX === 'left' && alignY === 'middle') {
  21. result[0] = 0;
  22. result[1] = Math.floor(-height / 2);
  23. } else if (alignX === 'center' && alignY === 'bottom') {
  24. result[0] = Math.floor(-width / 2);
  25. result[1] = Math.floor(-height);
  26. } else if (alignX === 'center' && alignY === 'top') {
  27. result[0] = Math.floor(-width / 2);
  28. result[1] = 0;
  29. } else {
  30. result[0] = Math.floor(-width / 2);
  31. result[1] = Math.floor(-height / 2);
  32. }
  33. return result;
  34. }
  35. function modifyCSS(DOM, CSS) {
  36. for (var key in CSS) {
  37. if (CSS.hasOwnProperty(key)) {
  38. DOM.style[key] = CSS[key];
  39. }
  40. }
  41. return DOM;
  42. }
  43. function createDom(str) {
  44. var container = document.createElement('div');
  45. str = str.replace(/(^\s*)|(\s*$)/g, '');
  46. container.innerHTML = '' + str;
  47. return container.childNodes[0];
  48. }
  49. class Html extends GuideBase {
  50. _initDefaultCfg() {
  51. this.type = 'html';
  52. /**
  53. * dom position
  54. * @type {Object | Array}
  55. */
  56. this.position = null;
  57. /**
  58. * alignment for horizontal direction,can be 'left','center','right'
  59. * @type {String}
  60. */
  61. this.alignX = 'center';
  62. /**
  63. * alignment for vertical direction,can be 'top', 'middle', 'bottom'
  64. * @type {String}
  65. */
  66. this.alignY = 'middle';
  67. /**
  68. * offset for horizontal direction
  69. * @type {Number}
  70. */
  71. this.offsetX = null;
  72. /**
  73. * offset for vertical direction
  74. * @type {Number}
  75. */
  76. this.offsetY = null;
  77. /**
  78. * the html string
  79. *@type {String | Function}
  80. */
  81. this.html = null;
  82. } // override paint
  83. render(coord, container) {
  84. var self = this;
  85. var position = self.parsePoint(coord, self.position);
  86. if (!position) {
  87. return;
  88. }
  89. var myNode = createDom(self.html);
  90. myNode = modifyCSS(myNode, {
  91. position: 'absolute',
  92. top: Math.floor(position.y) + 'px',
  93. left: Math.floor(position.x) + 'px',
  94. visibility: 'hidden'
  95. });
  96. var canvasDom = container.get('canvas').get('el');
  97. var parentNode = canvasDom.parentNode;
  98. parentNode = modifyCSS(parentNode, {
  99. position: 'relative'
  100. });
  101. var wrapperNode = createDom('<div class="guideWapper" style="position: absolute;top: 0; left: 0;"></div>');
  102. parentNode.appendChild(wrapperNode);
  103. wrapperNode.appendChild(myNode);
  104. var canvasOffsetTop = canvasDom.offsetTop;
  105. var canvasOffsetLeft = canvasDom.offsetLeft;
  106. var {
  107. alignX,
  108. alignY,
  109. offsetX,
  110. offsetY
  111. } = self;
  112. var width = getWidth(myNode);
  113. var height = getHeight(myNode);
  114. var newOffset = getOffsetFromAlign(alignX, alignY, width, height);
  115. position.x = position.x + newOffset[0] + canvasOffsetLeft;
  116. position.y = position.y + newOffset[1] + canvasOffsetTop;
  117. if (offsetX) {
  118. position.x += offsetX;
  119. }
  120. if (offsetY) {
  121. position.y += offsetY;
  122. }
  123. modifyCSS(myNode, {
  124. top: Math.floor(position.y) + 'px',
  125. left: Math.floor(position.x) + 'px',
  126. visibility: 'visible'
  127. });
  128. self.element = wrapperNode;
  129. }
  130. remove() {
  131. var element = this.element;
  132. element && element.parentNode && element.parentNode.removeChild(element);
  133. }
  134. }
  135. GuideBase.Html = Html;
  136. export default Html;