Skip to content

Instantly share code, notes, and snippets.

@erohinaelena
Last active February 15, 2017 13:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erohinaelena/eef385d60c25c5525bf3 to your computer and use it in GitHub Desktop.
Save erohinaelena/eef385d60c25c5525bf3 to your computer and use it in GitHub Desktop.
Old star map
<html>
<head>
<meta charset="utf-8">
<link href="stars.css" rel="stylesheet">
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="stars.js"></script>
<script>d3.select(self.frameElement).style("height", "840px")</script>
</body>
</html>
body {
margin: 0;
background: #fffab7;
}
text {
font: 12px sans-serif;
fill: #000;
font-size: 0.75em;
text-anchor: middle;
}
.bg {
background: #fffab7;
fill: #fffab7;
}
line {
stroke: #000;
stroke-width: 0.4;
}
line.zodiac {
stroke: #f18700;
stroke-width: 0.8;
}
.map-line {
stroke-width: 0.8;
}
.zodiac-bg {
stroke: #fffab7;
stroke-width: 2.5;
}
var width = 960,
height = 840
var projection = d3.geo.azimuthalEquidistant()
.scale(width / 5.5)
.translate([0, 0])
.rotate([0, 270])
var starsContainer = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')')
d3.json("/erohinaelena/raw/ec635d68e8bf55586d40/starData.json", function(error, data) {
var groups = starsContainer.selectAll('g')
.data(data)
.enter()
.append('g')
function addZodiacName(g, name, x, y) {
var rotation = 'rotate(' +
(Math.atan2(y, x) * 180 / Math.PI + 90) + ',' +
x + ',' +
y + ')';
g.append('text')
.classed('zodiac-bg', true)
g.append('text')
g.selectAll('text')
.text(name)
.attr('x', x)
.attr('y', y)
.attr('transform', rotation)
}
function distance(x, y) {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))
}
groups.each(function (constellation) {
var name = constellation.name
var projections = constellation.stars.map(function (star) {
var p = projection([star.ra, star.dec])
var out = (distance(p[0], p[1]) > width / 2.3)
return {
mag: star.mag,
projection: p,
out: out
}
})
var x = d3.mean(projections, function (d) { return d.projection[0] = -d.projection[0] })
var y = d3.mean(projections, function (d) { return d.projection[1] })
var g = d3.select(this)
projections = projections.filter(function (star) {
return !star.out && star.mag <= 6
})
if (distance(x,y) < width / 2.5 || constellation.zodiac){
var lines = constellation.lines.map(function (line) {
var p1 = projection([line.ra1, line.dec1])
var p2 = projection([line.ra2, line.dec2])
p1[0] = -p1[0]
p2[0] = -p2[0]
var out = distance(p1[0], p1[1]) > width / 2.4
return {line: [p1, p2], zodiac: constellation.zodiac, out: out}
})
var starLines = g.selectAll('line')
.data(lines)
.enter()
.append('line')
starLines
.attr('x1', function (d) { return d.line[0][0] })
.attr('y1', function (d) { return d.line[0][1] })
.attr('x2', function (d) { return d.line[1][0] })
.attr('y2', function (d) { return d.line[1][1] })
.style('stroke',function (d) { if (d.out && !d.zodiac) return 'none'})
.classed('zodiac', function (d) { return d.zodiac; })
}
var circles = g.selectAll('circle')
.data(projections)
.enter()
circles.append('circle')
.classed('bg', true)
.attr('cx', function (d) { return d.projection[0] })
.attr('cy', function (d) { return d.projection[1] })
.attr('r', function (d) { return 1.2 + Math.pow(1.2, 5 - d.mag) })
circles.append('circle')
.attr('cx', function (d) { return d.projection[0] })
.attr('cy', function (d) { return d.projection[1] })
.attr('r', function (d) { return Math.pow(1.2, 5 - d.mag) })
.attr('fill', 'black')
if (constellation.zodiac)
addZodiacName(g, name, x, y)
})
})
for (var i = 0; i < 24; i++){
var outerRadius = 415
var innerRadius = 50
starsContainer.append('line')
.classed('map-line', true)
.attr('x1', outerRadius * Math.cos(i * Math.PI / 12))
.attr('y1', outerRadius * Math.sin(i * Math.PI / 12))
.attr('x2', innerRadius * Math.cos(i * Math.PI / 12))
.attr('y2', innerRadius * Math.sin(i * Math.PI / 12))
}
for (i = 0; i < 6; i++){
starsContainer.append('circle')
.attr('cx', 0)
.attr('cy', 0)
.attr('r', innerRadius + i * (outerRadius - innerRadius) / 5)
.attr('fill','none')
.attr('stroke','black')
.attr('stroke-width',0.8)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment