svg2marker.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.svg2marker = exports.path2marker = void 0;
  4. var tslib_1 = require("tslib");
  5. /**
  6. * from: https://github.com/zqlu/svg2marker
  7. * translate svg string to G.Marker
  8. */
  9. var svg_path_parser_1 = tslib_1.__importDefault(require("svg-path-parser"));
  10. /**
  11. * Return function to register a Marker Symbol for give SVG Path
  12. *
  13. * @param svgPath SVG Path string
  14. * @param viewBoxWidth SVG view box width, default to 1024
  15. * @param viewBoxHeight SVG view box height, default to 1024
  16. */
  17. function path2marker(svgPath, viewBoxWidth, viewBoxHeight) {
  18. if (viewBoxWidth === void 0) { viewBoxWidth = 1024; }
  19. if (viewBoxHeight === void 0) { viewBoxHeight = 1014; }
  20. return function (x, y, r) {
  21. // @ts-ignore
  22. var paths = (0, svg_path_parser_1.default)(svgPath);
  23. return paths.map(function (path) {
  24. var arr = [];
  25. arr.push(path.relative === true ? path.code.toLowerCase() : path.code.toUpperCase());
  26. var pairs = [
  27. [path.x1, path.y1],
  28. [path.x2, path.y2],
  29. [path.x, path.y],
  30. ];
  31. pairs.forEach(function (pair) {
  32. var _a = tslib_1.__read(pair, 2), px = _a[0], py = _a[1];
  33. if (px !== undefined) {
  34. arr.push(path.relative === true ? (px / viewBoxWidth) * 2 * r : x - r + r * 2 * (px / viewBoxWidth));
  35. }
  36. if (py !== undefined) {
  37. arr.push(path.relative === true ? (py / viewBoxHeight) * 2 * r : y - r + r * 2 * (py / viewBoxHeight));
  38. }
  39. });
  40. return arr;
  41. });
  42. };
  43. }
  44. exports.path2marker = path2marker;
  45. /**
  46. * Return function to register a Marker symbol for give svg file
  47. *
  48. * @param icon SVG file content
  49. */
  50. function svg2marker(icon) {
  51. var pathMatch = /<path\s+d="(.*?)"/i.exec(icon);
  52. var viewBoxMatch = /viewBox="\d+\s+\d+\s+(\d+)\s+(\d+)"/i.exec(icon);
  53. if (pathMatch === null || pathMatch.length < 2) {
  54. return function () { return []; };
  55. }
  56. var width = 1024;
  57. var height = 1024;
  58. if (viewBoxMatch !== null && viewBoxMatch.length >= 3) {
  59. if (!Number.isNaN(parseInt(viewBoxMatch[1], 10))) {
  60. width = parseInt(viewBoxMatch[1], 10);
  61. }
  62. if (!Number.isNaN(parseInt(viewBoxMatch[2], 10))) {
  63. height = parseInt(viewBoxMatch[2], 10);
  64. }
  65. }
  66. return path2marker(pathMatch[1], width, height);
  67. }
  68. exports.svg2marker = svg2marker;
  69. //# sourceMappingURL=svg2marker.js.map