| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- "use strict";
- exports.__esModule = true;
- exports["default"] = void 0;
- var _common = require("../../util/common");
- var _base = _interopRequireDefault(require("./base"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
- function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
- function getOffsetFromAlign(alignX, alignY, width, height) {
- var result = [];
- if (alignX === 'left' && alignY === 'top') {
- result[0] = 0;
- result[1] = 0;
- } else if (alignX === 'right' && alignY === 'top') {
- result[0] = -width;
- result[1] = 0;
- } else if (alignX === 'left' && alignY === 'bottom') {
- result[0] = 0;
- result[1] = Math.floor(-height);
- } else if (alignX === 'right' && alignY === 'bottom') {
- result[0] = Math.floor(-width);
- result[1] = Math.floor(-height);
- } else if (alignX === 'right' && alignY === 'middle') {
- result[0] = Math.floor(-width);
- result[1] = Math.floor(-height / 2);
- } else if (alignX === 'left' && alignY === 'middle') {
- result[0] = 0;
- result[1] = Math.floor(-height / 2);
- } else if (alignX === 'center' && alignY === 'bottom') {
- result[0] = Math.floor(-width / 2);
- result[1] = Math.floor(-height);
- } else if (alignX === 'center' && alignY === 'top') {
- result[0] = Math.floor(-width / 2);
- result[1] = 0;
- } else {
- result[0] = Math.floor(-width / 2);
- result[1] = Math.floor(-height / 2);
- }
- return result;
- }
- function modifyCSS(DOM, CSS) {
- for (var key in CSS) {
- if (CSS.hasOwnProperty(key)) {
- DOM.style[key] = CSS[key];
- }
- }
- return DOM;
- }
- function createDom(str) {
- var container = document.createElement('div');
- str = str.replace(/(^\s*)|(\s*$)/g, '');
- container.innerHTML = '' + str;
- return container.childNodes[0];
- }
- var Html = /*#__PURE__*/function (_GuideBase) {
- _inheritsLoose(Html, _GuideBase);
- function Html() {
- return _GuideBase.apply(this, arguments) || this;
- }
- var _proto = Html.prototype;
- _proto._initDefaultCfg = function _initDefaultCfg() {
- this.type = 'html';
- /**
- * dom position
- * @type {Object | Array}
- */
- this.position = null;
- /**
- * alignment for horizontal direction,can be 'left','center','right'
- * @type {String}
- */
- this.alignX = 'center';
- /**
- * alignment for vertical direction,can be 'top', 'middle', 'bottom'
- * @type {String}
- */
- this.alignY = 'middle';
- /**
- * offset for horizontal direction
- * @type {Number}
- */
- this.offsetX = null;
- /**
- * offset for vertical direction
- * @type {Number}
- */
- this.offsetY = null;
- /**
- * the html string
- *@type {String | Function}
- */
- this.html = null;
- } // override paint
- ;
- _proto.render = function render(coord, container) {
- var self = this;
- var position = self.parsePoint(coord, self.position);
- if (!position) {
- return;
- }
- var myNode = createDom(self.html);
- myNode = modifyCSS(myNode, {
- position: 'absolute',
- top: Math.floor(position.y) + 'px',
- left: Math.floor(position.x) + 'px',
- visibility: 'hidden'
- });
- var canvasDom = container.get('canvas').get('el');
- var parentNode = canvasDom.parentNode;
- parentNode = modifyCSS(parentNode, {
- position: 'relative'
- });
- var wrapperNode = createDom('<div class="guideWapper" style="position: absolute;top: 0; left: 0;"></div>');
- parentNode.appendChild(wrapperNode);
- wrapperNode.appendChild(myNode);
- var canvasOffsetTop = canvasDom.offsetTop;
- var canvasOffsetLeft = canvasDom.offsetLeft;
- var alignX = self.alignX,
- alignY = self.alignY,
- offsetX = self.offsetX,
- offsetY = self.offsetY;
- var width = (0, _common.getWidth)(myNode);
- var height = (0, _common.getHeight)(myNode);
- var newOffset = getOffsetFromAlign(alignX, alignY, width, height);
- position.x = position.x + newOffset[0] + canvasOffsetLeft;
- position.y = position.y + newOffset[1] + canvasOffsetTop;
- if (offsetX) {
- position.x += offsetX;
- }
- if (offsetY) {
- position.y += offsetY;
- }
- modifyCSS(myNode, {
- top: Math.floor(position.y) + 'px',
- left: Math.floor(position.x) + 'px',
- visibility: 'visible'
- });
- self.element = wrapperNode;
- };
- _proto.remove = function remove() {
- var element = this.element;
- element && element.parentNode && element.parentNode.removeChild(element);
- };
- return Html;
- }(_base["default"]);
- _base["default"].Html = Html;
- var _default = Html;
- exports["default"] = _default;
|