|
Last change
on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago |
|
Prototype 1.1
|
-
Property mode
set to
100644
|
|
File size:
798 bytes
|
| Line | |
|---|
| 1 | export default function(x, y) {
|
|---|
| 2 | var nodes, strength = 1;
|
|---|
| 3 |
|
|---|
| 4 | if (x == null) x = 0;
|
|---|
| 5 | if (y == null) y = 0;
|
|---|
| 6 |
|
|---|
| 7 | function force() {
|
|---|
| 8 | var i,
|
|---|
| 9 | n = nodes.length,
|
|---|
| 10 | node,
|
|---|
| 11 | sx = 0,
|
|---|
| 12 | sy = 0;
|
|---|
| 13 |
|
|---|
| 14 | for (i = 0; i < n; ++i) {
|
|---|
| 15 | node = nodes[i], sx += node.x, sy += node.y;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | for (sx = (sx / n - x) * strength, sy = (sy / n - y) * strength, i = 0; i < n; ++i) {
|
|---|
| 19 | node = nodes[i], node.x -= sx, node.y -= sy;
|
|---|
| 20 | }
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | force.initialize = function(_) {
|
|---|
| 24 | nodes = _;
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| 27 | force.x = function(_) {
|
|---|
| 28 | return arguments.length ? (x = +_, force) : x;
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | force.y = function(_) {
|
|---|
| 32 | return arguments.length ? (y = +_, force) : y;
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | force.strength = function(_) {
|
|---|
| 36 | return arguments.length ? (strength = +_, force) : strength;
|
|---|
| 37 | };
|
|---|
| 38 |
|
|---|
| 39 | return force;
|
|---|
| 40 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.