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.
<div id="viewDiv"><div id="countdownTimer" class="countdownTimer"></div></div>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
.countdownTimer {
position: absolute;
right: 10px;
bottom: 30px;
background-color: rgba(255,255,255,0.7);
z-index: 100;
text-align: center;
padding: 5px;
font-family: "Avenir Next W00","Helvetica Neue",Helvetica,Arial,sans-serif;
min-width: 20px;
}
// Example of loading a protocol buffer feed directly into an ArcGIS
// API for JavaScript map.
//
// In this case we are loading Bus locations from MetroSTL
// (https://www.metrostlouis.org/developer-resources/)
// As of right now, it only updates every 10 mins :( so don't expect to see
// many changes on the map (although if you hang out for awhile it WILL update)
//
// Useful links:
// https://github.com/mapbox/pbf
// https://github.com/protobufjs/protobuf.js/wiki/How-to-read-binary-data-in-the-browser-or-under-node.js%3F
//
// One-time:
// Had to convert this proto file:
// https://developers.google.com/transit/gtfs-realtime/gtfs-realtime-proto
// into a JS module first using
// `npm install pbf; pbf gtfs-realtime.proto --browser > gtfs-realtime.browser.proto.js`
// (this is where `FeedMessage` is coming from below)
import { loadModules } from "https://unpkg.com/esri-loader/dist/esm/esri-loader.js";
// Note that the import line above works because Codepen is setting this script
// tag to have 'type="module"' - more info:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
// https://blog.codepen.io/2017/12/26/adding-typemodule-scripts-pens/
const protobufUpdate = async () => {
const url =
"https://grffe.com/proxy/proxy.php?https://www.metrostlouis.org/RealTimeData/StlRealTimeVehicles.pb?cacheBust=" +
new Date().getTime();
let response = await fetch(url);
if (response.ok) {
// if HTTP-status is 200-299
// get the response body (the method explained below)
const bufferRes = await response.arrayBuffer();
const pbf = new Pbf(new Uint8Array(bufferRes));
const obj = FeedMessage.read(pbf);
return obj.entity;
} else {
console.error("error:", response.status);
}
};
let timerInterval;
const resetTimer = () => {
clearInterval(timerInterval);
const node = document.querySelector(".countdownTimer");
node.innerHTML = 15;
timerInterval = setInterval(() => {
const n = document.querySelector(".countdownTimer");
n.innerHTML = n.innerHTML - 1;
}, 1000);
}
// Removes all the graphics, calls the API to get the data,
// and adds all the Graphics to the input graphicsLayer.
const updateLayer = async (featureLayer, layerView) => {
const [Graphic] = await loadModules(["esri/Graphic"]);
// then get all the locations by calling the API (Protocol buffer service)
const locations = await protobufUpdate();
console.log("locations:", locations);
// Add all the locations to the map:
const graphics = locations.map(locationObject => {
var point = {
type: "point", // autocasts as new Polyline()
latitude: locationObject.vehicle.position.latitude,
longitude: locationObject.vehicle.position.longitude
};
var timeStampDate = new Date(0); // The 0 there is the key, which sets the date to the epoch
timeStampDate.setUTCSeconds(locationObject.vehicle.timestamp);
var attributes = {
name: locationObject.vehicle.vehicle.label,
timestamp: timeStampDate.toTimeString(),
route: locationObject.vehicle.trip.route_id,
route_start: locationObject.vehicle.trip.start_time
};
return new Graphic({
geometry: point,
attributes: attributes,
});
});
// first clear out the graphicsLayer
console.log('featureLayer:', featureLayer);
layerView.queryFeatures().then((results) => {
featureLayer.applyEdits({
deleteFeatures: results.features,
addFeatures: graphics
});
});
};
const main = async () => {
// More info on esri-loader's loadModules function:
// https://github.com/Esri/esri-loader#loading-modules-from-the-arcgis-api-for-javascript
const [Map, MapView, FeatureLayer] = await loadModules(
["esri/Map", "esri/views/MapView", "esri/layers/FeatureLayer"],
{ css: true }
);
const fl = new FeatureLayer({
fields: [
{
name: "ObjectID",
alias: "ObjectID",
type: "oid"
},
{
name: "name",
alias: "Name",
type: "string"
},
{
name: "timestamp",
alias: "timestamp",
type: "string"
},
{
name: "route",
alias: "route",
type: "string"
},
{
name: "route_start",
alias: "route_start",
type: "string"
}
],
objectIdField: "ObjectID",
geometryType: "point",
renderer: {
type: "simple", // autocasts as new SimpleRenderer()
symbol: {
type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
style: "circle",
color: "blue",
size: "15px", // pixels
outline: {
// autocasts as new SimpleLineSymbol()
color: [255, 255, 255],
width: 1, // points
}
}
},
popupTemplate: {
title: "{name}",
content:
"Updated: {timestamp}<br />Route: {route} (Started {route_start})"
},
labelingInfo: [
{ // autocasts as new LabelClass()
symbol: {
type: "text", // autocasts as new TextSymbol()
color: "black",
// haloColor: "blue",
// haloSize: 1,
font: { // autocast as new Font()
// family: "Ubuntu Mono",
size: 10,
weight: "bold"
}
},
labelPlacement: "center-right",
labelExpressionInfo: {
expression: "$feature.name"
},
maxScale: 0,
minScale: 100000
}
],
source: []
});
const map = new Map({
basemap: "streets",
layers: [fl]
});
const viewOptions = {
container: "viewDiv",
map: map,
center: [-90.3, 38.6],
zoom: 10
};
// create 2D map:
var view = new MapView(viewOptions);
view.whenLayerView(fl).then(function(layerView) {
console.log('layerView', layerView);
// do something with the layerView
updateLayer(fl, layerView);
resetTimer();
// every 15 seconds update the graphicsLayer:
setInterval(() => {
updateLayer(fl, layerView);
resetTimer();
}, 15000);
});
};
main();
Also see: Tab Triggers