Created
February 26, 2015 05:11
-
-
Save deanshub/2ee8d3aee5f325c8c4aa to your computer and use it in GitHub Desktop.
sigma.js plugin for multiple selection of nodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
if (typeof sigma === 'undefined') | |
throw 'sigma is not declared'; | |
sigma.utils.pkg('sigma.plugins'); | |
var startPosition; | |
var WIDTH; | |
var HEIGHT; | |
var ctx; | |
var gCanvas; | |
var graph; | |
var callback; | |
function mousedown(e){ | |
startPosition={x:e.layerX, y:e.layerY}; | |
gCanvas.addEventListener('mousemove',mousemove); | |
} | |
function mouseup(e){ | |
gCanvas.removeEventListener('mousemove',mousemove); | |
gCanvas.removeEventListener('mousedown',mousedown); | |
gCanvas.removeEventListener('mouseup',mouseup); | |
clear(ctx); | |
graph.settings('mouseEnabled', true); | |
callback(null, getNodesInArea(startPosition.x, startPosition.y, e.layerX, e.layerY)); | |
} | |
function getNodesInArea(x1, y1, x2, y2){ | |
var nodesInArea = []; | |
var startX; | |
var endX; | |
var startY; | |
var endY; | |
if (x1 > x2){ | |
startX = x2; | |
endX = x1; | |
}else{ | |
startX = x1; | |
endX = x2; | |
} | |
if (y1 > y2){ | |
startY = y2; | |
endY = y1; | |
}else{ | |
startY = y1; | |
endY = y2; | |
} | |
graph.camera.quadtree._cache.result.forEach(function(node){ | |
var nodeX = node['renderer1:x']; | |
var nodeY = node['renderer1:y']; | |
if ((nodeX > startX) && (nodeX < endX) && | |
(nodeY > startY) && (nodeY < endY)){ | |
nodesInArea.push(node); | |
} | |
}); | |
return nodesInArea; | |
} | |
function mousemove(e){ | |
clear(ctx); | |
ctx.beginPath(); | |
ctx.lineWidth='1'; | |
ctx.setLineDash([6]); | |
ctx.strokeStyle='black'; | |
ctx.rect(startPosition.x, startPosition.y, e.layerX - startPosition.x, e.layerY - startPosition.y); | |
ctx.stroke(); | |
} | |
sigma.plugins.activateMouseEvents = function(s, cb) { | |
if (!s){ | |
cb('graph not supplied'); | |
}else{ | |
var renderer = s.renderers[0]; | |
var container = renderer.container; | |
graph = s; | |
callback = cb; | |
graph.settings('mouseEnabled', false); | |
gCanvas = container.lastChild; | |
HEIGHT = gCanvas.height; | |
WIDTH = gCanvas.width; | |
ctx = gCanvas.getContext('2d'); | |
gCanvas.addEventListener('mousedown',mousedown); | |
gCanvas.addEventListener('mouseup',mouseup); | |
} | |
}; | |
function clear(c) { | |
c.clearRect(0, 0, WIDTH, HEIGHT); | |
} | |
}).call(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment