force-graph 1.43.5 → 1.44.0

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.
@@ -3,7 +3,7 @@ import { zoom, zoomTransform } from 'd3-zoom';
3
3
  import { drag } from 'd3-drag';
4
4
  import { min, max } from 'd3-array';
5
5
  import { throttle } from 'lodash-es';
6
- import * as TWEEN from '@tweenjs/tween.js';
6
+ import { Group, Tween, Easing } from '@tweenjs/tween.js';
7
7
  import Kapsule from 'kapsule';
8
8
  import accessorFn from 'accessor-fn';
9
9
  import ColorTracker from 'canvas-color-tracker';
@@ -16,7 +16,7 @@ import { schemePaired } from 'd3-scale-chromatic';
16
16
  function styleInject(css, ref) {
17
17
  if (ref === void 0) ref = {};
18
18
  var insertAt = ref.insertAt;
19
- if (!css || typeof document === 'undefined') {
19
+ if (typeof document === 'undefined') {
20
20
  return;
21
21
  }
22
22
  var head = document.head || document.getElementsByTagName('head')[0];
@@ -41,12 +41,31 @@ function styleInject(css, ref) {
41
41
  var css_248z = ".force-graph-container canvas {\n display: block;\n user-select: none;\n outline: none;\n -webkit-tap-highlight-color: transparent;\n}\n\n.force-graph-container .graph-tooltip {\n position: absolute;\n top: 0;\n font-family: sans-serif;\n font-size: 16px;\n padding: 4px;\n border-radius: 3px;\n color: #eee;\n background: rgba(0,0,0,0.65);\n visibility: hidden; /* by default */\n}\n\n.force-graph-container .clickable {\n cursor: pointer;\n}\n\n.force-graph-container .grabbable {\n cursor: move;\n cursor: grab;\n cursor: -moz-grab;\n cursor: -webkit-grab;\n}\n\n.force-graph-container .grabbable:active {\n cursor: grabbing;\n cursor: -moz-grabbing;\n cursor: -webkit-grabbing;\n}\n";
42
42
  styleInject(css_248z);
43
43
 
44
+ function _arrayLikeToArray(r, a) {
45
+ (null == a || a > r.length) && (a = r.length);
46
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
47
+ return n;
48
+ }
49
+ function _arrayWithHoles(r) {
50
+ if (Array.isArray(r)) return r;
51
+ }
52
+ function _arrayWithoutHoles(r) {
53
+ if (Array.isArray(r)) return _arrayLikeToArray(r);
54
+ }
44
55
  function _construct(t, e, r) {
45
56
  if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
46
57
  var o = [null];
47
58
  o.push.apply(o, e);
48
59
  var p = new (t.bind.apply(t, o))();
49
- return r && _setPrototypeOf(p, r.prototype), p;
60
+ return p;
61
+ }
62
+ function _defineProperty(e, r, t) {
63
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
64
+ value: t,
65
+ enumerable: !0,
66
+ configurable: !0,
67
+ writable: !0
68
+ }) : e[r] = t, e;
50
69
  }
51
70
  function _isNativeReflectConstruct() {
52
71
  try {
@@ -56,6 +75,9 @@ function _isNativeReflectConstruct() {
56
75
  return !!t;
57
76
  })();
58
77
  }
78
+ function _iterableToArray(r) {
79
+ if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
80
+ }
59
81
  function _iterableToArrayLimit(r, l) {
60
82
  var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
61
83
  if (null != t) {
@@ -67,10 +89,7 @@ function _iterableToArrayLimit(r, l) {
67
89
  f = !0,
68
90
  o = !1;
69
91
  try {
70
- if (i = (t = t.call(r)).next, 0 === l) {
71
- if (Object(t) !== t) return;
72
- f = !1;
73
- } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
92
+ if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
74
93
  } catch (r) {
75
94
  o = !0, n = r;
76
95
  } finally {
@@ -83,6 +102,12 @@ function _iterableToArrayLimit(r, l) {
83
102
  return a;
84
103
  }
85
104
  }
105
+ function _nonIterableRest() {
106
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
107
+ }
108
+ function _nonIterableSpread() {
109
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
110
+ }
86
111
  function ownKeys(e, r) {
87
112
  var t = Object.keys(e);
88
113
  if (Object.getOwnPropertySymbols) {
@@ -104,6 +129,12 @@ function _objectSpread2(e) {
104
129
  }
105
130
  return e;
106
131
  }
132
+ function _slicedToArray(r, e) {
133
+ return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
134
+ }
135
+ function _toConsumableArray(r) {
136
+ return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
137
+ }
107
138
  function _toPrimitive(t, r) {
108
139
  if ("object" != typeof t || !t) return t;
109
140
  var e = t[Symbol.toPrimitive];
@@ -116,7 +147,7 @@ function _toPrimitive(t, r) {
116
147
  }
117
148
  function _toPropertyKey(t) {
118
149
  var i = _toPrimitive(t, "string");
119
- return "symbol" == typeof i ? i : String(i);
150
+ return "symbol" == typeof i ? i : i + "";
120
151
  }
121
152
  function _typeof(o) {
122
153
  "@babel/helpers - typeof";
@@ -127,60 +158,12 @@ function _typeof(o) {
127
158
  return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
128
159
  }, _typeof(o);
129
160
  }
130
- function _defineProperty(obj, key, value) {
131
- key = _toPropertyKey(key);
132
- if (key in obj) {
133
- Object.defineProperty(obj, key, {
134
- value: value,
135
- enumerable: true,
136
- configurable: true,
137
- writable: true
138
- });
139
- } else {
140
- obj[key] = value;
161
+ function _unsupportedIterableToArray(r, a) {
162
+ if (r) {
163
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
164
+ var t = {}.toString.call(r).slice(8, -1);
165
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
141
166
  }
142
- return obj;
143
- }
144
- function _setPrototypeOf(o, p) {
145
- _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
146
- o.__proto__ = p;
147
- return o;
148
- };
149
- return _setPrototypeOf(o, p);
150
- }
151
- function _slicedToArray(arr, i) {
152
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
153
- }
154
- function _toConsumableArray(arr) {
155
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
156
- }
157
- function _arrayWithoutHoles(arr) {
158
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
159
- }
160
- function _arrayWithHoles(arr) {
161
- if (Array.isArray(arr)) return arr;
162
- }
163
- function _iterableToArray(iter) {
164
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
165
- }
166
- function _unsupportedIterableToArray(o, minLen) {
167
- if (!o) return;
168
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
169
- var n = Object.prototype.toString.call(o).slice(8, -1);
170
- if (n === "Object" && o.constructor) n = o.constructor.name;
171
- if (n === "Map" || n === "Set") return Array.from(o);
172
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
173
- }
174
- function _arrayLikeToArray(arr, len) {
175
- if (len == null || len > arr.length) len = arr.length;
176
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
177
- return arr2;
178
- }
179
- function _nonIterableSpread() {
180
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
181
- }
182
- function _nonIterableRest() {
183
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
184
167
  }
185
168
 
186
169
  var autoColorScale = scaleOrdinal(schemePaired);
@@ -1032,6 +1015,12 @@ var forceGraph = Kapsule({
1032
1015
  links: []
1033
1016
  },
1034
1017
  onChange: function onChange(d, state) {
1018
+ // Wipe color registry if all objects are new
1019
+ [d.nodes, d.links].every(function (arr) {
1020
+ return (arr || []).every(function (d) {
1021
+ return !d.hasOwnProperty('__indexColor');
1022
+ });
1023
+ }) && state.colorTracker.reset();
1035
1024
  [{
1036
1025
  type: 'Node',
1037
1026
  objs: d.nodes
@@ -1215,7 +1204,7 @@ var forceGraph = Kapsule({
1215
1204
  // no animation
1216
1205
  setCenter(finalPos);
1217
1206
  } else {
1218
- new TWEEN.Tween(getCenter()).to(finalPos, transitionDuration).easing(TWEEN.Easing.Quadratic.Out).onUpdate(setCenter).start();
1207
+ state.tweenGroup.add(new Tween(getCenter()).to(finalPos, transitionDuration).easing(Easing.Quadratic.Out).onUpdate(setCenter).start());
1219
1208
  }
1220
1209
  return this;
1221
1210
  }
@@ -1248,14 +1237,14 @@ var forceGraph = Kapsule({
1248
1237
  // no animation
1249
1238
  setZoom(k);
1250
1239
  } else {
1251
- new TWEEN.Tween({
1240
+ state.tweenGroup.add(new Tween({
1252
1241
  k: getZoom()
1253
1242
  }).to({
1254
1243
  k: k
1255
- }, transitionDuration).easing(TWEEN.Easing.Quadratic.Out).onUpdate(function (_ref6) {
1244
+ }, transitionDuration).easing(Easing.Quadratic.Out).onUpdate(function (_ref6) {
1256
1245
  var k = _ref6.k;
1257
1246
  return setZoom(k);
1258
- }).start();
1247
+ }).start());
1259
1248
  }
1260
1249
  return this;
1261
1250
  }
@@ -1346,7 +1335,9 @@ var forceGraph = Kapsule({
1346
1335
  zoom: zoom(),
1347
1336
  forceGraph: new CanvasForceGraph(),
1348
1337
  shadowGraph: new CanvasForceGraph().cooldownTicks(0).nodeColor('__indexColor').linkColor('__indexColor').isShadow(true),
1349
- colorTracker: new ColorTracker() // indexed objects for rgb lookup
1338
+ colorTracker: new ColorTracker(),
1339
+ // indexed objects for rgb lookup
1340
+ tweenGroup: new Group()
1350
1341
  };
1351
1342
  },
1352
1343
  init: function init(domNode, state) {
@@ -1646,7 +1637,7 @@ var forceGraph = Kapsule({
1646
1637
  state.forceGraph.globalScale(globalScale).tickFrame();
1647
1638
  state.onRenderFramePost && state.onRenderFramePost(ctx, globalScale);
1648
1639
  }
1649
- TWEEN.update(); // update canvas animation tweens
1640
+ state.tweenGroup.update(); // update canvas animation tweens
1650
1641
 
1651
1642
  state.animationFrameRequestId = requestAnimationFrame(animate);
1652
1643
  })();
@@ -1,8 +1,8 @@
1
1
  <head>
2
2
  <style> body { margin: 0; } </style>
3
3
 
4
- <script src="//unpkg.com/force-graph"></script>
5
- <!--<script src="../../dist/force-graph.js"></script>-->
4
+ <!-- <script src="//unpkg.com/force-graph"></script>-->
5
+ <script src="../../dist/force-graph.js"></script>
6
6
  </head>
7
7
 
8
8
  <body>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "force-graph",
3
- "version": "1.43.5",
3
+ "version": "1.44.0",
4
4
  "description": "2D force-directed graph rendered on HTML5 canvas",
5
5
  "type": "module",
6
6
  "unpkg": "dist/force-graph.min.js",
@@ -47,10 +47,10 @@
47
47
  "src/**/*"
48
48
  ],
49
49
  "dependencies": {
50
- "@tweenjs/tween.js": "18 - 23",
50
+ "@tweenjs/tween.js": "18 - 25",
51
51
  "accessor-fn": "1",
52
52
  "bezier-js": "3 - 6",
53
- "canvas-color-tracker": "1",
53
+ "canvas-color-tracker": "^1.3",
54
54
  "d3-array": "1 - 3",
55
55
  "d3-drag": "2 - 3",
56
56
  "d3-force-3d": "2 - 3",
@@ -63,18 +63,18 @@
63
63
  "lodash-es": "4"
64
64
  },
65
65
  "devDependencies": {
66
- "@babel/core": "^7.23.9",
67
- "@babel/preset-env": "^7.23.9",
66
+ "@babel/core": "^7.25.2",
67
+ "@babel/preset-env": "^7.25.4",
68
68
  "@rollup/plugin-babel": "^6.0.4",
69
- "@rollup/plugin-commonjs": "^25.0.7",
70
- "@rollup/plugin-node-resolve": "^15.2.3",
69
+ "@rollup/plugin-commonjs": "^28.0.0",
70
+ "@rollup/plugin-node-resolve": "^15.3.0",
71
71
  "@rollup/plugin-terser": "^0.4.4",
72
- "postcss": "^8.4.35",
73
- "rimraf": "^5.0.5",
74
- "rollup": "^4.12.0",
75
- "rollup-plugin-dts": "^6.1.0",
72
+ "postcss": "^8.4.47",
73
+ "rimraf": "^6.0.1",
74
+ "rollup": "^4.22.4",
75
+ "rollup-plugin-dts": "^6.1.1",
76
76
  "rollup-plugin-postcss": "^4.0.2",
77
- "typescript": "^5.3.3"
77
+ "typescript": "^5.6.2"
78
78
  },
79
79
  "engines": {
80
80
  "node": ">=12"
@@ -3,7 +3,7 @@ import { zoom as d3Zoom, zoomTransform as d3ZoomTransform } from 'd3-zoom';
3
3
  import { drag as d3Drag } from 'd3-drag';
4
4
  import { max as d3Max, min as d3Min } from 'd3-array';
5
5
  import { throttle } from 'lodash-es';
6
- import * as TWEEN from '@tweenjs/tween.js';
6
+ import { Tween, Group as TweenGroup, Easing } from '@tweenjs/tween.js';
7
7
  import Kapsule from 'kapsule';
8
8
  import accessorFn from 'accessor-fn';
9
9
  import ColorTracker from 'canvas-color-tracker';
@@ -125,6 +125,9 @@ export default Kapsule({
125
125
  graphData: {
126
126
  default: { nodes: [], links: [] },
127
127
  onChange: ((d, state) => {
128
+ // Wipe color registry if all objects are new
129
+ [d.nodes, d.links].every(arr => (arr || []).every(d => !d.hasOwnProperty('__indexColor'))) && state.colorTracker.reset();
130
+
128
131
  [{ type: 'Node', objs: d.nodes }, { type: 'Link', objs: d.links }].forEach(hexIndex);
129
132
  state.forceGraph.graphData(d);
130
133
  state.shadowGraph.graphData(d);
@@ -210,11 +213,13 @@ export default Kapsule({
210
213
  if (!transitionDuration) { // no animation
211
214
  setCenter(finalPos);
212
215
  } else {
213
- new TWEEN.Tween(getCenter())
214
- .to(finalPos, transitionDuration)
215
- .easing(TWEEN.Easing.Quadratic.Out)
216
- .onUpdate(setCenter)
217
- .start();
216
+ state.tweenGroup.add(
217
+ new Tween(getCenter())
218
+ .to(finalPos, transitionDuration)
219
+ .easing(Easing.Quadratic.Out)
220
+ .onUpdate(setCenter)
221
+ .start()
222
+ );
218
223
  }
219
224
  return this;
220
225
  }
@@ -246,11 +251,13 @@ export default Kapsule({
246
251
  if (!transitionDuration) { // no animation
247
252
  setZoom(k);
248
253
  } else {
249
- new TWEEN.Tween({ k: getZoom() })
250
- .to({ k }, transitionDuration)
251
- .easing(TWEEN.Easing.Quadratic.Out)
252
- .onUpdate(({ k }) => setZoom(k))
253
- .start();
254
+ state.tweenGroup.add(
255
+ new Tween({ k: getZoom() })
256
+ .to({ k }, transitionDuration)
257
+ .easing(Easing.Quadratic.Out)
258
+ .onUpdate(({ k }) => setZoom(k))
259
+ .start()
260
+ );
254
261
  }
255
262
  return this;
256
263
  }
@@ -339,7 +346,8 @@ export default Kapsule({
339
346
  .nodeColor('__indexColor')
340
347
  .linkColor('__indexColor')
341
348
  .isShadow(true),
342
- colorTracker: new ColorTracker() // indexed objects for rgb lookup
349
+ colorTracker: new ColorTracker(), // indexed objects for rgb lookup
350
+ tweenGroup: new TweenGroup()
343
351
  }),
344
352
 
345
353
  init: function(domNode, state) {
@@ -640,7 +648,7 @@ export default Kapsule({
640
648
  state.onRenderFramePost && state.onRenderFramePost(ctx, globalScale);
641
649
  }
642
650
 
643
- TWEEN.update(); // update canvas animation tweens
651
+ state.tweenGroup.update(); // update canvas animation tweens
644
652
 
645
653
  state.animationFrameRequestId = requestAnimationFrame(animate);
646
654
  })();