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="mover-container">
<div id="mover"></div>
</div>
<h1>∇Δ <button id="zero">0</button></h1>
x: <button id="minus1">-</button> <span id="num1">num</span> <button id="plus1">+</button> = <input type="text" id="text1"><br><br>
y: <button id="minus2">-</button> <span id="num2">num</span> <button id="plus2">+</button> = <input type="text" id="text2"><br><br>
z: <button id="minus3">-</button> <span id="num3">num</span> <button id="plus3">+</button> = <input type="text" id="text3"><br><br>
<br><br>
TODO:<br>
- use a shader to draw a fuzzy plot of the expression
- make it so the actual value of the field is the expression, not a hidden value<br>
- make it so that fields can recursively nudge eachother's expressions<br>
- disable self-reference or research solving it e.g. x = x + 1 = Infinity... Hailstone conjecture tho<br>
- Fix very non-linear functions not nudging exactly 1 using an iterative gradient descent<br>
--- Fix Math.log, Math.sqrt, Math.sin, etc.<br>
- Re-implement using dual numbers for evaluating ∇?<br>
- Make it work for complex numbers and functions?<br>
- Does extending the complex numbers with a dual number allow you to do automatic differentiation on complex functions? (I don't think so, but why exactly? path dependence?)<br>
- nudging in arbitrary differentiable spaces?<br>
- topological inputs e.g. unit circle input, figure-eight, etc.<br>
- rotation nudges<br>
- use a method that avoids local-minima/maxima? e.g. numeric or symbolic differential equation solving, lagrange multipliers<br>
body {
margin: 30px;
}
span {
cursor: ew-resize;
user-select: none;
}
button {
border: none;
color: gray;
}
#mover-container {
position: absolute;
left: 400px;
top: 20px;
overflow: hidden;
border: 1px solid black;
height: 200px;
width: 200px;
// https://stackoverflow.com/a/32861765/5425899;
background-size: 10px 10px;
background-image: radial-gradient(circle, #ddd 1px, rgba(0, 0, 0, 0) 1px);
}
#mover {
border-radius: 100%;
height: 10px;
width: 10px;
background: OrangeRed;
}
const minusb1 = document.getElementById("minus1");
const plusb1 = document.getElementById("plus1");
const text1 = document.getElementById("text1");
const num1 = document.getElementById("num1");
let x = 0;
const minusb2 = document.getElementById("minus2");
const plusb2 = document.getElementById("plus2");
const text2 = document.getElementById("text2");
const num2 = document.getElementById("num2");
let y = 0;
const minusb3 = document.getElementById("minus3");
const plusb3 = document.getElementById("plus3");
const text3 = document.getElementById("text3");
const num3 = document.getElementById("num3");
let z = 0;
document.getElementById("zero").addEventListener("click", (e) => {
x = 0;
y = 0;
z = 0;
onUpdate();
});
const evf = (str) => {
try {
if (typeof eval("()=>" + str) === "function") {
return eval("()=>" + str);
}
} catch (e) {}
};
const del = (f, eps) => {
const base = f();
x += eps;
const xd = f();
x -= eps;
y += eps;
const yd = f();
y -= eps;
z += eps;
const zd = f();
z -= eps;
return [(xd - base) / eps, (yd - base) / eps, (zd - base) / eps];
};
const onUpdate = () => {
const xv = evf(text1.value) !== undefined ? evf(text1.value)() : x;
const yv = evf(text2.value) !== undefined ? evf(text2.value)() : y;
const zv = evf(text3.value) !== undefined ? evf(text3.value)() : z;
num1.textContent = xv.toFixed(1);
num2.textContent = yv.toFixed(1);
num3.textContent = zv.toFixed(1);
document.getElementById("mover").style.transform = `translate(${xv * 10}px, ${
yv * 10
}px) scale(${zv + 1})`;
};
onUpdate();
const clickThing = (val, amt) => {
if (evf(val) !== undefined) {
// slope of efv in x, y, and z directions
const [xd, yd, zd] = del(evf(val), amt); //using amt here assumes evf has a slope of 1??? lol
// if reaching a local maxima / minima BAD roughly
// if (Math.abs(xd) < 0.05 && Math.abs(yd) < 0.05 && Math.abs(zd) < 0.05)
// return
// want nx*xd + ny*yd + nz*zd = 1
const sum = xd + yd + zd;
const [normxd, normyd, normzd] = [xd / sum, yd / sum, zd / sum];
// want n = c*norm
const sum2 = normxd * xd + normyd * yd + normzd * zd;
const nx = normxd / sum2;
const ny = normyd / sum2;
const nz = normzd / sum2;
// TODO: make this iterative
// so it works for non-linear stuff
x += nx * amt;
y += ny * amt;
z += nz * amt;
onUpdate();
return true;
}
return false;
};
minusb1.addEventListener("click", () => {
if (!clickThing(text1.value, -0.01)) {
x -= 0.01;
onUpdate();
}
});
plusb1.addEventListener("click", () => {
if (!clickThing(text1.value, 0.01)) {
x += 0.01;
onUpdate();
}
});
minusb2.addEventListener("click", () => {
if (!clickThing(text2.value, -0.01)) {
y -= 0.01;
onUpdate();
}
});
plusb2.addEventListener("click", () => {
if (!clickThing(text2.value, 0.01)) {
y += 0.01;
onUpdate();
}
});
minusb3.addEventListener("click", () => {
if (!clickThing(text3.value, -0.01)) {
z -= 0.01;
onUpdate();
}
});
plusb3.addEventListener("click", () => {
if (!clickThing(text3.value, 0.01)) {
z += 0.01;
onUpdate();
}
});
text1.addEventListener("input", onUpdate);
text2.addEventListener("input", onUpdate);
text3.addEventListener("input", onUpdate);
// scrubbing
let scrubbing = null;
num1.addEventListener("mousedown", (e) => {
scrubbing = 1;
});
num2.addEventListener("mousedown", (e) => {
scrubbing = 2;
});
num3.addEventListener("mousedown", (e) => {
scrubbing = 3;
});
document.addEventListener("mouseup", (e) => {
scrubbing = null;
});
let prevMouseX = 0;
document.addEventListener("mousemove", (e) => {
const mouseX = e.x;
const diff = (mouseX - prevMouseX) / 10;
prevMouseX = mouseX;
if (diff === 0) return;
if (scrubbing === 1) {
if (!clickThing(text1.value, diff)) {
x += diff;
onUpdate();
}
}
if (scrubbing === 2) {
if (!clickThing(text2.value, diff)) {
y += diff;
onUpdate();
}
}
if (scrubbing === 3) {
if (!clickThing(text3.value, diff)) {
z += diff;
onUpdate();
}
}
});
Also see: Tab Triggers