HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
If the stylesheet you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<p class="title">Stacking & Animating Formula 1 Circuits in D3</p>
<p>
<span style="font-weight: bold; background-color: yellow;">Part 1</span>
|
<a href="https://codepen.io/jwasilgeo/pen/EpvrdB" target="_blank">Part 2</a>
|
<a href="https://codepen.io/jwasilgeo/pen/ajKEJQ" target="_blank">Part 3</a>
</p>
<div>
<svg class="orthographic-map" viewBox="0 0 400 400"></svg>
</div>
<p class="info">
find & stack... <span class="circuit-name"></span>
</p>
<div>
<svg class="stacking-map" viewBox="0 0 300 300"></svg>
</div>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/topojson-client@3.0.0/dist/topojson-client.min.js"></script>
<script src="https://unpkg.com/topojson-simplify@3.0.2/dist/topojson-simplify.min.js"></script>
body {
font-family: sans-serif;
margin: 10px auto;
text-align: center;
font-size: 0.95em;
}
svg {
stroke-linejoin: round;
stroke-linecap: round;
}
svg.orthographic-map {
max-width: 40vmin;
}
svg.stacking-map {
max-width: 45vmin;
}
svg.orthographic-map .circuit-path {
stroke-width: 4px;
stroke: steelblue;
fill: steelblue;
opacity: 0.6;
}
svg.stacking-map .circuit-path {
stroke-width: 1.25px;
stroke: steelblue;
fill: none;
opacity: 0.4;
transform-origin: center;
}
svg .land {
stroke-width: 0.5px;
stroke: gray;
fill: whitesmoke;
}
svg .sphere {
stroke-width: 0.5px;
stroke: gray;
fill: none;
}
p {
margin: 0 0 5px 0;
}
p.title {
font-weight: bold;
}
.info {
margin: 0;
}
.circuit-name {
font-weight: bold;
}
Promise.all([
d3.json(
"https://gist.githubusercontent.com/jwasilgeo/21a6a6b67e9dc14f28fa73bb2beb4a6e/raw/e29f0ef8f8d9832598b5429d6c5e2c6fa9d33666/F1_Circuits_2018_topojson.json"
),
d3.json("https://unpkg.com/world-atlas@1.1.4/world/110m.json")
]).then(function(loadedTopoJsonResults) {
var f1CircuitsTopoJson = loadedTopoJsonResults[0];
var f1CircuitsGeoJson = topojson.feature(
f1CircuitsTopoJson,
f1CircuitsTopoJson.objects.F1_Circuits_2018
);
var worldTopoJson = topojson.simplify(
topojson.presimplify(loadedTopoJsonResults[1]),
0.5
);
var worldGeoJson = topojson.feature(
worldTopoJson,
worldTopoJson.objects.land
);
var orthographicMap = createOrthographicMap(f1CircuitsGeoJson, worldGeoJson);
rotateMaps(f1CircuitsGeoJson, orthographicMap, 0);
});
function createOrthographicMap(f1CircuitsGeoJson, worldGeoJson) {
var svg = d3.select("svg.orthographic-map");
var dimensions = svg.attr("viewBox").split(" ");
var width = dimensions[2];
var height = dimensions[3];
var projection = d3.geoOrthographic().fitSize([width, height], {
type: "Sphere"
});
var geoPathGenerator = d3.geoPath(projection);
svg
.selectAll("path")
.data(f1CircuitsGeoJson.features)
.enter()
.append("path")
.attr("d", function(geoJsonFeature) {
var geoJsonCircle = d3
.geoCircle()
.center(d3.geoCentroid(geoJsonFeature))
.radius(1.5)();
return geoPathGenerator(geoJsonCircle);
})
.classed("circuit-path", true);
svg
.append("path")
.datum(worldGeoJson)
.attr("d", geoPathGenerator)
.classed("land", true)
.lower();
svg
.append("path")
.datum({
type: "Sphere"
})
.attr("d", geoPathGenerator)
.classed("sphere", true)
.lower();
return {
svg: svg,
geoPathGenerator: geoPathGenerator
};
}
function rotateMaps(geoJsonCollection, orthographicMap, repeatCount) {
var nextGeoJsonFeature = geoJsonCollection.features[0];
var nextCentroid = d3.geoCentroid(nextGeoJsonFeature);
var nextRotationDestination = [-nextCentroid[0], -nextCentroid[1]];
orthographicMap.svg
.selectAll("path")
.transition()
.duration(1250)
.delay(500)
.attrTween("d", function(geoJsonFeature) {
// for the current element being transitioned, figure out which svg "map" it belongs to
// this needs to happen because each svg "map" uses a different projection and geoPathGenerator
var geoPathGenerator = orthographicMap.geoPathGenerator;
var interpolator = d3.interpolate(
geoPathGenerator.projection().rotate(),
nextRotationDestination
);
// find out if the landmasses or one of the circles is the element currently being transitioned
if (d3.select(this).classed("land")) {
return function(t) {
geoPathGenerator.projection().rotate(interpolator(t));
return geoPathGenerator(geoJsonFeature) || "";
};
} else if (d3.select(this).classed("circuit-path")) {
return function(t) {
geoPathGenerator.projection().rotate(interpolator(t));
var geoJsonCircle = d3
.geoCircle()
.center(d3.geoCentroid(geoJsonFeature))
.radius(1.5)();
return geoPathGenerator(geoJsonCircle) || "";
};
} else {
return "";
}
})
.on("start", function(d, idx, nodeList) {
while (idx < nodeList.length - 1) {
return;
}
orthographicMap.svg
.selectAll("path.circuit-path")
.style("stroke", "steelblue")
.style("fill", "steelblue")
.filter(function(geoJsonFeature) {
return (
geoJsonFeature.properties.Name ===
nextGeoJsonFeature.properties.Name
);
})
.style("stroke", "red")
.style("fill", "red")
.raise();
d3.select(".circuit-name").text(nextGeoJsonFeature.properties.Name);
})
.on("end", function(d, idx, nodeList) {
while (idx < nodeList.length - 1) {
return;
}
repeatCount++;
addToStackingMap(nextGeoJsonFeature, function() {
// reset all the circles back to the original color when starting the entire cycle over again
// and clear the stacked circuits
if (repeatCount === geoJsonCollection.features.length) {
// rinse and repeat from the beginning after a delay
setTimeout(function() {
d3.select(".info").html("All done! Start over.");
// all circles on the world maps back to blue
orthographicMap.svg
.selectAll("path.circuit-path")
.transition()
.style("stroke", "steelblue")
.style("fill", "steelblue");
repeatCount = 0;
// clear the stacking map circuit stack
clearStackingMap(function() {
d3
.select(".info")
.html('find & stack... <span class="circuit-name"></span>');
geoJsonCollection.features.push(
geoJsonCollection.features.shift()
);
rotateMaps(geoJsonCollection, orthographicMap, repeatCount);
});
}, 4000);
} else {
// repeat to continue to the next circuit location
geoJsonCollection.features.push(geoJsonCollection.features.shift());
rotateMaps(geoJsonCollection, orthographicMap, repeatCount);
}
});
});
}
function clearStackingMap(callback) {
d3
.select("svg.stacking-map")
.selectAll("path")
.each(function(d, outerIndex, nodeList) {
d3
.select(this)
.transition()
.delay(100 * outerIndex)
.ease(d3.easeBackIn)
.style("transform", "scale(0.01)")
.on("end", function() {
while (outerIndex < nodeList.length - 1) {
return;
}
d3
.select("svg.stacking-map")
.selectAll("path")
.remove();
callback();
});
});
}
function addToStackingMap(geoJsonFeature, callback) {
var svg = d3.select("svg.stacking-map");
var dimensions = svg.attr("viewBox").split(" ");
var width = dimensions[2];
var height = dimensions[3];
var newCircuitPath = svg
.append("path")
.datum(geoJsonFeature)
.attr("d", function(geoJsonFeature) {
var featureCentroid = d3.geoCentroid(geoJsonFeature);
var featureCentroidRotation = [-featureCentroid[0], -featureCentroid[1]];
var projection = d3
.geoMercator()
// half of the parent <svg> width and height
.translate([width / 2, height / 2])
// this scale seems to work well for all the circuits
.scale(2400 * width)
// the projection rotation is what is most important for stacking features and
// it is calculated by finding the centroid coordinates of the geojson circuit
.rotate(featureCentroidRotation);
// the geoPathGenerator is responsible for generating the "d" attribute that a <path> element needs
var geoPathGenerator = d3.geoPath(projection);
return geoPathGenerator(geoJsonFeature);
})
.classed("circuit-path", true)
.raise();
newCircuitPath
.style("transform", "scale(0.01)")
.style("stroke", "red")
.style("stroke-width", 2)
.style("opacity", 0.7)
.transition()
.duration(500)
.ease(d3.easeBackOut)
.style("transform", null)
.on("end", function() {
newCircuitPath
.transition()
.style("transform", null)
.style("stroke", null)
.style("stroke-width", null)
.style("opacity", null)
.on("end", function() {
callback();
});
});
}
Also see: Tab Triggers