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) 1.99 kB
// Version 1.0.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};function a(n){for(var e=0;e<r.length;e++){var u=r[e],i=u.target.x-u.source.x,a=u.target.y-u.source.y||0,l=u.target.z-u.source.z||0,s=o(i,a,l),y=f(u);if(!(s<=y)){var g=n*c(u)*(s-y),d=null!=u.source.fx?0:null!=u.target.fx?1:.5,p=g*d,v=g*(1-d);u.source.vx+=i/s*p,u.target.vx-=i/s*v,t>1&&(u.source.vy+=a/s*p,u.target.vy-=a/s*v),t>2&&(u.source.vz+=l/s*p,u.target.vz-=l/s*v)}}}function l(){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 a.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,l()},a.links=function(t){return arguments.length?(r=t,l(),a):r},a.id=function(t){return arguments.length?(i=t,a):i},a.distance=function(t){return arguments.length?(f="function"==typeof t?t:e(+t),a):f},a.elasticity=function(t){return arguments.length?(c="function"==typeof t?t:e(+t),a):c},a}}));