Grid.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {
  2. append as svgAppend,
  3. attr as svgAttr,
  4. clear as svgClear,
  5. create as svgCreate
  6. } from 'tiny-svg';
  7. import { query as domQuery } from 'min-dom';
  8. import { SPACING, quantize } from '../GridUtil';
  9. import { getMid } from '../../../layout/LayoutUtil';
  10. var GRID_COLOR = '#ccc',
  11. LAYER_NAME = 'djs-grid';
  12. export var GRID_DIMENSIONS = {
  13. width: 100000,
  14. height: 100000
  15. };
  16. export default function Grid(canvas, eventBus) {
  17. this._canvas = canvas;
  18. var self = this;
  19. eventBus.on('diagram.init', function() {
  20. self._init();
  21. });
  22. eventBus.on('gridSnapping.toggle', function(event) {
  23. var active = event.active;
  24. self._setVisible(active);
  25. self._centerGridAroundViewbox();
  26. });
  27. eventBus.on('canvas.viewbox.changed', function(context) {
  28. var viewbox = context.viewbox;
  29. self._centerGridAroundViewbox(viewbox);
  30. });
  31. }
  32. Grid.prototype._init = function() {
  33. var defs = domQuery('defs', this._canvas._svg);
  34. if (!defs) {
  35. defs = svgCreate('defs');
  36. svgAppend(this._canvas._svg, defs);
  37. }
  38. var pattern = this.pattern = svgCreate('pattern');
  39. svgAttr(pattern, {
  40. id: 'djs-grid-pattern',
  41. width: SPACING,
  42. height: SPACING,
  43. patternUnits: 'userSpaceOnUse'
  44. });
  45. var circle = this.circle = svgCreate('circle');
  46. svgAttr(circle, {
  47. cx: 0.5,
  48. cy: 0.5,
  49. r: 0.5,
  50. fill: GRID_COLOR
  51. });
  52. svgAppend(pattern, circle);
  53. svgAppend(defs, pattern);
  54. var grid = this.grid = svgCreate('rect');
  55. svgAttr(grid, {
  56. x: -(GRID_DIMENSIONS.width / 2),
  57. y: -(GRID_DIMENSIONS.height / 2),
  58. width: GRID_DIMENSIONS.width,
  59. height: GRID_DIMENSIONS.height,
  60. fill: 'url(#djs-grid-pattern)'
  61. });
  62. };
  63. Grid.prototype._centerGridAroundViewbox = function(viewbox) {
  64. if (!viewbox) {
  65. viewbox = this._canvas.viewbox();
  66. }
  67. var mid = getMid(viewbox);
  68. svgAttr(this.grid, {
  69. x: -(GRID_DIMENSIONS.width / 2) + quantize(mid.x, SPACING),
  70. y: -(GRID_DIMENSIONS.height / 2) + quantize(mid.y, SPACING)
  71. });
  72. };
  73. Grid.prototype._isVisible = function() {
  74. return this.visible;
  75. };
  76. Grid.prototype._setVisible = function(visible) {
  77. if (visible === this.visible) {
  78. return;
  79. }
  80. this.visible = visible;
  81. var parent = this._getParent();
  82. if (visible) {
  83. svgAppend(parent, this.grid);
  84. } else {
  85. svgClear(parent);
  86. }
  87. };
  88. Grid.prototype._getParent = function() {
  89. return this._canvas.getLayer(LAYER_NAME, -2);
  90. };
  91. Grid.$inject = [
  92. 'canvas',
  93. 'eventBus'
  94. ];