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

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/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>
    <div id="ui_container"></div>
  </body>
</html>

              
            
!

CSS

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

JS

              
                
// *** ベクトルクラス ***
class Vec3 {
  constructor(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
  };
}

// *** 位置クラス ***
class Point {
  constructor(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
  };
}

// *** 座標ベクトル計算 ***
// ベクトル+ベクトル
function addVec(v1, v2) {
  return new Vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}

// ベクトル-ベクトル
function subVec(v1, v2) {
  return new Vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}

// ベクトル*スカラー
function mulVec(v, s) {
  return new Vec3(v.x * s, v.y * s, v.z *s);
}

// ベクトルの正規化
function normalize(v) {
  let l = sqrt(v.x**2 + v.y**2 + v.z**2);
  return new Vec3(v.x/l, v.y/l, v.z/l);
}

// ベクトルの外積
function crossVec(v1, v2) {
  let cross_v = new Vec3();
  cross_v.x = v1.y * v2.z - v1.z * v2.y;
  cross_v.y = v1.z * v2.x - v1.x * v2.z;
  cross_v.z = v1.x * v2.y - v1.y * v2.x;
  return cross_v;
}

// ベクトル描画処理
function drawVecLine(vec, start_p) {
  end_p_x = start_p.x + vec.x;
  end_p_y = start_p.y + vec.y;
  end_p_z = start_p.z + vec.z;
  return line(start_p.x, start_p.y, start_p.z, end_p_x, end_p_y, end_p_z);
}

// *** 変数定義 ***
// スライダー値(初期値)
let v1_x = 10;
let v1_y = 10;
let v1_z = 20;
let v2_x = 5;
let v2_y = 15;
let v2_z = 10;
// ベクトル
let v1 = new Vec3(v1_x, v1_y, v1_z);
let v2 = new Vec3(v2_x, v2_y, v2_z);
let v3 = crossVec(v1, v2); // v1 と v2 の外積

// *** 3D表示用SKETCH ***
// 初期設定処理
function setup() {
  // CANVAS設定(WEBGLを使用)
  createCanvas(400, 400, WEBGL);
  // ノーマルマテリアルを適用
  normalMaterial();
}

// 描画処理
function draw() {
  // マウスコントロールを設定
  orbitControl();
  // 背景色設定
  background(200);
  // 回転処理
  rotateX(radians(75));
  rotateZ(radians(45));
  // 平面描画
  push();
  plane(250, 250);
  // 線分設定
  strokeWeight(3);
  // ベクトル1描画
  v1 = new Vec3(v1_x, v1_y, v1_z);
  stroke(255, 0, 0);
  drawVecLine(v1, new Point(0, 0, 0));
  // ベクトル2描画
  v2 = new Vec3(v2_x, v2_y, v2_z);
  stroke(0, 255, 0);
  drawVecLine(v2, new Point(0, 0, 0));
  // ベクトル1とベクトル2の外積描画
  v3 = crossVec(v1, v2);
  stroke(0, 0, 255);
  drawVecLine(v3, new Point(0, 0, 0));
  pop();
}

// *** UI表示用SKETCH ***
// WEBGL上で画面表示できないため別で作成
var ui_sketch = function(p) {

  // 初期設定処理
  p.setup = function() {
    p.createCanvas(400, 180);
    // v1スライダー
    v1x_slider = createSlider(-200.0, 200.0, v1_x);
    v1x_slider.position(230, 30);
    v1x_slider.mouseReleased(setV1x);
    v1y_slider = createSlider(-200.0, 200.0, v1_y);
    v1y_slider.position(230, 50);
    v1y_slider.mouseReleased(setV1y);
    v1z_slider = createSlider(-200.0, 200.0, v1_z);
    v1z_slider.position(230, 70);
    v1z_slider.mouseReleased(setV1z);
    // v2スライダー
    v2x_slider = createSlider(-200.0, 200.0, v2_x);
    v2x_slider.position(230, 110);
    v2x_slider.mouseReleased(setV2x);
    v2y_slider = createSlider(-200.0, 200.0, v2_y);
    v2y_slider.position(230, 130);
    v2y_slider.mouseReleased(setV2y);
    v2z_slider = createSlider(-200.0, 200.0, v2_z);
    v2z_slider.position(230, 150);
    v2z_slider.mouseReleased(setV2z);
  };

  // 描画処理
  p.draw = function() {
    // 背景設定
    p.background(150);
    // テキスト描画
    p.fill(0);
    p.noStroke();
    p.textSize(15);
    p.fill(255, 0, 0);
    p.text("[v1] " + "(" + v1.x + ", " + v1.y + ", " + v1.z + ")", 230, 30);
    p.fill(0, 255, 0);
    p.text("[v2] " + "(" + v2.x + ", " + v2.y + ", " + v2.z + ")", 230, 110);
    p.fill(0, 0, 255);
    p.text("[v1*v2 外積] " + "(" + v3.x + ", " + v3.y + ", " + v3.z + ")", 30, 30);
  };

  // *** スライダー値設定 ***
  function setV1x() {
    v1_x = v1x_slider.value();
  }
  function setV1y() {
    v1_y = v1y_slider.value();
  }
  function setV1z() {
    v1_z = v1z_slider.value();
  }
  function setV2x() {
    v2_x = v2x_slider.value();
  }
  function setV2y() {
    v2_y = v2y_slider.value();
  }
  function setV2z() {
    v2_z = v2z_slider.value();
  }
};
new p5(ui_sketch, "ui_container");
              
            
!
999px

Console