Skip to content

Commit a932273

Browse files
authored
Expose ForceSimulation's default values by exporting them as constants (#530)
1 parent efc96e6 commit a932273

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

.changeset/slow-hounds-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': patch
3+
---
4+
5+
fix(ForceSimulation): Expose default values by exporting them as constants

packages/layerchart/src/lib/components/ForceSimulation.svelte

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,35 @@
2222
y2: number;
2323
}>;
2424
25+
/**
26+
* Default initial alpha value of the simulation.
27+
*/
28+
export const DEFAULT_ALPHA: number = 1;
29+
30+
/**
31+
* Default target alpha value for the simulation.
32+
*/
33+
export const DEFAULT_ALPHA_TARGET: number = 0;
34+
35+
/**
36+
* Default alpha decay rate per tick.
37+
*
38+
* Formula: `1 - Math.pow(0.001, 1 / 300)`.
39+
*/
40+
export const DEFAULT_ALPHA_DECAY: number = 1 - Math.pow(0.001, 1 / 300);
41+
42+
/**
43+
* Default minimum alpha value at which simulation stops.
44+
*/
45+
export const DEFAULT_ALPHA_MIN: number = 0.01;
46+
47+
/**
48+
* Default velocity decay factor applied to nodes each tick.
49+
*/
50+
export const DEFAULT_VELOCITY_DECAY: number = 0.4;
51+
2552
type NodeDatumFor<NodeDatum> = Prettify<NodeDatum & SimulationNodeDatum>;
53+
2654
type LinkDatumFor<NodeDatum, LinkDatum> = Prettify<
2755
LinkDatum & SimulationLinkDatum<NodeDatumFor<NodeDatum>>
2856
>;
@@ -46,31 +74,31 @@
4674
4775
/**
4876
* Current alpha value of the simulation
49-
* @default 1
77+
* @default DEFAULT_ALPHA
5078
*/
5179
alpha?: number;
5280
5381
/**
5482
* Target alpha value for the simulation
55-
* @default 0
83+
* @default DEFAULT_ALPHA_TARGET
5684
*/
5785
alphaTarget?: number;
5886
5987
/**
6088
* Alpha decay rate per tick
61-
* @default 1 - Math.pow(0.001, 1 / 300)
89+
* @default DEFAULT_ALPHA_DECAY
6290
*/
6391
alphaDecay?: number;
6492
6593
/**
6694
* Minimum alpha value at which simulation stops
67-
* @default 0.01
95+
* @default DEFAULT_ALPHA_MIN
6896
*/
6997
alphaMin?: number;
7098
7199
/**
72100
* Velocity decay factor applied to nodes each tick
73-
* @default 0.4
101+
* @default DEFAULT_VELOCITY_DECAY
74102
*/
75103
velocityDecay?: number;
76104
@@ -127,11 +155,11 @@
127155
let {
128156
forces,
129157
data,
130-
alpha = $bindable(1),
131-
alphaTarget = 0,
132-
alphaDecay = 1 - Math.pow(0.001, 1 / 300),
133-
alphaMin = 0.001,
134-
velocityDecay = 0.4,
158+
alpha = $bindable(DEFAULT_ALPHA),
159+
alphaTarget = DEFAULT_ALPHA_TARGET,
160+
alphaDecay = DEFAULT_ALPHA_DECAY,
161+
alphaMin = DEFAULT_ALPHA_MIN,
162+
velocityDecay = DEFAULT_VELOCITY_DECAY,
135163
stopped = false,
136164
static: staticProp,
137165
onStart: onStartProp = () => {},

0 commit comments

Comments
 (0)