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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 1× 1× 33× 33× 1× 1× 40× 40× 40× 40× 40× 52× 52× 52× 104× 104× 104× 50× 52× 25× 40× 40× 40× 80× 80× 50× 1× 50× 50× 50× 50× 50× 50× 175× 122× 53× 28× 25× 1× 25× 1× 83× 83× 83× 83× 1× 83× 25× 83× 1× 47× 92× | import { bind, debounce, forEach, isNumber, isObject } from 'min-dash'; import { isSnapped, setSnapped } from './SnapUtil'; import { append as svgAppend, attr as svgAttr, classes as svgClasses, create as svgCreate } from 'tiny-svg'; var SNAP_TOLERANCE = 7; export var SNAP_LINE_HIDE_DELAY = 1000; /** * Generic snapping feature. * * @param {EventBus} eventBus * @param {Canvas} canvas */ export default function Snapping(canvas) { this._canvas = canvas; // delay hide by 1000 seconds since last snap this._asyncHide = debounce(bind(this.hide, this), SNAP_LINE_HIDE_DELAY); } Snapping.$inject = [ 'canvas' ]; /** * Snap an event to given snap points. * * @param {Event} event * @param {SnapPoints} snapPoints */ Snapping.prototype.snap = function(event, snapPoints) { var context = event.context, snapContext = context.snapContext, snapLocations = snapContext.getSnapLocations(); var snapping = { x: isSnapped(event, 'x'), y: isSnapped(event, 'y') }; forEach(snapLocations, function(location) { var snapOrigin = snapContext.getSnapOrigin(location); var snapCurrent = { x: event.x + snapOrigin.x, y: event.y + snapOrigin.y }; // snap both axis if not snapped already forEach([ 'x', 'y' ], function(axis) { var locationSnapping; Eif (!snapping[axis]) { locationSnapping = snapPoints.snap(snapCurrent, location, axis, SNAP_TOLERANCE); if (locationSnapping !== undefined) { snapping[axis] = { value: locationSnapping, originValue: locationSnapping - snapOrigin[axis] }; } } }); // no need to continue snapping if (snapping.x && snapping.y) { return false; } }); // show snap lines this.showSnapLine('vertical', snapping.x && snapping.x.value); this.showSnapLine('horizontal', snapping.y && snapping.y.value); // snap event forEach([ 'x', 'y' ], function(axis) { var axisSnapping = snapping[axis]; if (isObject(axisSnapping)) { setSnapped(event, axis, axisSnapping.originValue); } }); }; Snapping.prototype._createLine = function(orientation) { var root = this._canvas.getLayer('snap'); var line = svgCreate('path'); svgAttr(line, { d: 'M0,0 L0,0' }); svgClasses(line).add('djs-snap-line'); svgAppend(root, line); return { update: function(position) { if (!isNumber(position)) { svgAttr(line, { display: 'none' }); } else { if (orientation === 'horizontal') { svgAttr(line, { d: 'M-100000,' + position + ' L+100000,' + position, display: '' }); } else { svgAttr(line, { d: 'M ' + position + ',-100000 L ' + position + ', +100000', display: '' }); } } } }; }; Snapping.prototype._createSnapLines = function() { this._snapLines = { horizontal: this._createLine('horizontal'), vertical: this._createLine('vertical') }; }; Snapping.prototype.showSnapLine = function(orientation, position) { var line = this.getSnapLine(orientation); Eif (line) { line.update(position); } this._asyncHide(); }; Snapping.prototype.getSnapLine = function(orientation) { if (!this._snapLines) { this._createSnapLines(); } return this._snapLines[orientation]; }; Snapping.prototype.hide = function() { forEach(this._snapLines, function(snapLine) { snapLine.update(); }); }; |