mirror of https://github.com/logseq/logseq
add comments for force mechanics
parent
cc38b5b48f
commit
2ee5a3f43b
|
@ -54,19 +54,34 @@
|
|||
:edge {:color "#A5B4FC"}})
|
||||
|
||||
(defn layout!
|
||||
"Node forces documentation can be read in more detail here https://d3js.org/d3-force"
|
||||
[nodes links link-dist]
|
||||
(let [nodes-count (count nodes)
|
||||
simulation (forceSimulation nodes)]
|
||||
(-> simulation
|
||||
(.force "link"
|
||||
;; The link force pushes linked nodes together or apart according to the desired link distance.
|
||||
;; The strength of the force is proportional to the difference between the linked nodes distance
|
||||
;; and the target distance, similar to a spring force.
|
||||
(-> (forceLink)
|
||||
(.id (fn [d] (.-id d)))
|
||||
(.distance link-dist)
|
||||
(.links links)))
|
||||
(.force "charge"
|
||||
;; The many-body (or n-body) force applies mutually amongst all nodes.
|
||||
;; It can be used to simulate gravity or electrostatic charge.
|
||||
(-> (forceManyBody)
|
||||
;; The minimum distance between nodes over which this force is considered.
|
||||
;; A minimum distance establishes an upper bound on the strength of the force between two nearby nodes, avoiding instability.
|
||||
(.distanceMin 1)
|
||||
;; The maximum distance between nodes over which this force is considered.
|
||||
;; Specifying a finite maximum distance improves performance and produces a more localized layout.
|
||||
(.distanceMax (if (> nodes-count 500) 4000 600))
|
||||
;; For a cluster of nodes that is far away, the charge force can be approximated by treating the cluster as a single, larger node.
|
||||
;; The theta parameter determines the accuracy of the approximation
|
||||
(.theta 0.5)
|
||||
;; A positive value causes nodes to attract each other, similar to gravity,
|
||||
;; while a negative value causes nodes to repel each other, similar to electrostatic charge.
|
||||
(.strength -600)))
|
||||
(.force "collision"
|
||||
(-> (forceCollide)
|
||||
|
@ -75,6 +90,9 @@
|
|||
(.force "x" (-> (forceX 0) (.strength 0.02)))
|
||||
(.force "y" (-> (forceY 0) (.strength 0.02)))
|
||||
(.force "center" (forceCenter))
|
||||
;; The decay factor is akin to atmospheric friction; after the application of any forces during a tick,
|
||||
;; each node’s velocity is multiplied by 1 - decay. As with lowering the alpha decay rate,
|
||||
;; less velocity decay may converge on a better solution, but risks numerical instabilities and oscillation.
|
||||
(.velocityDecay 0.8))
|
||||
(reset! *simulation simulation)
|
||||
simulation))
|
||||
|
|
Loading…
Reference in New Issue