UNPKG

@thewtex/vtk.js-esm

Version:

Visualization Toolkit for the Web

129 lines (103 loc) 5.3 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import macro from '../../macro.js'; import vtkSVGRepresentation from './SVGRepresentation.js'; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } var createSvgElement = vtkSVGRepresentation.createSvgElement; // ---------------------------------------------------------------------------- // vtkSVGLandmarkRepresentation // ---------------------------------------------------------------------------- function vtkSVGLandmarkRepresentation(publicAPI, model) { // Set our className model.classHierarchy.push('vtkSVGLandmarkRepresentation'); publicAPI.render = function () { var list = publicAPI.getRepresentationStates(); var coords = list.map(function (state) { return state.getOrigin(); }); var texts = list.map(function (state, index) { return state.getText ? state.getText() : "L".concat(index); }); return publicAPI.worldPointsToPixelSpace(coords).then(function (pixelSpace) { var points2d = pixelSpace.coords; var winHeight = pixelSpace.windowSize[1]; var root = createSvgElement('g'); var _loop = function _loop(i) { var xy = points2d[i]; if (Number.isNaN(xy[0]) || Number.isNaN(xy[1])) { return "continue"; // eslint-disable-line } var x = xy[0]; var y = winHeight - xy[1]; if (model.showCircle === true) { var circle = publicAPI.createListenableSvgElement('circle', i); Object.keys(model.circleProps || {}).forEach(function (prop) { return circle.setAttribute(prop, model.circleProps[prop]); }); circle.setAttribute('cx', x); circle.setAttribute('cy', y); root.appendChild(circle); } if (!texts[i]) { texts[i] = ''; } var splitText = texts[i].split('\n'); var newlineOffset = model.fontProperties != null && model.fontProperties.fontSize ? model.fontProperties.fontSize : 15; splitText.forEach(function (subText, j) { var text = publicAPI.createListenableSvgElement('text', i); Object.keys(model.textProps || {}).forEach(function (prop) { var propValue = model.textProps[prop]; if (model.offsetText === true && prop === 'dy') { propValue = model.textProps.dy + newlineOffset * j; } text.setAttribute(prop, propValue); }); text.setAttribute('x', x); text.setAttribute('y', y); if (model.fontProperties != null) { text.setAttribute('font-size', model.fontProperties.fontSize); text.setAttribute('font-family', model.fontProperties.fontFamily); text.setAttribute('font-weight', model.fontProperties.fontStyle); text.setAttribute('fill', model.fontProperties.fontColor); } text.textContent = subText; root.appendChild(text); }); }; for (var i = 0; i < points2d.length; i++) { var _ret = _loop(i); if (_ret === "continue") continue; } return root; }); }; } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- function defaultValues(initialValues) { return _objectSpread({ circleProps: { r: 5, stroke: 'red', fill: 'red' }, textProps: { fill: 'white', dx: 12, dy: -12 } }, initialValues); } // ---------------------------------------------------------------------------- function extend(publicAPI, model) { var initialValues = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; Object.assign(model, defaultValues(initialValues)); vtkSVGRepresentation.extend(publicAPI, model, initialValues); macro.setGet(publicAPI, model, ['circleProps', 'fontProperties', 'name', 'textProps']); // Object specific methods vtkSVGLandmarkRepresentation(publicAPI, model); } // ---------------------------------------------------------------------------- var newInstance = macro.newInstance(extend, 'vtkSVGLandmarkRepresentation'); // ---------------------------------------------------------------------------- var vtkSVGLandmarkRepresentation$1 = { extend: extend, newInstance: newInstance }; export default vtkSVGLandmarkRepresentation$1; export { extend, newInstance };