@krainovsd/graph 0.12.0-beta.1 → 0.12.0-beta.3

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.
package/lib/cjs/index.cjs CHANGED
@@ -308,9 +308,9 @@ function rgbAnimationByProgress(start, end, progress) {
308
308
  let countTime = 0;
309
309
  let countOperation = 0;
310
310
  function setDrawTime(cb, showEveryTick) {
311
- return () => {
311
+ return (recursive) => {
312
312
  const start = performance.now();
313
- cb();
313
+ cb(recursive);
314
314
  const end = performance.now();
315
315
  const result = end - start;
316
316
  countTime += result;
@@ -1944,29 +1944,42 @@ function getDrawNode(nodeRenders, textRenders) {
1944
1944
  }
1945
1945
 
1946
1946
  function initDraw() {
1947
- function calculateHighlight() {
1948
- this.highlightDrawing = true;
1947
+ let hasHighlight = false;
1948
+ function calculateHighlight(recursive) {
1949
+ if (hasHighlight && !recursive)
1950
+ return;
1951
+ /** animation up */
1949
1952
  if (!this.highlightWorking && this.highlightProgress > 0) {
1950
1953
  const highlightDownStep = 1 / this.highlightSettings.highlightDownFrames;
1951
1954
  this.highlightProgress -= highlightDownStep;
1952
1955
  if (!this.simulationWorking) {
1953
- return void requestAnimationFrame(() => this.draw());
1956
+ hasHighlight = true;
1957
+ return void requestAnimationFrame(() => this.draw(true));
1954
1958
  }
1955
- if (!this.linkSettings.particles)
1959
+ if (!this.linkSettings.particles) {
1960
+ hasHighlight = false;
1956
1961
  return;
1962
+ }
1957
1963
  }
1964
+ /** animation down */
1958
1965
  if (this.highlightWorking && this.highlightProgress < 1) {
1959
1966
  const highlightUpStep = 1 / this.highlightSettings.highlightUpFrames;
1960
1967
  this.highlightProgress += highlightUpStep;
1961
1968
  if (!this.simulationWorking) {
1962
- return void requestAnimationFrame(() => this.draw());
1969
+ hasHighlight = true;
1970
+ return void requestAnimationFrame(() => this.draw(true));
1963
1971
  }
1964
- if (!this.linkSettings.particles)
1972
+ if (!this.linkSettings.particles) {
1973
+ hasHighlight = false;
1965
1974
  return;
1975
+ }
1966
1976
  }
1977
+ /** animation active */
1967
1978
  if (this.linkSettings.particles && this.highlightWorking && !this.simulationWorking) {
1968
- return void requestAnimationFrame(() => this.draw());
1979
+ hasHighlight = true;
1980
+ return void requestAnimationFrame(() => this.draw(true));
1969
1981
  }
1982
+ /** animation stop */
1970
1983
  if (!this.highlightWorking && this.highlightProgress <= 0) {
1971
1984
  if (this.highlightedNeighbors || this.highlightedNode || this.highlightedLink) {
1972
1985
  this.highlightedNeighbors = null;
@@ -1974,13 +1987,14 @@ function initDraw() {
1974
1987
  this.highlightedLink = null;
1975
1988
  this.particles = {};
1976
1989
  if (!this.simulationWorking) {
1977
- return void requestAnimationFrame(() => this.draw());
1990
+ hasHighlight = true;
1991
+ return void requestAnimationFrame(() => this.draw(true));
1978
1992
  }
1979
1993
  }
1980
1994
  }
1981
- this.highlightDrawing = false;
1995
+ hasHighlight = false;
1982
1996
  }
1983
- function draw() {
1997
+ function draw(recursive = false) {
1984
1998
  if (!this.context)
1985
1999
  return;
1986
2000
  if (this.listeners.onDraw) {
@@ -2001,7 +2015,7 @@ function initDraw() {
2001
2015
  textRenders.forEach((render) => render());
2002
2016
  this.context.restore();
2003
2017
  this.listeners.onDrawFinished?.call?.(this);
2004
- calculateHighlight.bind(this)();
2018
+ calculateHighlight.bind(this)(recursive);
2005
2019
  }
2006
2020
  if (this.graphSettings.showDrawTime) {
2007
2021
  return setDrawTime(draw.bind(this), this.graphSettings.showDrawTimeEveryTick);
@@ -2059,7 +2073,7 @@ function initPointer() {
2059
2073
  this.highlightedLink = null;
2060
2074
  this.highlightedNeighbors = new Set(this.highlightedNode?.neighbors ?? []);
2061
2075
  this.highlightWorking = true;
2062
- if (!this.simulationWorking && !this.highlightDrawing)
2076
+ if (!this.simulationWorking)
2063
2077
  requestAnimationFrame(() => {
2064
2078
  this.draw();
2065
2079
  });
@@ -2074,14 +2088,14 @@ function initPointer() {
2074
2088
  this.highlightedNode = null;
2075
2089
  this.highlightedNeighbors = new Set([currentLink.source.id, currentLink.target.id]);
2076
2090
  this.highlightWorking = true;
2077
- if (!this.simulationWorking && !this.highlightDrawing)
2091
+ if (!this.simulationWorking)
2078
2092
  requestAnimationFrame(() => {
2079
2093
  this.draw();
2080
2094
  });
2081
2095
  }
2082
2096
  else if (!currentNode && !currentLink && (this.highlightedNode || this.highlightedLink)) {
2083
2097
  this.highlightWorking = false;
2084
- if (!this.simulationWorking && !this.highlightDrawing)
2098
+ if (!this.simulationWorking)
2085
2099
  requestAnimationFrame(() => {
2086
2100
  this.draw();
2087
2101
  });
@@ -2469,7 +2483,6 @@ class GraphCanvas {
2469
2483
  highlightedNeighbors = null;
2470
2484
  highlightProgress = 1;
2471
2485
  highlightWorking = false;
2472
- highlightDrawing = false;
2473
2486
  get simulationWorking() {
2474
2487
  const simulationAlpha = this.simulation?.alpha?.() ?? 0;
2475
2488
  const simulationAlphaMin = this.simulation?.alphaMin?.() ?? 0;
@@ -2504,15 +2517,15 @@ class GraphCanvas {
2504
2517
  nodes: this.nodes,
2505
2518
  };
2506
2519
  }
2507
- changeData(options, alpha) {
2520
+ changeData(options, alpha = 0.5, clearCache = true) {
2508
2521
  if (options.links != undefined)
2509
2522
  this.links = options.links;
2510
2523
  if (options.nodes != undefined)
2511
2524
  this.nodes = options.nodes;
2512
2525
  if (options.nodes != undefined || options.links != undefined)
2513
- this.updateData(alpha);
2526
+ this.updateData(alpha, clearCache);
2514
2527
  }
2515
- changeSettings(options) {
2528
+ changeSettings(options, clearCache = true) {
2516
2529
  if (options.graphSettings) {
2517
2530
  this.graphSettings = graphSettingsGetter(options.graphSettings, this.graphSettings);
2518
2531
  this.draw = initDraw.call(this);
@@ -2523,22 +2536,28 @@ class GraphCanvas {
2523
2536
  }
2524
2537
  if (options.highlightSettings) {
2525
2538
  this.highlightSettings = highlightSettingsGetter(options.highlightSettings, this.highlightSettings);
2526
- this.linkOptionsCache = {};
2527
- this.cachedNodeText = {};
2528
- this.cachedNodeLabel = {};
2529
- this.cachedTextNodeParameters = {};
2530
- this.nodeOptionsCache = {};
2539
+ if (clearCache) {
2540
+ this.linkOptionsCache = {};
2541
+ this.cachedNodeText = {};
2542
+ this.cachedNodeLabel = {};
2543
+ this.cachedTextNodeParameters = {};
2544
+ this.nodeOptionsCache = {};
2545
+ }
2531
2546
  }
2532
2547
  if (options.linkSettings) {
2533
2548
  this.linkSettings = linkSettingsGetter(options.linkSettings, this.linkSettings);
2534
- this.linkOptionsCache = {};
2549
+ if (clearCache) {
2550
+ this.linkOptionsCache = {};
2551
+ }
2535
2552
  }
2536
2553
  if (options.nodeSettings) {
2537
2554
  this.nodeSettings = nodeSettingsGetter(options.nodeSettings, this.nodeSettings);
2538
- this.cachedNodeText = {};
2539
- this.cachedNodeLabel = {};
2540
- this.cachedTextNodeParameters = {};
2541
- this.nodeOptionsCache = {};
2555
+ if (clearCache) {
2556
+ this.cachedNodeText = {};
2557
+ this.cachedNodeLabel = {};
2558
+ this.cachedTextNodeParameters = {};
2559
+ this.nodeOptionsCache = {};
2560
+ }
2542
2561
  initCollideForce.call(this, true);
2543
2562
  }
2544
2563
  if (options.forceSettings) {
@@ -2631,10 +2650,11 @@ class GraphCanvas {
2631
2650
  this.highlightedNeighbors = null;
2632
2651
  this.highlightProgress = 0;
2633
2652
  this.highlightWorking = false;
2634
- this.highlightDrawing = false;
2635
2653
  }
2636
- updateData(alpha) {
2637
- this.clearCache();
2654
+ updateData(alpha = 0.5, clearCache = true) {
2655
+ if (clearCache) {
2656
+ this.clearCache();
2657
+ }
2638
2658
  if (this.simulation) {
2639
2659
  initCollideForce.call(this, false);
2640
2660
  this.simulation
@@ -2650,7 +2670,7 @@ class GraphCanvas {
2650
2670
  .iterations(this.forceSettings.forces && this.forceSettings.linkForce
2651
2671
  ? this.forceSettings.linkIterations
2652
2672
  : 0))
2653
- .alpha(alpha ?? 0.5)
2673
+ .alpha(alpha)
2654
2674
  .restart();
2655
2675
  }
2656
2676
  }