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.
<div id="paper"></div>
<a target="_blank" href="https://www.jointjs.com">
<img id="logo" src="https://assets.codepen.io/7589991/jj-logo-red.svg" width="200" height="50"></img>
</a>
body {
overflow: hidden;
}
#paper {
position: absolute;
inset: 0 0 0 0;
}
.joint-popup.close-up-popup {
--arrow-width: 11px;
--arrow-color: #0075f2;
--arrow-mask-color: #dde6ed;
border: 3px solid #0075f2;
background: #dde6ed;
box-shadow: 5px 5px 5px 0 rgba(0, 0, 0, 0.2);
}
.close-up-line {
stroke: #0075f2;
}
.value-text {
display: none;
}
.show-average-values .value-text {
display: block;
}
.timing-context-toolbar {
box-shadow: 5px 5px 5px 0 rgba(0, 0, 0, 0.2);
.tools {
border: 1px solid #0075f2 !important;
.tool {
text-align: left;
font-size: 10px;
font-family: Arial, Helvetica, sans-serif;
color: #1a2938;
border: 0.5px solid #0075f2 !important;
i {
font-size: 8px;
color: #6a7c8a;
}
&:hover {
background-color: #f2f5f8;
}
}
}
}
#logo {
position: absolute;
bottom: 20px;
right: 0;
}
const { util, dia, ui, highlighters } = joint;
// generate tree as adjacency list
// where each node has a list of children
// each node is a unique id
function generateTree(options = {}) {
const { maxDepth = 1, minChildren = 1, maxChildren = 2 } = options;
let id = 0;
const tree = {};
function generateNode(depth) {
const children = [];
const currentId = id++;
tree[currentId] = children;
if (depth < maxDepth) {
const currentMinChildren = Array.isArray(minChildren)
? minChildren[Math.min(depth, minChildren.length - 1)]
: minChildren;
const currentMaxChildren = Array.isArray(maxChildren)
? maxChildren[Math.min(depth, maxChildren.length - 1)]
: maxChildren;
const numChildren = g.random(
currentMinChildren || 0,
currentMaxChildren || 0
);
for (let i = 0; i < numChildren; i++) {
children.push(generateNode(depth + 1));
}
}
return currentId;
}
generateNode(0);
return tree;
}
const BG_COLOR = "#dde6ed";
const FG_COLOR = "#cad8e3";
const FG_LABEL_COLOR = "#6a7c8a";
const NODE_COLOR = "#1a2938";
const LINK_COLOR = "#afc5d5";
const HIGHLIGHT_COLOR = "#0075f2";
const INTERMEDIATE_COLOR = "#7399bf";
const PARENT_GAP = 100;
const LEGEND_WIDTH = 100;
const NODE_SIZE = 10;
const NODE_MARKUP = util.svg`
<circle @selector="outer" />
<circle @selector="inner" />
<text @selector="value" class="value-text" />
`;
class Node extends dia.Element {
defaults() {
return {
...super.defaults,
type: "Node",
z: 2,
size: {
width: NODE_SIZE,
height: NODE_SIZE
},
attrs: {
outer: {
r: "calc(l / 2)",
cx: "calc(l / 2)",
cy: "calc(l / 2)",
stroke: BG_COLOR,
strokeWidth: 2,
fill: NODE_COLOR
},
inner: {
r: "calc(l / 2 - 3)",
cx: "calc(l / 2)",
cy: "calc(l / 2)",
stroke: BG_COLOR,
strokeWidth: 1,
fill: NODE_COLOR
},
value: {
textAnchor: "middle",
textVerticalAnchor: "middle",
x: "calc(l / 2)",
y: `calc(h + ${PARENT_GAP / 2})`,
fontSize: 10,
fontFamily: "Arial, sans-serif",
fill: HIGHLIGHT_COLOR,
stroke: BG_COLOR,
strokeWidth: 4,
paintOrder: "stroke"
}
}
};
}
preinitialize(...args) {
super.preinitialize(...args);
this.markup = NODE_MARKUP;
}
}
const LINK_MARKUP = util.svg`
<path @selector="wrapper" fill="none" cursor="pointer" stroke="transparent" stroke-linecap="round"/>
<path @selector="line" fill="none" pointer-events="none" />
`;
const LABEL_MARKUP = util.svg`
<text @selector="value" />
`;
class Link extends dia.Link {
defaults() {
return {
...super.defaults,
type: "Link",
z: 1,
bidirectional: false,
attrs: {
line: {
connection: true,
stroke: LINK_COLOR,
strokeWidth: 2,
strokeLinejoin: "round"
},
wrapper: {
connection: true,
strokeWidth: 10,
strokeLinejoin: "round"
}
}
};
}
preinitialize(...args) {
super.preinitialize(...args);
this.markup = LINK_MARKUP;
}
}
const LEGEND_MARKUP = util.svg`
<rect @selector="border" />
<rect @selector="bar" />
<text @selector="minValue" @group-selector="values" />
<text @selector="maxValue" @group-selector="values" />
`;
class Legend extends dia.Element {
density = 20;
defaults() {
return {
...super.defaults,
type: "Legend",
z: 2,
size: {
width: LEGEND_WIDTH,
height: 20
},
attrs: {
border: {
stroke: FG_COLOR,
strokeWidth: 1,
fill: "transparent",
width: "calc(w)",
height: "calc(h)"
},
bar: {
cursor: "pointer",
stroke: FG_COLOR,
x: 2,
y: 2,
width: "calc(w - 4)",
height: "calc(h - 4)"
},
values: {
textAnchor: "middle",
textVerticalAnchor: "middle",
y: "calc(h / 2)",
fontSize: 10,
fontFamily: "Arial, sans-serif",
fill: FG_LABEL_COLOR,
stroke: BG_COLOR,
strokeWidth: 4,
paintOrder: "stroke"
},
minValue: {
x: -10,
text: "0"
},
maxValue: {
x: "calc(w + 10)",
text: "1"
}
}
};
}
setBarColor(opt) {
const { density = 100, interpolate } = opt;
this.attr(
["bar", "fill"],
{
type: "linearGradient",
stops: Array.from({ length: density }, (_, i) => {
return {
offset: i / density,
color: interpolate(i / density)
};
})
},
{ rewrite: true }
);
}
preinitialize(...args) {
super.preinitialize(...args);
this.markup = LEGEND_MARKUP;
}
}
class LabelHighlighter extends dia.HighlighterView {
preinitialize() {
this.tagName = "text";
this.attributes = {
"font-size": 10,
"text-anchor": "middle",
"font-family": "Arial, sans-serif",
fill: HIGHLIGHT_COLOR,
stroke: BG_COLOR,
"stroke-width": 6,
"paint-order": "stroke"
};
}
highlight(linkView) {
const point = linkView.getPointAtRatio(0.5);
this.vel.attr("x", point.x);
this.vel.attr("y", point.y);
this.vel.text(linkView.model.getSourceCell().get("value").toFixed(4), {
textVerticalAnchor: "middle"
});
}
}
const cellNamespace = {
Node,
Legend,
Link
};
const maxDepth = 6;
const minChildren = [2, 2, 3, 2, 0];
const maxChildren = [2, 2, 3, 6, 1];
const list = generateTree({
maxDepth,
minChildren,
maxChildren
});
const graph = new dia.Graph({}, { cellNamespace });
const cells = joint.graphUtils.constructTree("0", {
makeElement: (label) => {
const childCount = list[label].length;
return new Node({
id: label,
siblingRank: childCount,
attrs: {
inner: {
fill: childCount > 0 ? INTERMEDIATE_COLOR : NODE_COLOR
}
}
});
},
makeLink: (parentElement, childElement) => {
return new Link({
source: { id: parentElement.id },
target: { id: childElement.id }
});
},
children: (root) => list[root]
});
graph.resetCells(cells);
graph.getElements().forEach((element) => {
const value = Math.random();
element.set({
// sibling rank will be normalized to a natural number when the layout is applied
siblingRank: value,
value
});
});
// Set bfs index (the index of the element in the bfs traversal, taking the sibling rank into account)
// Node: getNeighbors() returns the neighbors in the order they were added to the graph
function sortedBfs(node) {
let i = 0;
const queue = [node];
const sorted = [];
while (queue.length > 0) {
const current = queue.shift();
current.set("bfsIndex", i++);
queue.push(
...util.sortBy(graph.getNeighbors(current, { outbound: true }), (el) =>
el.get("siblingRank")
)
);
}
return sorted;
}
sortedBfs(graph.getCell(0));
// Set sibling labels (average value)
graph.getElements().forEach((element) => {
const childNodes = graph.getNeighbors(element, { outbound: true });
if (childNodes.length === 0) return;
const sumValue = childNodes.reduce(
(acc, neighbor) => acc + neighbor.get("value"),
0
);
const averageValue = sumValue / childNodes.length;
const singleChild = childNodes.length === 1;
element.attr("value", {
writingMode: singleChild ? "vertical-lr" : "lr",
text: averageValue.toFixed(2),
fontSize: 9 + (singleChild ? 0 : Math.round(averageValue * 10)),
y:
!singleChild && element.get("bfsIndex") % 2 === 0
? `calc(h + ${PARENT_GAP / 2 + 10})`
: `calc(h + ${PARENT_GAP / 2 - 10})`
});
});
// Set link colors
let interpolateColor = util.interpolate.hexColor("#FFC857", "#ED2637");
let interpolationTiming = util.timing.quad;
const interpolate = (t) => interpolateColor(interpolationTiming(t));
const paper = new joint.dia.Paper({
el: document.getElementById("paper"),
width: "100%",
height: "100%",
model: graph,
async: true,
sorting: joint.dia.Paper.sorting.APPROX,
cellViewNamespace: cellNamespace,
interactive: false,
clickThreshold: 10,
background: {
color: "#dde6ed"
},
defaultAnchor: {
name: "modelCenter"
},
defaultConnectionPoint: {
name: "anchor"
}
});
// Lay out the tree and transform the paper to fit the content
const contentArea = runLayout(paper, {
parentGap: PARENT_GAP,
maxDepth,
nodeSize: NODE_SIZE
});
paper.scaleContentToFit({ contentArea });
// Add legend element
const legend = new Legend({
position: { x: contentArea.topRight().x - 115, y: 15 }
});
graph.addCell(legend);
colorTree(graph, interpolate);
paper.on("element:pointerclick", (legendView) => {
if (legendView.model.get("type") !== "Legend") return;
const contextToolbar = new ui.ContextToolbar({
target: legendView.el,
root: paper.el,
vertical: true,
anchor: "top",
padding: 3,
scale: paper.scale().sx,
tools: [
{
action: "linear",
content: "Linear"
},
{
action: "quad",
content: "Quadratic"
},
{
action: "exponential",
content: "Exponential"
},
{
action: "clamped",
content: "Clamped <i>(0.3-0.7)</i>"
},
{
action: "threshold",
content: "Threshold <i>(0.8)</i>"
}
]
});
contextToolbar.el.style.width = `${LEGEND_WIDTH}px`;
contextToolbar.el.classList.add("timing-context-toolbar");
contextToolbar.on("all", (eventName) => {
if (!eventName.includes("action")) return;
const [, timingFunctionName] = eventName.split(":");
switch (timingFunctionName) {
case "clamped":
interpolationTiming = (t) => (t < 0.3 || t > 0.7 ? 0 : 1);
break;
case "threshold":
interpolationTiming = (t) => (t < 0.8 ? 0 : 1);
break;
default:
interpolationTiming = util.timing[timingFunctionName];
break;
}
colorTree(graph, interpolate);
contextToolbar.remove();
});
contextToolbar.render();
});
// Functions
function colorTree(graph, interpolate) {
legend.setBarColor({ density: 100, interpolate });
graph.getLinks().forEach((link) => {
link.attr(
["line", "stroke"],
interpolate(link.getTargetCell().get("value"))
);
});
}
function runLayout(
paper,
{ siblingGap = 0, startDepth = 0, parentGap, maxDepth, nodeSize }
) {
const graph = paper.model;
const tree = new joint.layout.TreeLayout({
graph,
siblingGap,
parentGap,
direction: "B",
updateVertices: false,
updateAttributes: (layoutArea, node) => {
node.set("depth", layoutArea.level);
}
});
tree.layout();
const area = tree.getLayoutBBox().moveAndExpand({
x: -80,
y: -20,
width: 80 + 20,
height: 20 + 20
});
// Draw depth lines.
const backLayerEl = paper.getLayerNode(dia.Paper.Layers.BACK);
Array.from({ length: maxDepth - startDepth + 1 }).forEach((_, i) => {
const y = nodeSize / 2 + (nodeSize + parentGap) * i;
const lineVEl = V("line", {
x1: area.x,
y1: y,
x2: area.x + area.width,
y2: y,
stroke: FG_COLOR
});
const textVEL = V("text", {
x: area.x + 10,
y: y - 5,
fill: FG_LABEL_COLOR,
"font-size": 12,
"font-family": "Arial"
});
textVEL.text(`depth ${startDepth + i}`);
lineVEl.appendTo(backLayerEl);
textVEL.appendTo(backLayerEl);
});
return area;
}
paper.on("element:mouseenter", (elementView) => {
if (elementView.model.get("type") === "Legend") return;
inspectNode(elementView);
});
paper.on("element:mouseleave", (elementView) => {
if (elementView.model.get("type") === "Legend") return;
leaveNode();
});
const scheduleShowSubgraph = util.debounce(showSubgraph, 100);
inspectNode(graph.getCell(0).findView(paper));
function showSubgraph(elementView, depth = 3) {
const { model: element } = elementView;
const popupGraph = new joint.dia.Graph({}, { cellNamespace });
const subgraphCells = getSubgraph(graph, element, depth);
const clonesMap = graph.cloneSubgraph(subgraphCells);
const popupCells = Object.values(clonesMap);
popupGraph.resetCells(popupCells);
popupGraph.getLinks().forEach((link) => {
link.labels([
{
attrs: {
value: {
text: link.getTargetCell().get("value").toFixed(2),
textAnchor: "middle",
textVerticalAnchor: "middle",
fontSize: 10,
fontFamily: "Arial, sans-serif",
fill: HIGHLIGHT_COLOR,
stroke: BG_COLOR,
strokeWidth: 2,
paintOrder: "stroke"
}
},
markup: LABEL_MARKUP,
position: {
distance: 1,
offset: { y: -5 }
}
}
]);
});
clonesMap[element.id].position(0, 0);
const popupPaper = new dia.Paper({
model: popupGraph,
frozen: true,
async: false,
sorting: joint.dia.Paper.sorting.APPROX,
cellViewNamespace: cellNamespace,
interactive: false,
labelsLayer: true,
background: {
color: "#dde6ed"
}
});
const startDepth = element.get("depth");
const area = runLayout(popupPaper, {
parentGap: 50,
siblingGap: 20,
startDepth,
maxDepth: startDepth + depth,
nodeSize: NODE_SIZE
});
let { width, height } = area;
if (width > window.innerWidth / 2) {
width /= 2;
height /= 2;
} else if (width < window.innerWidth / 4) {
width *= 2;
height *= 2;
}
popupPaper.setDimensions(width, height);
popupPaper.scaleContentToFit({ contentArea: area });
const popup = new ui.Popup({
content: popupPaper.el,
target: elementView.el,
anchor: "left",
position: "right",
padding: 12
});
popup.el.classList.add("close-up-popup");
popup.el.style.pointerEvents = "none";
popup.on("popup:close", () => popupPaper.remove());
popup.render();
popupPaper.unfreeze();
}
function getSubgraph(graph, element, maxDepth) {
const successors = [element];
graph.search(
element,
(el, depth) => {
if (depth <= maxDepth) {
successors.push(el);
}
},
{ outbound: true }
);
return graph.getSubgraph(successors);
}
// Highlighting
function inspectNode(elementView) {
const { model: element } = elementView;
leaveNode();
if (graph.isSink(element)) {
highlightPath(elementView);
} else if (graph.isSource(element)) {
paper.el.classList.add("show-average-values");
} else {
scheduleShowSubgraph(elementView);
}
highlightNode(elementView);
}
function leaveNode() {
if (scheduleShowSubgraph) {
scheduleShowSubgraph.cancel();
}
dia.HighlighterView.removeAll(paper, "close-up");
LabelHighlighter.removeAll(paper);
ui.Popup.close();
paper.el.classList.remove("show-average-values");
}
function highlightPath(elementView) {
const [link] = graph.getConnectedLinks(elementView.model, { inbound: true });
if (!link) return;
highlightLink(link.findView(paper));
highlightPath(link.getSourceCell().findView(paper));
}
function highlightLink(linkView) {
highlighters.addClass.add(linkView, "line", "close-up", {
className: "close-up-line"
});
LabelHighlighter.add(linkView, "root", "close-up-label", {
layer: dia.Paper.Layers.FRONT
});
}
function highlightNode(elementView) {
highlighters.mask.add(elementView, "outer", "close-up", {
layer: dia.Paper.Layers.FRONT,
padding: 0,
attrs: {
stroke: HIGHLIGHT_COLOR,
"stroke-width": 2
}
});
}
Also see: Tab Triggers