|
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:
1.0 KB
|
| Line | |
|---|
| 1 | import constant from "./constant.js";
|
|---|
| 2 |
|
|---|
| 3 | export default function(y) {
|
|---|
| 4 | var strength = constant(0.1),
|
|---|
| 5 | nodes,
|
|---|
| 6 | strengths,
|
|---|
| 7 | yz;
|
|---|
| 8 |
|
|---|
| 9 | if (typeof y !== "function") y = constant(y == null ? 0 : +y);
|
|---|
| 10 |
|
|---|
| 11 | function force(alpha) {
|
|---|
| 12 | for (var i = 0, n = nodes.length, node; i < n; ++i) {
|
|---|
| 13 | node = nodes[i], node.vy += (yz[i] - node.y) * strengths[i] * alpha;
|
|---|
| 14 | }
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | function initialize() {
|
|---|
| 18 | if (!nodes) return;
|
|---|
| 19 | var i, n = nodes.length;
|
|---|
| 20 | strengths = new Array(n);
|
|---|
| 21 | yz = new Array(n);
|
|---|
| 22 | for (i = 0; i < n; ++i) {
|
|---|
| 23 | strengths[i] = isNaN(yz[i] = +y(nodes[i], i, nodes)) ? 0 : +strength(nodes[i], i, nodes);
|
|---|
| 24 | }
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | force.initialize = function(_) {
|
|---|
| 28 | nodes = _;
|
|---|
| 29 | initialize();
|
|---|
| 30 | };
|
|---|
| 31 |
|
|---|
| 32 | force.strength = function(_) {
|
|---|
| 33 | return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength;
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | force.y = function(_) {
|
|---|
| 37 | return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), initialize(), force) : y;
|
|---|
| 38 | };
|
|---|
| 39 |
|
|---|
| 40 | return force;
|
|---|
| 41 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.