Created
April 10, 2018 22:09
-
-
Save cjwinchester/92bf43e8020879bd11eb17a9faa9928e to your computer and use it in GitHub Desktop.
pulsing dots on an old d3 map ftw
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<link rel="stylesheet" href="styles.css" /> | |
</head> | |
<body> | |
<div id="map"></div> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.1/d3.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-queue/3.0.7/d3-queue.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/3.0.2/topojson.min.js"></script> | |
<script src="scripts.js"></script> | |
</body> | |
</html> |
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
var d3_map = d3.select('#map'); | |
var aspect = 960 / 500; | |
var width = d3_map.node().getBoundingClientRect().width; | |
var height = width / aspect; | |
var projection = d3.geo.albersUsa() | |
.scale(width) | |
.translate([width/2, height/2]); | |
var path = d3.geo.path() | |
.projection(projection); | |
var svg = d3_map.append('svg') | |
.attr('width', width) | |
.attr('height', height); | |
d3.queue() | |
.defer(d3.json, 'us.json') | |
.await(ready); | |
function ready(error, us) { | |
if (error) throw error; | |
var dots = [ | |
{'city': 'Colorado Springs, CO', 'lat': 38.875868, 'lng': -104.8984438} | |
] | |
svg.insert('path', '.graticule') | |
.datum(topojson.feature(us, us.objects.land)) | |
.attr('class', 'land') | |
.attr('d', path); | |
// draw the state boundaries | |
svg.insert('path', '.graticule') | |
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })) | |
.attr('class', 'state-boundary') | |
.attr('d', path); | |
// draw the map bubbles | |
svg.selectAll('circle') | |
.data(dots) | |
.enter() | |
.append('circle') | |
.attr('cx', function (d) { return projection([d.lng, d.lat])[0]; }) | |
.attr('cy', function (d) { return projection([d.lng, d.lat])[1]; }) | |
.attr('class', 'dot') | |
.attr('r', 7) | |
.each(pulse); | |
function pulse() { | |
var circle = svg.selectAll('circle'); | |
(function repeat() { | |
circle = circle.transition() | |
.duration(1000) | |
.attr('r', 10) | |
.ease('sine') | |
.transition() | |
.duration(1000) | |
.attr('r', 7) | |
.each('end', repeat); | |
})(); | |
} | |
// https://davidwalsh.name/javascript-debounce-function | |
function debounce(func, wait, immediate) { | |
var timeout; | |
return function() { | |
var context = this, args = arguments; | |
var later = function() { | |
timeout = null; | |
if (!immediate) func.apply(context, args); | |
}; | |
var callNow = immediate && !timeout; | |
clearTimeout(timeout); | |
timeout = setTimeout(later, wait); | |
if (callNow) func.apply(context, args); | |
}; | |
} | |
var resize = debounce(function() { | |
var width = d3_map.node().getBoundingClientRect().width; | |
var height = width / aspect; | |
svg.attr('width', width) | |
.attr('height', height); | |
projection | |
.scale(width) | |
.translate([width / 2, height / 2]); | |
d3.select('.state-boundary') | |
.attr('d', path); | |
d3.selectAll('.land') | |
.attr('d', path); | |
d3.selectAll('.dot') | |
.attr('cx', function (d) { return projection([d.lng, d.lat])[0]; }) | |
.attr('cy', function (d) { return projection([d.lng, d.lat])[1]; }); | |
}, 250); | |
window.addEventListener('resize', resize); | |
} |
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
.land { | |
fill: #fafafa; | |
stroke: #bbb; | |
} | |
.state-boundary { | |
fill: none; | |
stroke: #bbb; | |
stroke-linejoin: round; | |
stroke-linecap: round; | |
} | |
.dot { | |
fill: red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment