css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              #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/master/public/img/logo_bare.png', alt='Abdullah\'s logo')
            
          
!
            
              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
  
            
          
!
            
              // TODO: clean this mess up.

// 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,
  zoomSensativity = 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 = map(mouseX, 0, width, xCoords[0], xCoords[xCoords.length - 1]);
    ellipse(x, (-func(x / (width / (scale * 2))) + scale) * (height / (scale * 2)), 10, 10);
  },
  tangent: () => {
    let xPos = map(mouseX, 0, width, xCoords[0], xCoords[xCoords.length - 1]),
      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 += zoomSensativity * event.delta);
  zoom = constrain(zoom, minZoom, maxZoom);
  scale = constrain(scale, 2, maxZoom);
  xCoords.length = 0;
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console