Skip to content

Instantly share code, notes, and snippets.

@danpaulsmith
Created May 23, 2015 10:14
Show Gist options
  • Select an option

  • Save danpaulsmith/a8ea93a3e6a80ca9d701 to your computer and use it in GitHub Desktop.

Select an option

Save danpaulsmith/a8ea93a3e6a80ca9d701 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/qeqelabize
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="https://raw.githubusercontent.com/ayasdi/grapher/master/build/grapher-min.js"></script>
<script src="https://raw.githubusercontent.com/ayasdi/grapher-target/master/target-min.js"></script>
<style id="jsbin-css">
canvas#graph {
width: 100%;
height: 100%;
position: fixed;
}
</style>
</head>
<body>
<canvas id="graph"></canvas>
<script id="jsbin-javascript">
function getOffset(e) {
if (e.offsetX) return {
x: e.offsetX,
y: e.offsetY
};
var rect = e.target.getBoundingClientRect();
var x = e.clientX - rect.left,
y = e.clientY - rect.top;
return {
x: x,
y: y
};
}
// Generate some data
var network = {nodes: [], links: []},
width = window.innerWidth,
height = window.innerHeight,
numNodes = 10,
numLinks = 30,
i;
for (i = 0; i < numNodes; i++) {
network.nodes.push({
x: Math.random() * width,
y: Math.random() * height,
r: Math.random() * 10 + 5
});
}
for (i = 0; i < numLinks; i++) {
network.links.push({
from: Math.floor(Math.random() * numNodes),
to: Math.floor(Math.random() * numNodes),
});
}
// Create a grapher instance (width, height, options)
// The options are the same as PIXI's renderer options.
var grapher = new Grapher({
// Pass grapher the canvas
canvas: document.getElementById('graph')
});
// Give it the network data
grapher.data(network);
// Render the graph
grapher.play();
// When the user moves the mouse, we update the node's position
grapher.on('mousemove', function(e) {
var eOffset = getOffset(e);
var point = grapher.getDataPosition(eOffset.x, eOffset.y);
console.log('target node ' + grapher.target(point));
console.log('nearest node ' + grapher.nearest(point));
});
</script>
<script id="jsbin-source-css" type="text/css">canvas#graph {
width: 100%;
height: 100%;
position: fixed;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">function getOffset(e) {
if (e.offsetX) return {
x: e.offsetX,
y: e.offsetY
};
var rect = e.target.getBoundingClientRect();
var x = e.clientX - rect.left,
y = e.clientY - rect.top;
return {
x: x,
y: y
};
}
// Generate some data
var network = {nodes: [], links: []},
width = window.innerWidth,
height = window.innerHeight,
numNodes = 10,
numLinks = 30,
i;
for (i = 0; i < numNodes; i++) {
network.nodes.push({
x: Math.random() * width,
y: Math.random() * height,
r: Math.random() * 10 + 5
});
}
for (i = 0; i < numLinks; i++) {
network.links.push({
from: Math.floor(Math.random() * numNodes),
to: Math.floor(Math.random() * numNodes),
});
}
// Create a grapher instance (width, height, options)
// The options are the same as PIXI's renderer options.
var grapher = new Grapher({
// Pass grapher the canvas
canvas: document.getElementById('graph')
});
// Give it the network data
grapher.data(network);
// Render the graph
grapher.play();
// When the user moves the mouse, we update the node's position
grapher.on('mousemove', function(e) {
var eOffset = getOffset(e);
var point = grapher.getDataPosition(eOffset.x, eOffset.y);
console.log('target node ' + grapher.target(point));
console.log('nearest node ' + grapher.nearest(point));
});</script></body>
</html>
canvas#graph {
width: 100%;
height: 100%;
position: fixed;
}
function getOffset(e) {
if (e.offsetX) return {
x: e.offsetX,
y: e.offsetY
};
var rect = e.target.getBoundingClientRect();
var x = e.clientX - rect.left,
y = e.clientY - rect.top;
return {
x: x,
y: y
};
}
// Generate some data
var network = {nodes: [], links: []},
width = window.innerWidth,
height = window.innerHeight,
numNodes = 10,
numLinks = 30,
i;
for (i = 0; i < numNodes; i++) {
network.nodes.push({
x: Math.random() * width,
y: Math.random() * height,
r: Math.random() * 10 + 5
});
}
for (i = 0; i < numLinks; i++) {
network.links.push({
from: Math.floor(Math.random() * numNodes),
to: Math.floor(Math.random() * numNodes),
});
}
// Create a grapher instance (width, height, options)
// The options are the same as PIXI's renderer options.
var grapher = new Grapher({
// Pass grapher the canvas
canvas: document.getElementById('graph')
});
// Give it the network data
grapher.data(network);
// Render the graph
grapher.play();
// When the user moves the mouse, we update the node's position
grapher.on('mousemove', function(e) {
var eOffset = getOffset(e);
var point = grapher.getDataPosition(eOffset.x, eOffset.y);
console.log('target node ' + grapher.target(point));
console.log('nearest node ' + grapher.nearest(point));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment