force-graph 1.49.0 → 1.49.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { select } from 'd3-selection';
2
2
  import { zoom, zoomTransform } from 'd3-zoom';
3
3
  import { drag } from 'd3-drag';
4
- import { min, max } from 'd3-array';
4
+ import { sum, min, max } from 'd3-array';
5
5
  import { throttle } from 'lodash-es';
6
6
  import { Group, Tween, Easing } from '@tweenjs/tween.js';
7
7
  import Kapsule from 'kapsule';
@@ -940,6 +940,7 @@ function linkKapsule (kapsulePropNames, kapsuleType) {
940
940
 
941
941
  var HOVER_CANVAS_THROTTLE_DELAY = 800; // ms to throttle shadow canvas updates for perf improvement
942
942
  var ZOOM2NODES_FACTOR = 4;
943
+ var DRAG_CLICK_TOLERANCE_PX = 5; // How many px can a node be accidentally dragged before disabling the click
943
944
 
944
945
  // Expose config from forceGraph
945
946
  var bindFG = linkKapsule('forceGraph', CanvasForceGraph);
@@ -1413,17 +1414,20 @@ var forceGraph = Kapsule({
1413
1414
  var initPos = obj.__initialDragPos;
1414
1415
  var dragPos = ev;
1415
1416
  var k = zoomTransform(state.canvas).k;
1416
- var translate = {
1417
- x: initPos.x + (dragPos.x - initPos.x) / k - obj.x,
1418
- y: initPos.y + (dragPos.y - initPos.y) / k - obj.y
1419
- };
1420
1417
 
1421
1418
  // Move fx/fy (and x/y) of nodes based on the scaled drag distance since the drag start
1422
1419
  ['x', 'y'].forEach(function (c) {
1423
1420
  return obj["f".concat(c)] = obj[c] = initPos[c] + (dragPos[c] - initPos[c]) / k;
1424
1421
  });
1425
1422
 
1426
- // prevent freeze while dragging
1423
+ // Only engage full drag if distance reaches above threshold
1424
+ if (!obj.__dragged && DRAG_CLICK_TOLERANCE_PX >= Math.sqrt(sum(['x', 'y'].map(function (k) {
1425
+ return Math.pow(ev[k] - initPos[k], 2);
1426
+ })))) return;
1427
+ var translate = {
1428
+ x: initPos.x + (dragPos.x - initPos.x) / k - obj.x,
1429
+ y: initPos.y + (dragPos.y - initPos.y) / k - obj.y
1430
+ };
1427
1431
  state.forceGraph.d3AlphaTarget(0.3) // keep engine running at low intensity throughout drag
1428
1432
  .resetCountdown(); // prevent freeze while dragging
1429
1433
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "force-graph",
3
- "version": "1.49.0",
3
+ "version": "1.49.2",
4
4
  "description": "2D force-directed graph rendered on HTML5 canvas",
5
5
  "type": "module",
6
6
  "unpkg": "dist/force-graph.min.js",
@@ -1,7 +1,7 @@
1
1
  import { select as d3Select } from 'd3-selection';
2
2
  import { zoom as d3Zoom, zoomTransform as d3ZoomTransform } from 'd3-zoom';
3
3
  import { drag as d3Drag } from 'd3-drag';
4
- import { max as d3Max, min as d3Min } from 'd3-array';
4
+ import { max as d3Max, min as d3Min, sum as d3Sum } from 'd3-array';
5
5
  import { throttle } from 'lodash-es';
6
6
  import { Tween, Group as TweenGroup, Easing } from '@tweenjs/tween.js';
7
7
  import Kapsule from 'kapsule';
@@ -14,6 +14,7 @@ import linkKapsule from './kapsule-link.js';
14
14
 
15
15
  const HOVER_CANVAS_THROTTLE_DELAY = 800; // ms to throttle shadow canvas updates for perf improvement
16
16
  const ZOOM2NODES_FACTOR = 4;
17
+ const DRAG_CLICK_TOLERANCE_PX = 5; // How many px can a node be accidentally dragged before disabling the click
17
18
 
18
19
  // Expose config from forceGraph
19
20
  const bindFG = linkKapsule('forceGraph', CanvasForceGraph);
@@ -398,7 +399,12 @@ export default Kapsule({
398
399
  })
399
400
  .on('start', ev => {
400
401
  const obj = ev.subject;
401
- obj.__initialDragPos = { x: obj.x, y: obj.y, fx: obj.fx, fy: obj.fy };
402
+ obj.__initialDragPos = {
403
+ x: obj.x,
404
+ y: obj.y,
405
+ fx: obj.fx,
406
+ fy: obj.fy
407
+ };
402
408
 
403
409
  // keep engine running at low intensity throughout drag
404
410
  if (!ev.active) {
@@ -414,15 +420,19 @@ export default Kapsule({
414
420
  const dragPos = ev;
415
421
 
416
422
  const k = d3ZoomTransform(state.canvas).k;
423
+
424
+ // Move fx/fy (and x/y) of nodes based on the scaled drag distance since the drag start
425
+ ['x', 'y'].forEach(c => obj[`f${c}`] = obj[c] = initPos[c] + (dragPos[c] - initPos[c]) / k);
426
+
427
+ // Only engage full drag if distance reaches above threshold
428
+ if (!obj.__dragged && (DRAG_CLICK_TOLERANCE_PX >= Math.sqrt(d3Sum(['x', 'y'].map(k => (ev[k] - initPos[k])**2)))))
429
+ return;
430
+
417
431
  const translate = {
418
432
  x: (initPos.x + (dragPos.x - initPos.x) / k) - obj.x,
419
433
  y: (initPos.y + (dragPos.y - initPos.y) / k) - obj.y
420
434
  };
421
435
 
422
- // Move fx/fy (and x/y) of nodes based on the scaled drag distance since the drag start
423
- ['x', 'y'].forEach(c => obj[`f${c}`] = obj[c] = initPos[c] + (dragPos[c] - initPos[c]) / k);
424
-
425
- // prevent freeze while dragging
426
436
  state.forceGraph
427
437
  .d3AlphaTarget(0.3) // keep engine running at low intensity throughout drag
428
438
  .resetCountdown(); // prevent freeze while dragging