(function () { var pageWidth, pageHeight, proj, zoomed, zoom, g, path, circleIt, randomCircle, colours, cstGeoJson, toDots, toCountries; pageWidth = window.innerWidth; pageHeight = window.innerHeight; iteration = 0; var transformFunction; proj = new d3.geo.mercator().center([ -105, 51 ]).rotate([ 0, 0, 0 ]).translate([ pageWidth / 2, pageHeight / 2 ]).scale(1 << 8); zoomed = function () { var transform; transform = 'translate(' + zoom.translate() + ') scale(' + zoom.scale() + ')'; return d3.select('svg#map-canvas g').attr('transform', transform).selectAll('path').style('stroke-width', function () { return 3 / zoom.scale(); }); }; zoom = new d3.behavior.zoom().on('zoom', zoomed); g = d3.select('div#map').append('svg').attr('id', 'map-canvas').call(zoom).append('g'); path = d3.geo.path().projection(proj); circleIt = function (it, x, y, r) { var from, circle; from = path(it); circle = 'm ' + (x - r) + ' ' + y + ' a ' + r + ' ' + r + ' 0 1 0 ' + 2 * r + ' 0 a ' + r + ' ' + r + ' 0 1 0 ' + -2 * r + ' 0'; return polymorph.transpose(from, circle); }; rectangleIt = function(it, x,y,r) { var from,rectangle; from = path(it); r=r*6; rectangle = "m " + (x-r) + ' ' + y + 'h'+r+'v'+r+"h"+ (-r) + "v"+(-r)+"Z"; console.log(from,rectangle); return polymorph.transpose(from,rectangle); }; randomCircle = function (it) { var p, w, h, x, y, r; p = 20; w = pageWidth - 2 * p; h = pageHeight - 2 * p; x = Math.random() * w + p; y = Math.random() * h + p; r = Math.log(it.properties.AREA) * 3; return transformFunction(it,x,y,r); }; transformFunction = circleIt; colours = { Canada: '#CACACA', Mexico: '#8C8787', 'United States': '#4E4E4E' }; cstGeoJson = topojson.feature(countries, countries.objects.countries); g.selectAll('path').data(cstGeoJson.features.filter(function (it) { var name, area, reg; name = it.properties.name; area = it.properties.AREA; reg = /(Canada|Mexico|United States)/; if (name.match(reg) && area > 1) { return true; } else { return false; } })).enter().append('path').attr('d', randomCircle).style('fill', function (it) { return colours[it.properties.name]; }); toDots = function () { var l, i; l = d3.selectAll('path')[0].length; i = 1; iteration ++; return d3.selectAll('path').transition().delay(function () { console.log('00'); if (iteration % 2 === 0) { transformFunction = circleIt; } else { transformFunction = rectangleIt; } return Math.random() * 1000; }).duration(2000).attr('d', randomCircle).each('end', function () { if (l === i++) { return toCountries(); } }); }; toCountries = function () { var l, i; l = d3.selectAll('path')[0].length; i = 1; return d3.selectAll('path').transition().delay(function () { return Math.random() * 1000; }).duration(2000).attr('d', path).each('end', function () { if (l === i++) { return toDots(); } }); }; toCountries(); }.call(this));