meshviewer/lib/forcegraph.js

289 lines
7.9 KiB
JavaScript
Raw Normal View History

define(['d3-selection', 'd3-force', 'd3-zoom', 'd3-drag', 'd3-timer', 'd3-ease', 'd3-interpolate', 'utils/math', 'forcegraph/draw'],
function (d3Selection, d3Force, d3Zoom, d3Drag, d3Timer, d3Ease, d3Interpolate, math, draw) {
2017-02-23 21:32:25 +00:00
'use strict';
2015-03-31 15:22:36 +00:00
return function (linkScale, sidebar) {
2017-02-23 21:32:25 +00:00
var self = this;
var el;
var canvas;
var ctx;
var force;
var forceLink;
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
var transform = d3Zoom.zoomIdentity;
var intNodes = [];
var dictNodes = {};
var intLinks = [];
var movetoTimer;
2017-10-30 02:14:08 +00:00
var initial = 1.8;
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
var NODE_RADIUS_DRAG = 10;
var NODE_RADIUS_SELECT = 15;
var LINK_RADIUS_SELECT = 12;
var ZOOM_ANIMATE_DURATION = 350;
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
var ZOOM_MIN = 1 / 8;
var ZOOM_MAX = 3;
2015-07-08 16:03:19 +00:00
2017-10-30 02:14:08 +00:00
var FORCE_ALPHA = 0.01;
2015-03-31 15:22:36 +00:00
2017-02-23 21:32:25 +00:00
draw.setTransform(transform);
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
function resizeCanvas() {
canvas.width = el.offsetWidth;
canvas.height = el.offsetHeight;
draw.setMaxArea(canvas.width, canvas.height);
2015-04-15 23:29:17 +00:00
}
function transformPosition(p) {
transform.x = p.x;
transform.y = p.y;
transform.k = p.k;
}
function moveTo(callback, forceMove) {
clearTimeout(movetoTimer);
if (!forceMove && force.alpha() > 0.3) {
movetoTimer = setTimeout(function timerOfMoveTo() {
moveTo(callback);
}, 300);
return;
}
var result = callback();
var x = result[0];
var y = result[1];
var k = result[2];
var end = { k: k };
2017-12-25 15:12:44 +00:00
end.x = (canvas.width + sidebar.getWidth()) / 2 - x * k;
end.y = canvas.height / 2 - y * k;
var start = { x: transform.x, y: transform.y, k: transform.k };
var interpolate = d3Interpolate.interpolateObject(start, end);
var timer = d3Timer.timer(function (t) {
if (t >= ZOOM_ANIMATE_DURATION) {
timer.stop();
return;
}
var v = interpolate(d3Ease.easeQuadInOut(t / ZOOM_ANIMATE_DURATION));
transformPosition(v);
window.requestAnimationFrame(redraw);
});
2015-04-10 18:47:11 +00:00
}
2017-02-23 21:32:25 +00:00
function onClick() {
if (d3Selection.event.defaultPrevented) {
return;
}
2015-03-31 19:18:06 +00:00
2017-02-23 21:32:25 +00:00
var e = transform.invert([d3Selection.event.clientX, d3Selection.event.clientY]);
var n = force.find(e[0], e[1], NODE_RADIUS_SELECT);
2015-03-31 19:29:47 +00:00
2017-02-23 21:32:25 +00:00
if (n !== undefined) {
router.fullUrl({ node: n.o.node_id });
2017-02-23 21:32:25 +00:00
return;
}
2015-04-10 18:47:11 +00:00
2017-02-23 21:32:25 +00:00
e = { x: e[0], y: e[1] };
2017-02-23 21:32:25 +00:00
var closedLink;
var radius = LINK_RADIUS_SELECT;
intLinks
.forEach(function (d) {
var distance = math.distanceLink(e, d.source, d.target);
if (distance < radius) {
closedLink = d;
radius = distance;
}
2017-02-23 21:32:25 +00:00
});
2017-02-23 21:32:25 +00:00
if (closedLink !== undefined) {
2017-03-05 11:29:21 +00:00
router.fullUrl({ link: closedLink.o.id });
}
}
2017-02-23 21:32:25 +00:00
function redraw() {
ctx.save();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.translate(transform.x, transform.y);
ctx.scale(transform.k, transform.k);
2015-04-10 11:50:39 +00:00
2017-02-23 21:32:25 +00:00
intLinks.forEach(draw.drawLink);
intNodes.forEach(draw.drawNode);
2015-04-10 11:50:39 +00:00
2017-02-23 21:32:25 +00:00
ctx.restore();
}
2015-04-10 11:50:39 +00:00
2017-02-23 21:32:25 +00:00
el = document.createElement('div');
el.classList.add('graph');
2015-04-19 00:16:06 +00:00
2017-02-23 21:32:25 +00:00
forceLink = d3Force.forceLink()
.distance(function (d) {
2017-10-31 13:22:00 +00:00
if (d.o.type.indexOf('vpn') === 0) {
2017-02-23 21:32:25 +00:00
return 0;
}
return 75;
})
.strength(function (d) {
2017-10-31 13:22:00 +00:00
if (d.o.type.indexOf('vpn') === 0) {
2017-02-23 21:32:25 +00:00
return 0.02;
}
return Math.max(0.5, d.o.source_tq);
});
2017-02-23 21:32:25 +00:00
var zoom = d3Zoom.zoom()
.scaleExtent([ZOOM_MIN, ZOOM_MAX])
.on('zoom', function () {
transform = d3Selection.event.transform;
draw.setTransform(transform);
redraw();
});
2015-07-30 21:47:58 +00:00
2017-02-23 21:32:25 +00:00
force = d3Force.forceSimulation()
.force('link', forceLink)
.force('charge', d3Force.forceManyBody())
.force('x', d3Force.forceX().strength(0.02))
.force('y', d3Force.forceY().strength(0.02))
.force('collide', d3Force.forceCollide())
.on('tick', redraw)
2017-10-30 02:14:08 +00:00
.alphaDecay(0.025);
2017-02-23 21:32:25 +00:00
var drag = d3Drag.drag()
.subject(function () {
var e = transform.invert([d3Selection.event.x, d3Selection.event.y]);
var n = force.find(e[0], e[1], NODE_RADIUS_DRAG);
if (n !== undefined) {
n.x = d3Selection.event.x;
n.y = d3Selection.event.y;
return n;
}
return undefined;
})
.on('start', function () {
if (!d3Selection.event.active) {
force.alphaTarget(FORCE_ALPHA).restart();
}
d3Selection.event.subject.fx = transform.invertX(d3Selection.event.subject.x);
d3Selection.event.subject.fy = transform.invertY(d3Selection.event.subject.y);
})
.on('drag', function () {
d3Selection.event.subject.fx = transform.invertX(d3Selection.event.x);
d3Selection.event.subject.fy = transform.invertY(d3Selection.event.y);
})
.on('end', function () {
if (!d3Selection.event.active) {
force.alphaTarget(0);
}
d3Selection.event.subject.fx = null;
d3Selection.event.subject.fy = null;
});
2015-04-10 18:47:11 +00:00
2017-02-23 21:32:25 +00:00
canvas = d3Selection.select(el)
.append('canvas')
.on('click', onClick)
.call(drag)
.call(zoom)
.node();
ctx = canvas.getContext('2d');
draw.setCTX(ctx);
window.addEventListener('resize', function () {
resizeCanvas();
redraw();
});
self.setData = function setData(data) {
2017-10-31 12:32:39 +00:00
intNodes = data.nodes.online.map(function (d) {
var e = dictNodes[d.node_id];
if (!e) {
2017-02-23 21:32:25 +00:00
e = {};
dictNodes[d.node_id] = e;
2017-02-23 21:32:25 +00:00
}
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
e.o = d;
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
return e;
2017-10-30 02:14:08 +00:00
});
2015-04-15 23:29:17 +00:00
intLinks = data.links.filter(function (d) {
2017-10-31 12:32:39 +00:00
return data.nodeDict[d.source.node_id].is_online && data.nodeDict[d.target.node_id].is_online;
}).map(function (d) {
return {
o: d,
source: dictNodes[d.source.node_id],
target: dictNodes[d.target.node_id],
color: linkScale(d.source_tq),
color_to: linkScale(d.target_tq)
};
2017-02-23 21:32:25 +00:00
});
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
force.nodes(intNodes);
forceLink.links(intLinks);
2015-04-15 23:29:17 +00:00
2017-10-30 02:14:08 +00:00
force.alpha(initial).velocityDecay(0.15).restart();
if (initial === 1.8) {
initial = 0.5;
}
2017-02-23 21:32:25 +00:00
resizeCanvas();
};
2015-07-07 08:19:38 +00:00
2017-02-23 21:32:25 +00:00
self.resetView = function resetView() {
moveTo(function calcToReset() {
draw.setHighlight(null);
return [0, 0, (ZOOM_MIN + config.forceGraph.zoomModifier) / 2];
}, true);
};
2017-02-23 21:32:25 +00:00
self.gotoNode = function gotoNode(d) {
moveTo(function calcToNode() {
2017-11-01 10:43:20 +00:00
draw.setHighlight({ type: 'node', id: d.node_id });
var n = dictNodes[d.node_id];
if (n) {
return [n.x, n.y, (ZOOM_MAX + 1) / 2];
2017-02-23 21:32:25 +00:00
}
return resetView();
});
2017-02-23 21:32:25 +00:00
};
2015-04-03 00:32:32 +00:00
2017-02-23 21:32:25 +00:00
self.gotoLink = function gotoLink(d) {
moveTo(function calcToLink() {
draw.setHighlight({ type: 'link', id: d[0].id });
2017-11-01 10:43:20 +00:00
var l = intLinks.find(function (link) {
return link.o.id === d[0].id;
2017-11-01 10:43:20 +00:00
});
if (l) {
return [(l.source.x + l.target.x) / 2, (l.source.y + l.target.y) / 2, (ZOOM_MAX / 2) + ZOOM_MIN];
2017-02-23 21:32:25 +00:00
}
return resetView();
});
2017-02-23 21:32:25 +00:00
};
2015-04-07 23:21:52 +00:00
2017-03-05 11:29:21 +00:00
self.gotoLocation = function gotoLocation() {
// ignore
};
2017-02-23 21:32:25 +00:00
self.destroy = function destroy() {
force.stop();
canvas.parentNode.removeChild(canvas);
2017-02-23 21:32:25 +00:00
force = null;
2017-02-23 21:32:25 +00:00
if (el.parentNode) {
el.parentNode.removeChild(el);
}
2017-02-23 21:32:25 +00:00
};
2017-02-23 21:32:25 +00:00
self.render = function render(d) {
d.appendChild(el);
resizeCanvas();
};
2015-07-11 22:11:18 +00:00
2017-02-23 21:32:25 +00:00
return self;
};
2017-02-23 21:32:25 +00:00
});