Skip to content

Instantly share code, notes, and snippets.

@pstuffa
Last active August 29, 2015 14:17
Show Gist options
  • Save pstuffa/baa25d828cf1512307f4 to your computer and use it in GitHub Desktop.
Save pstuffa/baa25d828cf1512307f4 to your computer and use it in GitHub Desktop.
Rotating Icosahedron-do
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
fill: #f88e22;
stroke: #000;
stroke-width: 2px;
}
</style>
<body>
<svg id="mySvg" width="10" height="10">
<defs id="mdef">
<pattern id="image" x="0" y="0" height="10" width="10">
<image x="-50" y="-50" width="400" height="400" xlink:href="http://a.espncdn.com/combiner/i?img=/i/headshots/nba/players/full/3026.png&w=350&h=254"></image>
</pattern>
</defs>
</svg>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var velocity = [.01, .01],
t0 = Date.now();
var projection = d3.geo.orthographic()
.scale(height / 2 - 10);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var face = svg.selectAll("path")
.data(icosahedronFaces)
.enter().append("path")
// .style("fill", "url(#image)")
.each(function(d) { d.polygon = d3.geom.polygon(d.map(projection)); });
d3.timer(function() {
var time = Date.now() - t0;
projection.rotate([time * velocity[0], time * velocity[1]]);
face
.each(function(d) { d.forEach(function(p, i) { d.polygon[i] = projection(p); }); })
.style("display", function(d) { return d.polygon.area() > 0 ? null : "none"; })
.style("fill", "url(#image)")
.attr("d", function(d) { return "M" + d.polygon.join("L") + "Z"; });
});
function icosahedronFaces() {
var faces = [],
y = Math.atan2(1, 2) * 180 / Math.PI;
for (var x = 0; x < 360; x += 72) {
faces.push(
[[x + 0, -90], [x + 0, -y], [x + 72, -y]],
[[x + 36, y], [x + 72, -y], [x + 0, -y]],
[[x + 36, y], [x + 0, -y], [x - 36, y]],
[[x + 36, y], [x - 36, y], [x - 36, 90]]
);
}
return faces;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment