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

              
                // 座標クラス
class Point {
  constructor(_x, _y) {
    this.x = _x;
    this.y = _y;
  }
}

// ベクトルクラス
class Vec {
  constructor(_x, _y) {
    this.x = _x;
    this.y = _y;
  }
}

// スライダー値(初期値)
let v1_x = -50;
let v1_y = 50;
let scolar = 2;
// 座標
let p0 = new Point(200, 200);
let p1; // p0 + v1
let p2; // p1 + v2
// ベクトル
let v1 = new Point(v1_x, v1_y);
let v2 = new Point(v1_x * scolar, v1_y * scolar);

function setup() {
  createCanvas(400, 400);
  // v1スライダー
  v1x_slider = createSlider(-200.0, 200.0, v1_x);
  v1x_slider.position(250, 30);
  v1x_slider.mouseReleased(setV1x);
  v1y_slider = createSlider(-200.0, 200.0, v1_y);
  v1y_slider.position(250, 50);
  v1y_slider.mouseReleased(setV1y);
  // scスライダー
  sc_slider = createSlider(0.0, 10.0, scolar);
  sc_slider.position(250, 100);
  sc_slider.mouseReleased(setScolar);
}

function draw() {
  // スライダー値を設定
  v1.x = v1_x;
  v1.y = v1_y;
  v2 = mulVec(v1, scolar);
  // 座標 + ベクトル
  p1 = addPointVec(p0, v1);
  p2 = addPointVec(p1, v2);
  
  background(255);
  // 図形描画
  stroke(0, 0, 0);
  fill(0);
  circle(p0.x, p0.y, 20);
  fill(230);
  circle(p1.x, p1.y, 20);
  circle(p2.x, p2.y, 20);
  // 線描画
  stroke(0, 0, 255);
  line(p0.x, p0.y, p2.x, p2.y);
  stroke(255, 0, 0);
  line(p0.x, p0.y, p1.x, p1.y);

  // テキスト描画
  fill(0);
  noStroke();
  textSize(15);
  fill(255, 0, 0);
  text("[v1] " + "(" + v1.x + ", " + v1.y + ")", 250, 30);
  fill(0, 0, 255);
  text("[v1 * scolar] " + "(" + v2.x + ", " + v2.y + ")", 250, 150);
  fill(0);
  text("[scolar] " + scolar, 250, 100);
}

// *** 座標ベクトル計算 ***
// p1->p2方向のベクトル
function point2Vec(p1, p2) {
  // p2 - p1
  return new Vec(p2.x - p1.x, p2.y - p1.y);
}

// 座標+ベクトル
function addPointVec(p, v) {
  return new Point(p.x + v.x, p.y + v.y);
}

// ベクトル+ベクトル
function addVec(v1, v2) {
  return new Point(v1.x + v2.x, v1.y + v2.y);
}

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

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

// *** スライダー値設定 ***
function setV1x() {
  v1_x = v1x_slider.value();
}

function setV1y() {
  v1_y = v1y_slider.value();
}

function setScolar() {
  scolar = sc_slider.value();
}

              
            
!
999px

Console