Skip to content

Commit

Permalink
feat:fix force layout without animate
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo.lcw committed Jul 12, 2020
1 parent 9c17d29 commit 6ab38e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
14 changes: 12 additions & 2 deletions packages/graphin/src/layout/basic/force.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { optimizeDrawing, optimizeDrawingByNode } from '../../perf/optimizeDrawi
import { LayoutOptionBase, Data, Node as NodeType, ForceSimulation } from '../../types';

import forceWithWorker from './forceWithWorker';
import { isBoolean } from 'lodash';

export interface ForceLayoutOptions extends ForceProps, LayoutOptionBase {
isOptimization?: boolean;
Expand All @@ -26,6 +27,8 @@ const forceLayout = (data: Data, options: ForceLayoutOptions): Return => {
...others
} = options;

let animationCfg = isBoolean(animation) ? animation : true;

/** Webworker solution. Otherwise, browser UI rendering is blocked */
if (enableWorker && data.nodes.length > 100) {
// 100以下的节点不走webworker
Expand All @@ -36,7 +39,7 @@ const forceLayout = (data: Data, options: ForceLayoutOptions): Return => {
const simulation = new ForceLayout({
width,
height,
animation: animation !== undefined ? animation : true,
animation: animationCfg,
done: () => {
if (isOptimization) {
optimizeDrawing(graph, false);
Expand All @@ -49,8 +52,15 @@ const forceLayout = (data: Data, options: ForceLayoutOptions): Return => {
// 2. Mount Data
simulation.setData(data);

let resultData = data;

// 3. Custom rendering function
simulation.register('render', (forceData: Data) => {
if (!animationCfg) {
// 如果不需要动画
resultData = forceData;
return;
}
try {
forceData.nodes.forEach((item: NodeType) => {
const node = graph.findById(item.id);
Expand All @@ -74,7 +84,7 @@ const forceLayout = (data: Data, options: ForceLayoutOptions): Return => {
simulation.start();

return {
data,
data: resultData,
simulation,
};
};
Expand Down
24 changes: 7 additions & 17 deletions packages/graphin/src/layout/force/ForceLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,27 +276,17 @@ class ForceLayout {
};

slienceForce = () => {
console.time('force time without animate');
const firstTickInterval = 0.22;
for (let i = 0; i < this.props.MaxIterations; i++) {
for (let i = 0; this.averageDistance > 0.5 || i < 1; i++) {
const tickInterval = Math.max(0.02, firstTickInterval - i * 0.002);
this.tick(tickInterval);
const energy = this.calTotalEnergy();
/** 如果需要监控信息,则提供给用户 */
const monitor = this.registers.get('monitor');
if (monitor) {
monitor(this.reportMointor(energy));
}
if (
energy <= this.props.minEnergyThreshold ||
i === this.props.MaxIterations - 1 // 1000000次/(1000/60) = 60000s = 1min
) {
this.render();
if (this.props.done) {
this.props.done();
}
break;
}
this.iterations++;
}
console.timeEnd('force time without animate');
console.log('迭代次数:', this.iterations);
this.render();
this.props.done && this.props.done();
};

/** polyfill: support webworker requestAnimationFrame */
Expand Down

0 comments on commit 6ab38e4

Please sign in to comment.