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.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>TravelTime Tiles</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.7.0/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.7.0/mapbox-gl.css" rel="stylesheet" />
</head>
<body>
<div id="legend">
<div class="instrct">
<h3>Instructions</h3>
<ul>
<li>You can click on route line to add additional points</li>
<li>You can select transport mode and it will be displayed if it is available</li>
<li>You can drag location markers (origin/destination) points</li>
<li>You can enable range search and specify search range in seconds within 12 hours</li>
</ul>
</div>
<h4>
Transport mode:
</h4>
<div>
<input type="radio" name="transportMode" onchange="refreshLines()" value="cycling">Cycling<br>
<input type="radio" name="transportMode" onchange="refreshLines()" value="driving">Driving<br>
<input type="radio" name="transportMode" onchange="refreshLines()" checked="checked"
value="public_transport">Public transport<br>
<input type="radio" name="transportMode" onchange="refreshLines()" value="walking">Walking<br>
<input type="radio" name="transportMode" onchange="refreshLines()" value="coach">Coach<br>
<input type="radio" name="transportMode" onchange="refreshLines()" value="train">Train<br>
<input type="radio" name="transportMode" onchange="refreshLines()" value="ferry">Ferry<br>
<input type="radio" name="transportMode" onchange="refreshLines()" value="driving+ferry">Driving + ferry<br>
<input type="radio" name="transportMode" onchange="refreshLines()" value="cycling+ferry">Cycling + ferry<br>
<input type="radio" name="transportMode" onchange="refreshLines()" value="cycling+public_transport">Cycling +
public transport<br>
</div>
<h4>
Range search
</h4>
<div>
<input type="checkbox" name="rangeEnable" id="rangeCheckbox" onchange="refreshLines()">Enable range search<br>
<input type="number" id="rangeValue" min="1" max"43200" onchange="refreshLines()" value="1"> Second(s)
</div>
<h4>
Transport navigation legend:
</h4>
<div>
<span style="background-color: #fc5c65;"> </span> Bus
</div>
<div>
<span style="background-color: #2bcbba;"> </span> Walk
</div>
<div>
<span style="background-color: #778ca3;"> </span> Other
</div>
<div>
<span style="background-color: #3867d6;"> </span> Rail
</div>
<div>
<span style="background-color: #a55eea;"> </span> Underground
</div>
<div>
<span style="background-color: #45aaf2;"> </span> Car
</div>
<div>
@ Travel time platform | OpenStreetMap
</div>
</div>
<div id="map"></div>
</body>
</html>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
.mapboxgl-popup {
max-width: 400px;
font: 12px/20px "Helvetica Neue", Arial, Helvetica, sans-serif;
}
#legend {
position: fixed;
bottom: 10px;
left: 10px;
z-index: 900;
background-color: white;
padding: 10px;
margin: 10px;
height: Calc(100% - 60px);
overflow-y: auto;
}
#legend div > span {
width: 20px;
height: 20px;
display: inline-block;
}
.instrct {
max-width: 350px;
}
.instrct ul {
padding-inline-start: 15px;
}
#rangeValue {
width: 80px;
}
var busColor = "#fc5c65";
var walkColor = "#2bcbba";
var otherColor = "#778ca3";
var railColor = "#3867d6";
var undergroundColor = "#a55eea";
var carColor = "#45aaf2";
function getTransportMode() {
return document.querySelector('input[name="transportMode"]:checked').value;
}
function loadData(start, end, pairName) {
var rangeEnabled = document.querySelector('#rangeCheckbox').checked;
var rangeValue = document.querySelector('#rangeValue').value;
pairName || (pairName = "not_set");
map.getLayer(pairName) && map.removeLayer(pairName);
var requestData = JSON.stringify({
locations: [
{
id: "Start",
coords: start
},
{
id: "End",
coords: end
}
],
departure_searches: [
{
id: "departure search example",
departure_location_id: "Start",
arrival_location_ids: ["End"],
transportation: {
type: getTransportMode()
},
departure_time: new Date().toISOString(),
properties: ["travel_time", "distance", "route"],
range: {
enabled: rangeEnabled,
max_results: 1,
width: parseInt(rangeValue)
}
}
]
});
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function() {
if (this.readyState === 4) {
parseAndLoadLines(JSON.parse(this.response), pairName);
}
});
xhr.open("POST", "https://api.traveltimeapp.com/v4/routes");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("X-Application-Id", "APP ID HERE");
xhr.setRequestHeader("X-Api-Key", "API KEY HERE");
xhr.send(requestData);
}
var map = new mapboxgl.Map({
container: "map",
style: {
'version': 8,
'sources': {
'raster-tiles': {
'type': 'raster',
'tiles': [
'https://tiles.traveltime.com/lux/{z}/{x}/{y}.png?key=APP ID HERE'
],
'tileSize': 256,
'attribution':
'Map tiles by <a target="_top" rel="noopener" href="http://stamen.com">Stamen Design</a>, under <a target="_top" rel="noopener" href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a target="_top" rel="noopener" href="http://openstreetmap.org">OpenStreetMap</a>, under <a target="_top" rel="noopener" href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>'
}
},
'layers': [
{
'id': 'simple-tiles',
'type': 'raster',
'source': 'raster-tiles',
'minzoom': 0,
'maxzoom': 22
}
]
},
center: [13.352798436541264, 52.51307448201544],
zoom: 15,
attributionControl: false
});
map.on("load", function() {
refreshLines();
});
for (let index = 0; index < 10; index++) {
map.on("click", "pair_" + index, function(e) {
var marker = addMarker(e.lngLat.lng, e.lngLat.lat);
var name = "pair_" + index;
var aMarker = pairs[index][0];
var bMarker = pairs[index][1];
pairs.splice(index + 1, 0, [marker, aMarker]);
pairs[index] = [bMarker, marker];
refreshLines();
});
}
function addMarker(lng, lat) {
var marker = new mapboxgl.Marker({
draggable: true
})
.setLngLat([lng, lat])
.addTo(map);
marker.on("dragend", e => refreshLines());
return marker;
}
var markers = [
addMarker(13.339723415070296, 52.515636259501576),
addMarker(13.36456681231744, 52.51768082692038)
];
var pairs = [[markers[0], markers[1]]];
function refreshLines() {
for (var pairI in pairs) {
loadData(
pairs[pairI][0].getLngLat(),
pairs[pairI][1].getLngLat(),
"pair_" + pairI
);
}
}
function parseAndLoadLines(response, pairName) {
var parsed = apiResponseToLineStrings(response);
loadLines(parsed, pairName);
}
function apiResponseToLineStrings(response) {
var location = response.results[0].locations[0];
if (!location) {
var mode = getTransportMode();
alert(`No route available for transport mode: '${mode}'`);
}
var id = location.id;
var properties = location.properties[0];
var travelTime = properties.travel_time;
var route = properties.route;
var lineStrings = route.parts.map(part => {
var coords = part.coords;
var coordsAsArrays = coords.map(coord => {
return [coord.lng, coord.lat];
});
var mode = part.mode;
return {
type: "Feature",
properties: {
color: modeToColor(mode),
mode: mode
},
geometry: {
type: "LineString",
coordinates: coordsAsArrays
}
};
});
return {
type: "FeatureCollection",
features: lineStrings
};
}
function loadLines(parsed, pairName) {
map.getSource(pairName) && map.removeSource(pairName);
map.addSource(pairName, {
type: "geojson",
data: parsed
});
map.addLayer({
id: pairName,
type: "line",
source: pairName,
layout: {
"line-join": "round",
"line-cap": "round"
},
paint: {
"line-color": ["get", "color"],
"line-width": 9
}
});
map.on("mouseenter", "LineString", function() {
map.getCanvas().style.cursor = "pointer";
});
map.on("mouseleave", "LineString", function() {
map.getCanvas().style.cursor = "";
});
}
function modeToColor(mode) {
switch (mode) {
case "walk":
return walkColor;
case "bus":
case "coach":
return busColor;
case "rail_overground":
case "train":
case "rail_national":
case "rail_dlr":
return railColor;
case "rail_underground":
return undergroundColor;
case "car":
case "parking":
return carColor;
default:
return otherColor;
}
}
Also see: Tab Triggers