Skip to content

Instantly share code, notes, and snippets.

@VisDockHub
Last active January 3, 2016 01:48
Show Gist options
  • Select an option

  • Save VisDockHub/8391155 to your computer and use it in GitHub Desktop.

Select an option

Save VisDockHub/8391155 to your computer and use it in GitHub Desktop.
SVG Image

This example is a VisDock integration into a static SVG image. The image consists of multiple layers of SVG path elements. Users can use various selection tools, lasso, rectangle, ellipse and lines to query parts of the image and use the Pan-and-Zoom tool to transform the image for their conveniences. The image was found here on Wikipedia.

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<link href="http://rawgithub.com/VisDockHub/NewVisDock/master/master/visdock.css" rel="stylesheet" type="text/css"/>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://rawgithub.com/VisDockHub/NewVisDock/master/master/visdock.js"></script>
<script src="http://rawgithub.com/VisDockHub/NewVisDock/master/master/2D.js"></script>
<script src="http://rawgithub.com/VisDockHub/NewVisDock/master/master/IntersectionUtilities.js"></script>
<script src="http://rawgithub.com/VisDockHub/NewVisDock/master/master/visdock.utils.js"></script>
<script>
VisDock.init("body", {width: 1000, height: 800});
var viewport = VisDock.getViewport();
var path_query = [];
var mouse_click = [];
var mouse_loc = [];
d3.xml("pic.svg", "image/svg+xml", function(xml) {
for (var i = 0; i < xml.documentElement.children.length; ){
viewport[0][0].appendChild(xml.documentElement.children[i])
}
})
d3.selectAll("#svg2").remove();
Panel.x = 200;
Panel.y = 200;
Panel.setTransform();
function compare(shape1, shape2, inclusive){
var hits = [];
for (var i = 0; i < shape2.length; i++){
if (i != 12 && i < 78 && i != 79 || i > 84){
var captured = shape1.intersectPath([shape2[i]], inclusive)
if (captured.length == 1){
hits.push(captured[0])
path_query[num].push(i)
}
}
}
return hits;
}
VisDock.eventHandler = {
getHitsPolygon : function(points, inclusive) {
var path = d3.selectAll("path")[0]
var shapebound = new createPolygon(points);
if (path_query[num] == undefined) path_query[num] = [];
return compare(shapebound, path, inclusive);
},
getHitsLine : function(points, inclusive) {
var shapebound = new createLine(points);
return shapebound.intersectPath(d3.selectAll("path")[0], inclusive)
},
getHitsEllipse : function(points, inclusive) {
var path = d3.selectAll("path")[0]
var shapebound = new createEllipse(points);
if (path_query[num] == undefined) path_query[num] = [];
return compare(shapebound, path, inclusive);
},
setColor : function(hits) {
for (var i = 0; i < hits.length; i++) {
var str = hits[i].getAttributeNS(null, "transform")
VisDock.utils.addPathLayer(hits[i], "fill: " + VisDock.color[num - 1] + "; opacity: 0.5");
QueryManager.layers[num - 1][i][0][0].setAttributeNS(null, "id", "Group" + (num - 1))
QueryManager.layers[num - 1][i][0][0].setAttributeNS(null, "transform", str)
}
setup();
},
changeColor : function(color, query, index) {
var vis = VisDock.utils.getQueryVisibility(index);
for (var i = 0; i < query.length; i++) {
query[i][0][0].setAttributeNS(null, "style", "fill: " + color + "; opacity: " + vis)
}
},
changeVisibility : function(vis, query) {
var color = VisDock.utils.getQueryColor(index);
for (var i = 0; i < query.length; i++) {
query[i][0][0].setAttributeNS(null, "style", "fill: " + color + "; opacity: " + vis)
}
},
removeColor : function(hits, index) {
for (var i = 0; i < hits.length; i++) {
hits[i].remove();
}
},
QueryClick : function(query, index) {
}
}
function setup(){
mouse_click[num-1] = false;
VisDock.startChrome()
var path = d3.selectAll("path")[0]
d3.selectAll("#Group" + (num-1)).on("mousedown", function() {
var str = this.getAttributeNS(null, "id").split("Group")[1]
var index = parseInt(str)
var group = this;
if (mouse_loc[index] == undefined){
mouse_loc[index] = d3.mouse(d3.selectAll("svg")[0][0]);
}
d3.selectAll("#Group" + index).attr("pointer-events", "none")
mouse_click[index] = true;
d3.selectAll("svg").on("mousemove", function(){
if (mouse_click[index]){
var displace_x = d3.mouse(d3.selectAll("svg")[0][0])[0] - mouse_loc[index][0];
var displace_y = d3.mouse(d3.selectAll("svg")[0][0])[1] - mouse_loc[index][1];
for (var j = 0; j<path_query[index].length; j++){
var n = path_query[index][j];
path[n].setAttributeNS(null, "transform", "translate(" + displace_x + "," + displace_y + ")")
}
d3.selectAll("#Group" + index)
.attr("transform", "translate(" + displace_x + "," + displace_y + ")")
}
})
d3.selectAll("svg").on("mouseup", function(){
mouse_click[index] = false;
d3.selectAll("#Group" + index).attr("pointer-events", "visiblePainted")
})
})
VisDock.finishChrome();
}
BirdView.init(viewport, 1050, 850)
d3.select(self.frameElement).style("width", "1050px")
d3.select(self.frameElement).style("height", "850px")
</script>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment