UNPKG

d3-force-elastic

Version:

A Hooke's law spring-like attraction force between node pairs for the d3-force simulation engine.

3 lines (2 loc) 2.13 kB
// Version 1.1.0 d3-force-elastic - https://github.com/vasturiano/d3-force-elastic !function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).d3=t.d3||{})}(this,(function(t){"use strict";function n(t){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(t)}function e(t){return function(){return t}}function o(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return Math.sqrt(t*t+n*n+e*e)}t.forceElastic=function(){var t,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],u=[],i=function(t){return t.index},f=function(t){return 30},c=function(t){return.8},a=function(t){return 0};function l(n){for(var e=0;e<r.length;e++){var u=r[e],i=u.target.x-u.source.x,l=u.target.y-u.source.y||0,s=u.target.z-u.source.z||0,y=o(i,l,s),g=f(u),p=a(u);if(p||!(y<=g)){var d=n*c(u)*(y-g)*(y<g?p:1),v=null!=u.source.fx?0:null!=u.target.fx?1:.5,h=d*v,b=d*(1-v);u.source.vx+=i/y*h,u.target.vx-=i/y*b,t>1&&(u.source.vy+=l/y*h,u.target.vy-=l/y*b),t>2&&(u.source.vz+=s/y*h,u.target.vz-=s/y*b)}}}function s(){var t=new Map(u.map((function(t,n){return[i(t,n,u),t]})));r.forEach((function(e){"object"!==n(e.source)&&(e.source=t.get(e.source)||e.source),"object"!==n(e.target)&&(e.target=t.get(e.target)||e.target)}))}return l.initialize=function(n){u=n;for(var e=arguments.length,o=new Array(e>1?e-1:0),r=1;r<e;r++)o[r-1]=arguments[r];t=o.find((function(t){return[1,2,3].includes(t)}))||2,s()},l.links=function(t){return arguments.length?(r=t,s(),l):r},l.id=function(t){return arguments.length?(i=t,l):i},l.distance=function(t){return arguments.length?(f="function"==typeof t?t:e(+t),l):f},l.elasticity=function(t){return arguments.length?(c="function"==typeof t?t:e(+t),l):c},l.compressionFactor=function(t){return arguments.length?(a="function"==typeof t?t:e(+t),l):a},l}}));