Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                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;
}

              
            
!

JS

              
                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();
    }
  }
});

              
            
!
999px

Console