Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                #content
footer#footer(style="position: absolute; bottom: 2%; right: 2%;")
  a#footer-link(href='https://khanabdullah.com/')
    #footer-content(style="position:relative;")
      img#footer-logo(style="position:fixed; width:70px;bottom:10px;right:10px;" src='https://raw.githubusercontent.com/abdullah-K/khanabdullah.com/v2/src/assets/img/logo.png', alt='Abdullah\'s logo')
              
            
!

CSS

              
                body, html
  width: 100vw
  height: 100vh
  
body
  background: #1d1d1d
  padding: 0
  margin: 0
  display: -webkit-box
  display: -ms-flexbox
  display: flex
  -webkit-box-pack: center
  -ms-flex-pack: center 
  justify-content: center
  -webkit-box-align: center
  -ms-flex-align: center
  align-items: center

#content
  position: fixed
  top: 50%
  left: 50%
  transform: translate(-50%, -50%)
#canvas
  border-radius: 35px
  box-shadow: -2px 2px 30px 5px #0f0f0f
  
              
            
!

JS

              
                // enter a continuous mathematical function below (or uncomment some of the examples)

function func(x) {
  // return -Math.sqrt(x+2) + 1;
  // return Math.pow(x - 1, 2) - 3;
  // return Math.sin(1.3*x);
  // return Math.pow(x, 3) + 2 * Math.pow(x, 2) -.4 * x;
  return 0.05 * -Math.pow(0.7*x + 2, 3) * Math.pow(0.7*x - 2, 2);
  // return x * Math.sqrt(4 - x)
}

let scale = 5,
  zoom = 1,
  minZoom = 1,
  maxZoom = 25.0,
  zoomSensitivity = 0.02;

function grid() {
  // for(let i = -scale; i <= (scale*2); i++){
  //   stroke(59)
  //   strokeWeight(1)
  //   line(((i*width/scale)/2), 0, (i*width/scale)/2, height);
  //   line(0, ((i*height/scale)/2), width, (i*height/scale)/2);
  // }
  let scaleRounded = Math.round(scale);
  stroke(80);
  strokeWeight(2);
  line(width / 2, 0, width / 2, height);
  line(0, height / 2, width, height / 2);
  fill(220);
  text(scaleRounded, width - 20, height / 2 + 20);
  text(-scaleRounded, 10, height / 2 + 20);
  text(scaleRounded, width / 2 + 10, 20);
  text(-scaleRounded, width / 2 + 10, height - 10);
  fill(180);
  noStroke()
  text("move cursor horizontally", 20, height - 20);
}

let canvasSize =
  window.innerWidth < 500 ? 300 : window.innerWidth < 600 ? 500 : 550;
// let canvasSize = window.innerWidth < 500 ? 300: 700

function setup() {
  let canvas = createCanvas(canvasSize, canvasSize, P2D);
  canvas.parent("content");
  canvas.id("canvas");
  cursor(CROSS);
}

function windowResized() {
  canvasSize =
    window.innerWidth < 500 ? 300 : window.innerWidth < 600 ? 500 : 550;
  resizeCanvas(canvasSize, canvasSize);
  xCoords.length = 0;
  graph.plot(x => func(x));
}

let xCoords = [],
  slope = (func, x, h) => (func(x + h) - func(x)) / h,
  linear = (m, x, b) => m * x + b;

let graph = {
  plot: func => {
    translate(width / 2, 0);
    beginShape();
    let xScale = width / (2 * scale),
        yScale = height / (2 * scale);
    for (let x = -(width / 2); x < width / 2; x++) {
      let xVal = x / xScale,
        yVal = height - (func(xVal) + scale) * yScale;

      xCoords.push(x);
      noFill();
      stroke(54, 136, 249);
      strokeWeight(3);
      vertex(x, yVal);
    }
    endShape();
  },
  trace: () => {
    noStroke();
    fill(54, 136, 249);
    let x = constrain(map(mouseX, 0, width, xCoords[0], xCoords[xCoords.length - 1]),-width/2,width/2);
    ellipse(x, (-func(x / (width / (scale * 2))) + scale) * (height / (scale * 2)), 10, 10);
  },
  tangent: () => {
    let xPos = constrain(map(mouseX, 0, width, xCoords[0], xCoords[xCoords.length - 1]),-width/2,width/2),
      yPos =
        height-(func(xPos / (width / (scale * 2))) + scale) * (height / (scale * 2)),
      xVal = xPos / (width / (scale * 2));

    let m = slope(func, xVal, 0.000000001),
      b = -(m * xPos + yPos),
      diff = width;

    let y1 = linear(m, xPos - diff, b),
      y2 = linear(m, xPos + diff, b);

    strokeWeight(2);
    stroke(249, 136, 54);

    line(xPos - diff, -y1, xPos + diff, -y2);
    stroke(80);
    strokeWeight(2);
    fill(220);
    text(`x = ${xVal.toFixed(2)}`, -(width / 2) + 25, 35);
    text(`d/dx = ${isNaN(parseFloat(m)) ? `∅` : m.toFixed(3)}`, -(width / 2) + 25, 55);
  }
};

function draw() {
  background(51);
  grid();
  graph.plot(x => func(x));
  graph.tangent();
  graph.trace();
}

function mouseWheel(event) {
  zoom, (scale += zoomSensitivity * event.delta);
  zoom = constrain(zoom, minZoom, maxZoom);
  scale = constrain(scale, 2, maxZoom);
  xCoords.length = 0;
}

              
            
!
999px

Console