xxxxxxxxxx
let num = 100; //円の数
let vecLocation = []; //円の中心の位置ベクトル
let vecVelocity = []; //円の速度ベクトル
let s = 0;
function setup() {
createCanvas(windowWidth, windowHeight); //画面を生成
frameRate(60); //フレームレート
for (let i = 0; i < num; i++) {
vecLocation[i] = createVector(width / 2, height / 2);
vecVelocity[i] = createVector(random(-10, 10), random(-10, 10));
}
}
function draw() {
background(240, 100, 199) //背景を描画
noStroke(); //枠線なし
//円の数だけくりかえす
for (let i = 0; i < num; i++) {
vecLocation[i].add(vecVelocity[i]); //円の座標を更新
fill(random(255), random(255), random(255)); //塗りの色
ellipse(vecLocation[i].x, vecLocation[i].y,vecLocation[i].x/3, vecLocation[i].y/3 ); //指定した位置に円を描画
if (vecLocation[i].x < 0 || vecLocation[i].x > width || vecLocation[i].x < random(0, width/2) || vecLocation[i].x > random(width/2,width)) { //もし画面の左端、または右端に到達したら
vecVelocity[i].x = vecVelocity[i].x * -1; //X方向のスピードを反転
}
if (vecLocation[i].y < 0 || vecLocation[i].y > height || vecLocation[i].y < random(0, height/2) || vecLocation[i].y > random(height/2,height)) { //もし画面の下端、または上端に到達したら
vecVelocity[i].y = vecVelocity[i].y * -1; //Y方向のスピードを反転
}
}
}