UNPKG

d3-force-grid

Version:

A force to snap nodes to a grid for the d3-force simulation engine.

3 lines (2 loc) 1.25 kB
// Version 1.0.0 d3-force-grid - https://github.com/vasturiano/d3-force-grid !function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).d3=n.d3||{})}(this,(function(n){"use strict";n.forceGrid=function(){var n,t,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:10,r=[],i=e,o=e,u=e,f=.3,c=!1;function l(n){var e=f*(c?n:1);r.forEach((function(n){t.forEach((function(t){var r=t.dim,i=t.v,o=t.step,u=n[r],f=Math.round(u/o)*o;n[i]+=(f-u)*e}))}))}return l.initialize=function(e){r=e;for(var f=arguments.length,c=new Array(f>1?f-1:0),l=1;l<f;l++)c[l-1]=arguments[l];n=c.find((function(n){return[1,2,3].includes(n)}))||2,t=["x",n>1?"y":null,n>2?"z":null].filter((function(n){return n})).map((function(n){return{dim:n,v:"v".concat(n),step:{x:i,y:o,z:u}[n]}}))},l.step=function(n){return arguments.length?(i=o=u=n,l):i},l.stepX=function(n){return arguments.length?(i=n,l):i},l.stepY=function(n){return arguments.length?(o=n,l):o},l.stepZ=function(n){return arguments.length?(u=n,l):u},l.strength=function(n){return arguments.length?(f=n,l):f},l.considerAlpha=function(n){return arguments.length?(c=n,l):c},l}}));