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 URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
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.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and 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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<h1>Earthquakes in the last 24 Hours</h1>
body {
background: #bbb;
}
h1 {
text-align: center;
text-transform: uppercase;
color: #444;
font-family: sans-serif;
margin: 1em 0;
font-size: 2em;
}
svg {
margin: 0 auto;
display: block;
background: #bbb;
}
.sphere {
fill: #bbb;
}
.land {
fill: #666;
}
.boundry {
fill: none;
stroke: #bbb;
stroke-linejoin: round;
stroke-linecap: round;
vector-effect: non-scaling-stroke;
}
.overlay {
fill: none;
pointer-events: all;
}
// Setup the svg element size and margins
var margin = {top: 20, right: 20, bottom: 20, left: 20},
width = 1200 - margin.left - margin.right,
height = 800 - margin.top - margin.bottom;
// Set the projection methods for the world map
var projection = d3.geoMercator()
.translate([width/2, height/1.5])
.scale((width - 1) / 2 / Math.PI);
// Set the world map path
var path = d3.geoPath()
.projection(projection);
// Create a variable to hold the main svg element
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// Group to hold the maps and borders
var g = svg.append('g')
.attr('id', 'world-map');
// Add a clip path element to the world map group
// for the x axis
g.append('clipPath')
.attr('id', 'clip-path')
.append('rect')
.attr('x', 0)
.attr('y', 30)
.attr('width', width)
.attr('height', height - 30)
// Group to hold all of the earthquake elements
var gQuakes = svg.append('g')
.attr('id', 'all-quakes');
// Import the geoJSON file for the world map
d3.json('https://s3-us-west-2.amazonaws.com/s.cdpn.io/25240/world-110m.json', function(error, world) {
if(error) throw error;
// Setup 24 hours ago object
var dateObj = new Date();
dateObj.setDate(dateObj.getDate() - 1);
// Append the World Map
var worldMap = g.append('path')
.attr('clip-path', 'url(#clip-path)') // attaches the clip path to not draw the map underneath the x axis
.datum(topojson.merge(world, world.objects.countries.geometries)) // draws a single land object for the entire map
.attr('class', 'land')
.attr('d', path)
// Append the World Map Country Borders
g.append('path')
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
.attr('class', 'boundry')
.attr('d', path);
// Create the x scale based on the domain of the 24 hour ago object and now
var x = d3.scaleTime()
.domain([dateObj, new Date()])
.range([0, width - margin.right - margin.left]);
// Append the xAxis on top
var xAxis = svg.append('g')
.attr('id', 'xAxis')
.attr('transform', 'translate(20, 20)')
.call(d3.axisTop(x));
// Import the last 24 hours of earthquake data from USGS
d3.json('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson', function(error, data) {
if(error) throw error;
var quake = data.features.reverse();
// Create a group with the quake id to hold the quake circle and pulse circle
var earthquakeGroups = gQuakes.selectAll('g')
.data(quake)
.enter().append('g')
.attr('id', function(d) {
return d.id;
})
.attr('class', 'quake-group');
//Create the pulse-circle circles for the earthquake pulse
gQuakes.selectAll('.quake-group')
.append('circle')
.attr('class', 'circle pulse-circle')
.attr('cx', function(d) {
return projection([d.geometry.coordinates[0], d.geometry.coordinates[1]])[0];
})
.attr('cy', function(d) {
return projection([d.geometry.coordinates[0], d.geometry.coordinates[1]])[1];
})
.attr('r', function(d) {
return 0;
})
.attr('fill', '#fff');
// Create the main quake circle with title
gQuakes.selectAll('.quake-group')
.append('circle')
.attr('cx', function(d) {
return projection([d.geometry.coordinates[0], d.geometry.coordinates[1]])[0];
})
.attr('cy', function(d) {
return projection([d.geometry.coordinates[0], d.geometry.coordinates[1]])[1];
})
.attr('r', 0 )
.attr('class', 'circle quake-circle')
.style('fill', 'red')
.style('opacity', 0.75)
.append('title')
.text(function(d) {
return 'Magnitue ' + d.properties.mag + ' ' + d.properties.place;
});
// Function that calculates the difference between the earthquake and 24 hours ago and
// creates a delay property.
var setQuakeDelay = function() {
for(var i = 0, max = quake.length; i < max; i++){
var timeDiff = quake[i].properties.time - dateObj;
var timeDiffObj = new Date(timeDiff);
quake[i].delay = Date.parse(timeDiffObj) / 5000; // Speed up the animation, otherwise it would take 24 hours ><
}
}
setQuakeDelay();
// Grab the longest delay for the xAxis marker
var longestDelay = quake[quake.length - 1].delay;
// Changes the radius of the earthquakes to their magnitue using a transition
// and the delay created from the setQuakeDelay function
var quakeCircles = svg.selectAll('.quake-circle')
.data(quake)
.transition()
.delay(function(d) {
return d.delay;
})
.duration(1000)
.attr('r', function(d) {
if(d.properties.mag < 0) {
return 0.1;
} else {
return d.properties.mag
}
});
// Changes the radius of the pulse circle to eight times the magnitude
// and fades out as it expands over two seconds
var pulseCircles = svg.selectAll('.pulse-circle')
.data(quake)
.transition()
.delay(function(d) {
return d.delay;
})
.duration(2000)
.attr('r', function(d) {
if(d.properties.mag < 0) {
return 0.1 * 8;
} else {
return d.properties.mag * 8;
}
})
.style('opacity', 0)
.remove()
// Add the time marker that moves across the xAxis while the animation it playing.
// It's not perfectly in sync, but it's close enough for this example. The length of
// the animation is equal to the longest delay that we calculated earlier.
var timeline = xAxis.append('circle')
.attr('class', 'transition-circle')
.attr('cx', 0)
.attr('cy', 0)
.attr('r', 3)
.style('fill', 'red')
.transition()
.ease(d3.easeLinear)
.duration(longestDelay + 1000)
.attr('cx', 1120)
})
})
Also see: Tab Triggers