Published
Edited
Mar 24, 2021
2 stars
Insert cell
Insert cell
Insert cell
{
await visibility();
const draw = render(); // return the drawing function from the render() method
return renderGif(invalidation, draw, 1500, { preview: draw(0), pause: 1000 }); // render the gif
}
Insert cell
loop_render = () => {
// Create the svg
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.style("text-anchor", "middle")
.style('font-family', 'sans-serif')
.style("font-size", "12px");

// Add a white background
const rect = svg
.append("rect")
.attr("width", width)
.attr("height", height)
.style("fill", "white");

// Add a path for each projection
const paths = svg
.selectAll("path")
.data(projections, d => d.name)
.join("path")
.attr("d", d => {
// Define a projection
const projection = d3
.geoProjection(d.value)
.fitExtent(
[[10, 10], [width, height]],
topojson.feature(shapefile, shapefile.objects.countries)
);

// Set the projection and apply it to the data
const path_d = d3.geoPath().projection(projection)(path_data);
return path_d;
})
.style("fill", "none")
.style("stroke-width", .1);

// Return a function that takes in t and returns the node
return t => {
const _t = t < .5 ? t * 2 : (1 - t) * 2;
// Set the radius based on t, which will go from 0 to 1
paths.attr("stroke", (d, i) =>
(i + 1) / projections.length <= _t ? "black" : "white"
);
return svg.node();
};
}
Insert cell
{
await visibility();
const draw = loop_render(); // return the drawing function from the render() method
return renderGif(invalidation, draw, 3000, { preview: draw(0), pause: 0 }); // render the gif
}
Insert cell
Insert cell
path_data = topojson.mesh(shapefile)
Insert cell
import { projections } from "@d3/projection-transitions"
Insert cell
d3 = require("d3", "d3-geo@2", "d3-geo-projection@3")
Insert cell
// Load shapefile
shapefile = d3.json(
"https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json"
)
Insert cell
topojson = require("topojson-client@3")
Insert cell
height = 500
Insert cell
import { renderGif } from '@mootari/gif'
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more