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.
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"/>
<title>Arcade profiles - line segment labeling</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.24/esri/themes/light/main.css"/>
<script src="https://js.arcgis.com/4.24/"></script>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
#titleDiv {
max-width: 300px;
padding: 10px;
}
#sketchTools{
align-items: center;
}
</style>
<script>
require([
"esri/widgets/Sketch",
"esri/Map",
"esri/Graphic",
"esri/layers/GraphicsLayer",
"esri/views/MapView",
"esri/symbols/TextSymbol",
"esri/arcade",
"esri/widgets/Search",
"esri/widgets/BasemapGallery"
], (Sketch, Map, Graphic, GraphicsLayer, MapView, TextSymbol, arcade, Search, BasemapGallery) => {
(async () => {
const graphicsLayer = new GraphicsLayer();
const labelLayer = new GraphicsLayer();
const map = new Map({
basemap: "topo-vector",
layers: [graphicsLayer, labelLayer]
});
const view = new MapView({
container: "viewDiv",
map: map,
scale: 4514,
center: [-117.4203, 47.6615 ]
});
view.ui.add("titleDiv", "top-right");
await view.when();
const sketch = new Sketch({
layer: graphicsLayer,
view,
creationMode: "update",
visibleElements: {
createTools: {
point: false,
polyline: true,
polygon: false,
rectangle: false,
circle: false
},
selectionTools: {
"rectangle-selection": false,
"lasso-selection": false
},
snappingControlsElements: {
layerList: false
}
},
container: "sketchTools"
});
// view.ui.add(sketch, "top-right");
const labelSegmentProfile = {
variables: [
{
name: "$feature",
type: "feature"
}
]
};
// Expression for calculating measurements for active line segment
const labelSegmentExpression = `
var paths = Geometry($feature).paths;
var finalPath = Back(paths);
var finalPoint = Back(finalPath);
var previousPoint = finalPath[Count(finalPath)-2];
var a = Angle(previousPoint, finalPoint);
var b = Bearing(previousPoint, finalPoint);
var c = When(
(b < 22.5 && b >= 0) || b > 337.5, 'N',
b >= 22.5 && b < 67.5, 'NE',
b >= 67.5 && b < 112.5, 'E',
b >= 112.5 && b < 157.5, 'SE',
b >= 157.5 && b < 202.5, 'S',
b >= 202.5 && b < 247.5, 'SW',
b >= 247.5 && b < 292.5, 'W',
b >= 292.5 && b < 337.5, 'NW',
''
);
var d = DistanceGeodetic(previousPoint, finalPoint, "meters");
return {
label: {
text: Text(d, "#,###.#") + "m\\nBearing: " + Text(b, "#.#°") + " (" + c + ")",
// symbol angle direction is opposite of Angle function
angle: iif(b < 180, a * -1, a * -1 + 180),
color: "black",
font: {
family: "Orbitron",
style: "normal",
weight: "bold",
size: 9
},
haloColor: "white",
haloSize: 1.5,
yoffset: 4,
xoffset: iif((b <= 180 && b > 90) || b >= 270, 4, -4),
horizontalAlignment: "center",
verticalAlignment: "bottom"
},
location: Centroid([previousPoint, finalPoint])
};
`;
// Compile the expression and create an executor
const arcadeExecutor = await arcade.createArcadeExecutor(
labelSegmentExpression,
labelSegmentProfile
);
let activeLabel = null;
let createId = 0;
sketch.on("create", ({ state, graphic, tool, toolEventInfo }) => {
if(tool === "polyline" && state === "active"){
if(labelLayer.graphics.includes(activeLabel)){
// clear graphic on each update
labelLayer.graphics.remove(activeLabel);
activeLabel = null;
}
graphic.attributes = {
lineId: createId
};
// create label symbol and position
const { label, location } = arcadeExecutor.execute({
$feature: graphic
});
activeLabel = new Graphic({
symbol: new TextSymbol(label),
geometry: location,
attributes: {
lineId: createId
}
});
labelLayer.graphics.add(activeLabel);
// start new measurement for each added vertex
if(toolEventInfo.type === "vertex-add"){
activeLabel = null;
}
}
if(state === "complete"){
createId++;
}
});
// Expression for labeling all segments of a previously created line
const labelAllSegmentsExpression = `
var paths = Geometry($feature).paths;
var finalPath = Back(paths);
var labels = [];
var firstPoint;
var lastPoint;
for (var i in finalPath){
if(IsEmpty(firstPoint)){
firstPoint = finalPath[i];
continue;
}
if(IsEmpty(lastPoint)){
lastPoint = finalPath[i];
var a = Angle(firstPoint, lastPoint);
var b = Bearing(firstPoint, lastPoint);
var c = When(
(b < 22.5 && b >= 0) || b > 337.5, 'N',
b >= 22.5 && b < 67.5, 'NE',
b >= 67.5 && b < 112.5, 'E',
b >= 112.5 && b < 157.5, 'SE',
b >= 157.5 && b < 202.5, 'S',
b >= 202.5 && b < 247.5, 'SW',
b >= 247.5 && b < 292.5, 'W',
b >= 292.5 && b < 337.5, 'NW',
null
);
var d = DistanceGeodetic(firstPoint, lastPoint, "meters");
Push(labels, {
label: {
text: Text(d, "#,###.#") + "m\\nBearing: " + Text(b, "#.#°") + " (" + c + ")",
angle: iif(b < 180, a * -1, a * -1 + 180),
color: "black",
font: {
family: "Orbitron",
style: "normal",
weight: "bold",
size: 9
},
haloColor: "white",
haloSize: 1.5,
yoffset: 4,
xoffset: iif((b <= 180 && b > 90) || b >= 270, 4, -4),
horizontalAlignment: "center",
verticalAlignment: "bottom"
},
location: Centroid([firstPoint, lastPoint])
});
firstPoint = lastPoint;
lastPoint = null;
}
}
return labels;
`;
// Compile the expression and create an executor
const labelAllSegmentsExecutor = await arcade.createArcadeExecutor(
labelAllSegmentsExpression,
labelSegmentProfile
);
sketch.on(["update", "undo", "redo"], ({ type, state, graphics, tool, toolEventInfo }) => {
if(state === "active" || type === "undo" || type === "redo"){
graphics.forEach(graphic => {
const oldLabels = labelLayer.graphics.filter(label => graphic.attributes.lineId === label.attributes.lineId);
labelLayer.graphics.removeMany(oldLabels);
const labels = labelAllSegmentsExecutor.execute({
$feature: graphic
});
const labelGraphics = labels.map(({ label, location }) => {
activeLabel = new Graphic({
symbol: new TextSymbol(label),
geometry: location,
attributes: {
lineId: graphic.attributes.lineId
}
});
return activeLabel;
});
labelLayer.graphics.addMany(labelGraphics);
});
}
});
sketch.on("delete", ({ graphics }) => {
graphics.forEach(graphic => {
const oldLabels = labelLayer.graphics.filter(label => graphic.attributes.lineId === label.attributes.lineId);
labelLayer.graphics.removeMany(oldLabels);
});
});
})()
});
</script>
</head>
<body>
<div id="viewDiv"></div>
<div id="titleDiv" class="esri-widget">
<h1 id="title">Measure by segment</h1>
<p>Use the sketch tools to create a line. Each line segment will be labeled with its geodesic length, bearing (compass direction), and angle from the previous point.</p>
<div id="sketchTools"></div>
</div>
</body>
</html>
Also see: Tab Triggers