Skip to content

Instantly share code, notes, and snippets.

@VisDockHub
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

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

Select an option

Save VisDockHub/8973231 to your computer and use it in GitHub Desktop.
Raindrops Example

The raindrops example shows the radial geometry using the rotational transformation of SVG objects. The VisDock toolkit has been integrated into the raindrops example built with D3.js (found here) by M. Bostock. Using selection tools, users can query path elements that are transformed rotationally.

<!DOCTYPE html>
<meta charset="utf-8">
<title>Raindrops</title>
<style>
body {
background: #012;
}
path {
stroke: #fff;
stroke-opacity: .5;
}
</style>
<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>
var width = 960,
height = 500;
VisDock.init("body", {width: 960, height: 700});
var viewport = VisDock.getViewport();
var svg = viewport;
Panel.x = width / 2;
Panel.y = height / 2;
Panel.setTransform();
/*var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
*/
var gradient = svg.append("defs").append("linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "20%")
.attr("x2", "20%")
.attr("y2", "100%");
gradient.append("stop")
.attr("offset", "20%")
.attr("stop-color", "#ccf");
gradient.append("stop")
.attr("offset", "50%")
.attr("stop-color", "#1C425C");
gradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", "#19162B");
// could use transparent gradient overlay to vary raindrop color
svg.selectAll("path")
.data(d3.range(358))
.enter().append("path")
.attr("fill", "url(#gradient)")
.attr("d", function() { return raindrop(10 + Math.random() * 200); })
.attr("transform", function(d) {
return "rotate(" + d + ")"
+ "translate(" + (height / 4 + Math.random() * height / 6) + ",0)"
+ "rotate(90)";
});
// size is linearly proportional to square pixels (not exact, yet)
function raindrop(size) {
var r = Math.sqrt(size / Math.PI);
return "M" + r + ",0"
+ "A" + r + "," + r + " 0 1,1 " + -r + ",0"
+ "C" + -r + "," + -r + " 0," + -r + " 0," + -3*r
+ "C0," + -r + " " + r + "," + -r + " " + r + ",0"
+ "Z";
}
VisDock.eventHandler = {
getHitsPolygon : function(points, inclusive) {
var shapebound = new createPolygon(points);
return shapebound.intersectPath(d3.selectAll("path")[0], inclusive)
},
getHitsLine : function(points, inclusive) {
var shapebound = new createLine(points);
return shapebound.intersectPath(d3.selectAll("path")[0], inclusive)
},
getHitsEllipse : function(points, inclusive) {
var shapebound = new createEllipse(points);
return shapebound.intersectPath(d3.selectAll("path")[0], inclusive)
},
setColor : function(hits) {
QueryManager.layers[num - 1] = [];
for (var i = 0; i < hits.length; i++) {
var str = hits[i].getAttributeNS(null, "transform");
var d = hits[i].getAttributeNS(null, "d")
QueryManager.layers[num - 1][i] = d3.select(hits[i].parentNode).append("path").attr("transform", str)
.attr("d", d)
.attr("style", "fill: " + VisDock.color[num - 1] + "; opacity: 0.5; pointer-events: none")
}
},
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, index) {
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) {
}
}
BirdView.init(viewport, 960, 700)
d3.select(self.frameElement).style("width", "960px")
d3.select(self.frameElement).style("height", "700px")
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment