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