How to Use Three.js, a Lightweight 3D Library, with Neo4j
Join the DZone community and get the full member experience.
Join For Freelast week we saw sigma.js , and as promised here is a graph visualization with three.js and neo4j . three.js is a lightweight 3d library, written by mr. doob and a small army of contributors.
the things you can do with three.js are amazing, and my little demo
here doesn’t give it justice, but nonetheless i’ll show you how to build
it.
we need to pass the nodes and relationships to three.js, one of the ways we can do that easily is with the
gon gem
, since we’re using sinatra, we’ll use the special
gon-sinatra gem
.
class app < sinatra::base register gon::sinatra def nodes neo = neography::rest.new cypher_query = " start node = node:nodes_index(type='user')" cypher_query << " return id(node), node" neo.execute_query(cypher_query)["data"].collect{|n| {"id" => n[0]}.merge(n[1]["data"])} end def edges neo = neography::rest.new cypher_query = " start source = node:nodes_index(type='user')" cypher_query << " match source -[rel]-> target" cypher_query << " return id(rel), id(source), id(target)" neo.execute_query(cypher_query)["data"].collect{|n| {"id" => n[0], "source" => n[1], "target" => n[2]} } end get '/' do neo = neography::rest.new gon.nodes = nodes gon.edges = edges erb :index end end
in our view we will add”include_gon” which ties our nodes and edges to our html page.
<!doctype html> <html lang="en"> <head> <title>three.js and neo4j</title> <%= include_gon %> <meta charset="utf-8"> <script type="text/javascript" src="three.js"></script> <link type="text/css" rel="stylesheet" href="neo_three.css"/> </head> <body> <script type="text/javascript" src="neo_three.js"></script> </body> </html>
if you view the source of index.html, you’ll see our nodes and edges.
window.gon = {}; gon.nodes=[{"id":1, "rotation_x":5.061454830783556, "name":"zfbushqe", "position_y":256, "position_x":658, "position_z":577, "rotation_y":3.543018381548489}, {"id":2, "rotation_x":4.572762640225143, "name":"afntayhh", "position_y":-22, "position_x":510, "position_z":404, "rotation_y":2.2689280275926285} ... gon.edges=[{"id":3,"source":1,"target":198}, {"id":2,"source":1,"target":39}, {"id":1,"source":1,"target":21} ... ]
to use three.js, we’ll need to create a camera, a scene and pick a renderer to use.
camera = new three.perspectivecamera( 75, window.innerwidth / window.innerheight, 1, 10000 ); camera.position.z = 100; scene = new three.scene(); scene.add( camera ); renderer = new three.canvasrenderer(); renderer.setsize( window.innerwidth, window.innerheight ); container.appendchild( renderer.domelement );
to create our nodes, we’ll use spheres and use a normal material. we’ll grab the nodes from gon and use their properties to position and set the orientation of our spheres.
var geometry = new three.spheregeometry( 50, 8, 7, false ); var material = new three.meshnormalmaterial(); group = new three.object3d(); for (n in gon.nodes) { var mesh = new three.mesh( geometry, material ); mesh.position.x = gon.nodes[n].position_x; mesh.position.y = gon.nodes[n].position_y; mesh.position.z = gon.nodes[n].position_z; mesh.rotation.x = gon.nodes[n].rotation_x; mesh.rotation.y = gon.nodes[n].rotation_y; mesh.matrixautoupdate = false; mesh.updatematrix(); group.add( mesh ); } scene.add( group );
for our edges, we’ll create simple lines of random colors between the source and target nodes.
for (n in gon.edges) { var line_segment = new three.geometry(); line_segment.vertices.push( new three.vertex( group.children[gon.edges[n].source - 1].position ) ); line_segment.vertices.push( new three.vertex( group.children[gon.edges[n].target - 1].position ) ); var line = new three.line( line_segment, new three.linebasicmaterial( { color: math.random() * 0xffffff, opacity: 0.5 } ) ); scene.add(line) }
we’ll animate our visualization by rendering the scene and controlling the camera with the mouse.
document.addeventlistener( 'mousemove', ondocumentmousemove, false ); function ondocumentmousemove(event) { mousex = event.clientx - windowhalfx; mousey = event.clienty - windowhalfy; } function animate() { requestanimationframe( animate ); render(); } function render() { camera.position.x += ( mousex - camera.position.x ) * .05; camera.position.y += ( - mousey + 200 - camera.position.y ) * .05; camera.lookat( scene.position ); renderer.render( scene, camera ); }
i’m going to skip the graph creation but all the code is available on github as usual, and you’ll definitively want to see this live on heroku .
if you want to learn more about three.js, check out these tutorial and these videos .
Opinions expressed by DZone contributors are their own.
Comments