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 class="container A">
<svg class="typeRange" height="165" width="330" view-box="0 0 330 165">
<g class="scale" stroke="red"></g>
<path class="outline" d="" />
<path class="fill" d="" />
<polygon class="needle" points="220,10 300,210 220,250 140,210" />
</svg>
<div class="output">30</div>
</div>
<p>You may change the initial value by dragging over the gauge
<br>or by editing the input below:</p>
<input type="text" class="initialValue" value="18" />
/*GREENS: #4ac4ac, #399988, #0f4534, #0a1a17;*/
* {
margin: 0;
padding: 0;
}
body,
html {
width: 100%;
height: 100%;
margin: 0px auto;
background-color: #0a1a17;
font-family: Verdana, Geneva, sans-serif;
font-size: 12px;
color: #ccc;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
}
.container {
position: relative;
margin: 100px auto 50px auto;
height: 165px;
width: 330px;
}
.output {
line-height: 35px;
width: 60px;
height: 30px;
background-color: #0f4534;
border-radius: 60px 60px 0 0;
position: absolute;
top: 135px;
left: 135px;
text-align: center;
}
.initialValue {
border: none;
border-bottom: 1px solid #399988;
color: #399988;
display: block;
width: 3em;
background-color: transparent;
margin: 1em auto;
outline: none;
font-size: 16px;
text-align: center;
}
/*SVG*/
svg {
margin: 0px;
padding: 0;
cursor: pointer;
border: 1px solid #0a1a17;
}
svg.focusable {
border: 1px solid #0f4534;
}
.outline,
.fill,
.center,
.needle,
.scale,
.output {
pointer-events: none;
}
.outline {
fill: #0f4534;
}
.fill {
fill: #399988;
}
.needle {
fill: #aa0000;
}
.scale {
stroke: #aaa;
}
text {
text-anchor: middle;
dominant-baseline: alphabetic;
font: 12px verdana, sans-serif;
fill: #aaa;
}
var containersRy = document.querySelector(".container");
var svg = document.querySelector(".typeRange");
var output = document.querySelector(".output");
var outline = document.querySelector(".outline");
var fill = document.querySelector(".fill");
var center = document.querySelector(".center");
var needle = document.querySelector(".needle");
var initialValue = document.querySelector(".initialValue");
var rad = Math.PI / 180;
var NS = "http:\/\/www.w3.org/2000/svg";
var W = parseInt(window.getComputedStyle(svg, null).getPropertyValue("width"));
var offset = 40;
var cx = ~~(W / 2);
var cy = 160;
var r1 = cx - offset;
var delta = ~~(r1 / 4);
var initVal = initialValue.value;
var isDragging = false;
var x1 = cx + r1,
y1 = cy;
var r2 = r1 - delta;
var x2 = offset,
y2 = cy;
var x3 = x1 - delta,
y3 = cy;
function drawScale() {
sr1 = r1 + 5;
sr2 = r2 - 5;
srT = r1 + 20;
var scale = document.querySelector(".scale");
clearRect(scale)
var n = 0;
for (var sa = -180; sa <= 0; sa += 18) {
var sx1 = cx + sr1 * Math.cos(sa * rad);
var sy1 = cy + sr1 * Math.sin(sa * rad);
var sx2 = cx + sr2 * Math.cos(sa * rad);
var sy2 = cy + sr2 * Math.sin(sa * rad);
var sxT = cx + srT * Math.cos(sa * rad);
var syT = cy + srT * Math.sin(sa * rad);
var scaleLine = document.createElementNS(NS, "line");
var scaleLineObj = {
class: "scale",
x1: sx1,
y1: sy1,
x2: sx2,
y2: sy2
};
setSVGAttributes(scaleLine, scaleLineObj);
scale.appendChild(scaleLine);
var scaleText = document.createElementNS(NS, "text");
var scaleTextObj = {
class: "scale",
x: sxT,
y: syT,
};
setSVGAttributes(scaleText, scaleTextObj);
scaleText.textContent = n * 10;
scale.appendChild(scaleText);
n++
}
}
function drawInput(cx, cy, r1, offset, delta, a) {
var d1 = getD1(cx, cy, r1, offset, delta);
var d2 = getD2(cx, cy, r1, offset, delta, a);
drawScale();
outline.setAttributeNS(null, "d", d1);
fill.setAttributeNS(null, "d", d2);
drawNeedle(cx, cy, r1, a);
}
function updateInput(p, cx, cy, r1, offset, delta) {
var x = p.x;
var y = p.y;
var lx = cx - x;
var ly = cy - y;
var a = Math.atan2(ly, lx) / rad - 180;
drawInput(cx, cy, r1, offset, delta, a);
output.innerHTML = Math.round((a + 180) / 1.8);
initialValue.value = Math.round((a + 180) / 1.8);
}
function getD1(cx, cy, r1, offset, delta) {
var x1 = cx + r1,
y1 = cy;
var x2 = offset,
y2 = cy;
var r2 = r1 - delta;
var x3 = x1 - delta,
y3 = cy;
var d1 =
"M " + x1 + ", " + y1 + " A" + r1 + "," + r1 + " 0 0 0 " + x2 + "," + y2 + " H" + (offset + delta) + " A" + r2 + "," + r2 + " 0 0 1 " + x3 + "," + y3 + " z";
return d1;
}
function getD2(cx, cy, r1, offset, delta, a) {
a *= rad;
var r2 = r1 - delta;
var x4 = cx + r1 * Math.cos(a);
var y4 = cy + r1 * Math.sin(a);
var x5 = cx + r2 * Math.cos(a);
var y5 = cy + r2 * Math.sin(a);
var d2 =
"M " + x4 + ", " + y4 + " A" + r1 + "," + r1 + " 0 0 0 " + x2 + "," + y2 + " H" + (offset + delta) + " A" + r2 + "," + r2 + " 0 0 1 " + x5 + "," + y5 + " z";
return d2;
}
function drawNeedle(cx, cy, r1, a) {
var nx1 = cx + 5 * Math.cos((a - 90) * rad);
var ny1 = cy + 5 * Math.sin((a - 90) * rad);
var nx2 = cx + (r1 + 15) * Math.cos(a * rad);
var ny2 = cy + (r1 + 15) * Math.sin(a * rad);
var nx3 = cx + 5 * Math.cos((a + 90) * rad);
var ny3 = cy + 5 * Math.sin((a + 90) * rad);
var points = nx1 + "," + ny1 + " " + nx2 + "," + ny2 + " " + nx3 + "," + ny3;
needle.setAttributeNS(null, "points", points);
}
// helpers
function oMousePos(elmt, evt) {
var ClientRect = elmt.getBoundingClientRect();
return { //obj
x: Math.round(evt.clientX - ClientRect.left),
y: Math.min(Math.round(evt.clientY - ClientRect.top), cy)
}
}
function clearRect(node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
}
function setSVGAttributes(elmt, oAtt) {
for (var prop in oAtt) {
elmt.setAttributeNS(null, prop, oAtt[prop]);
}
}
// events
window.addEventListener("load", function() {
var pa = (initVal * 1.8) - 180;
var p = {}
p.x = cx + r1 * Math.cos(pa * rad);
p.y = cy + r1 * Math.sin(pa * rad);
updateInput(p, cx, cy, r1, offset, delta)
}, false);
initialValue.addEventListener("input", function() {
var val = this.value;
var newVal = (!isNaN(val) && val >= 0 && val <= 100) ? val : 18;
var pa = (newVal * 1.8) - 180;
var p = {}
p.x = cx + r1 * Math.cos(pa * rad);
p.y = cy + r1 * Math.sin(pa * rad);
updateInput(p, cx, cy, r1, offset, delta)
}, false);
svg.addEventListener("mousedown", function(evt) {
isDragging = true;
this.classList.add("focusable");
var mousePos = oMousePos(svg, evt);
updateInput(mousePos, cx, cy, r1, offset, delta);
}, false);
svg.addEventListener("mouseup", function(evt) {
isDragging = false;
this.classList.remove("focusable");
}, false);
svg.addEventListener("mouseout", function(evt) {
isDragging = false;
this.classList.remove("focusable");
}, false);
svg.addEventListener("mousemove", function(evt) {
if (isDragging) {
var mousePos = oMousePos(svg, evt);
updateInput(mousePos, cx, cy, r1, offset, delta);
}
}, false);
Also see: Tab Triggers