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

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/addons/p5.sound.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>
              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
}
canvas {
  display: block;
  border: solid 1px;
}
              
            
!

JS

              
                let width = 400;
let height = 400;
let maxGridValue = 5; // 座標軸の最大値

function setup() {
  createCanvas(width, height);
}

function draw() {
  background(255);
  drawGrid();
  drawFunc();
  drawDerivative();
}

// グリッドの描画
function drawGrid() {
  strokeWeight(2);
  drawLineCanvas(-maxGridValue, 0, maxGridValue, 0);
  drawLineCanvas(0, -maxGridValue, 0, maxGridValue);
  for (let x = -maxGridValue; x < maxGridValue; x++) {
    for (let y = -maxGridValue; y < maxGridValue; y++) {
      // 縦線
      if (x+1 == 0) {
        stroke(80, 80, 80);
      } else {
        stroke(180, 180, 180);
      }
      drawLineCanvas(x+1, y, x+1, y+1);
      // 横線
      if (y+1 == 0) {
        stroke(80, 80, 80);
      } else {
        stroke(180, 180, 180);
      }
      drawLineCanvas(x, y+1, x+1, y+1);
    }
  }
}

// 関数の描画
function drawFunc() {
  let dx = 0.1;
  strokeWeight(4);
  for (let x = -maxGridValue; x < maxGridValue; x+=dx) {
    stroke(0, 0, 255);
    drawLineCanvas(x, func(x), (x+dx), func(x+dx));
  }
}

// 導関数の描画
function drawDerivative() {
  let dx = 0.1;
  strokeWeight(4);
  for (let x = -maxGridValue; x < maxGridValue; x+=dx) {
    stroke(255, 0, 255);
    drawLineCanvas(x, derivative(x), (x+dx), derivative(x+dx));
  }
}

// 関数
function func(x) {
  // return x**2;
  return x**3;
  // return -2 * x*x*x + 3 * x*x;
  // return cos(x);
  // return sin(x);
}

// 導関数
function derivative(x) {
  let h = 0.01; // ここを小さくするほど精度が上がる
  return (func(x+h) - func(x))/h;
}

// 受け取った座標位置で線を綺麗に表示する
// 例: 5x5の座標の場合 (1,2) => (40,80)
function drawLineCanvas(fromX, fromY, toX, toY) {
  fromX *= width/2/maxGridValue;
  fromY *= height/2/maxGridValue;
  toX *= width/2/maxGridValue;
  toY *= height/2/maxGridValue;
  let offset = [width/2, height/2]; // Y軸は反転させる
  line(fromX+offset[0], height-(fromY+offset[1]), toX+offset[0], height-(toY+offset[1]));
}

              
            
!
999px

Console