ToroVerse

作品:花火パーティクル

この回でできるようになること

夜空に、光の点が、すうっと上がり、ぱっと開いて、尾を引きながら、落ちていく。花火を、作ります。1つの花火は、何十個もの、小さな光の粒で、できています。粒の1つ1つに、同じ決まりを与えるだけで、全体が、花火の形になります。

sketch.js
let rockets = [];
let sparks = [];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
  background(240, 60, 8);
}

function draw() {
  blendMode(BLEND);
  background(240, 60, 8, 0.25);
  blendMode(ADD);

  // ときどき、自動で打ち上げる
  if (random(1) < 0.015) {
    rockets.push(new Rocket(random(60, 340), random(80, 220)));
  }

  for (let i = rockets.length - 1; i >= 0; i--) {
    rockets[i].update();
    rockets[i].show();
    if (rockets[i].isDone()) {
      explode(rockets[i].x, rockets[i].y, rockets[i].kind);
      rockets.splice(i, 1);
    }
  }

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  rockets.push(new Rocket(mouseX, mouseY));
}

function explode(x, y, kind) {
  if (kind === 0) {
    // 丸く開く
    let h = random(360);
    for (let i = 0; i < 80; i++) {
      sparks.push(new Spark(x, y, h, 2.2, 0.05, 70));
    }
  }
  if (kind === 1) {
    // しだれる、金色
    for (let i = 0; i < 100; i++) {
      sparks.push(new Spark(x, y, 45, 1.5, 0.09, 130));
    }
  }
  if (kind === 2) {
    // 2色
    let h = random(200);
    for (let i = 0; i < 50; i++) {
      sparks.push(new Spark(x, y, h, 2.8, 0.05, 70));
      sparks.push(new Spark(x, y, h + 150, 1.4, 0.05, 70));
    }
  }
}

class Rocket {
  constructor(x, targetY) {
    this.x = x;
    this.y = 400;
    this.targetY = targetY;

    let pick = random(1);
    this.kind = 0;
    if (pick > 0.4) {
      this.kind = 1;
    }
    if (pick > 0.7) {
      this.kind = 2;
    }
  }

  update() {
    this.y -= 6;
  }

  isDone() {
    return this.y <= this.targetY;
  }

  show() {
    noStroke();
    fill(40, 30, 100);
    circle(this.x, this.y, 4);
  }
}

class Spark {
  constructor(x, y, h, power, gravity, life) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, power);
    this.speedY = randomGaussian(0, power);
    this.hue = h;
    this.gravity = gravity;
    this.life = life;
    this.maxLife = life;
  }

  update() {
    this.speedX *= 0.97;
    this.speedY *= 0.97;
    this.speedY += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(this.hue, 80, 100, this.life / this.maxLife);
    circle(this.x, this.y, 4);
  }
}

ときどき、花火が、自動で上がります。キャンバスをクリックすると、その場所をめがけて、花火が打ち上がります。丸く開くもの、金色に垂れ下がるもの、2色のもの。3種類の花火が、でたらめに選ばれます。


この回が終わると、次のことができるようになります。

  • 1つの粒のふるまいを、クラスにまとめる
  • 粒を、1か所から、丸く飛び散らせる
  • 色と、消え方を整えて、光らしく見せる
  • 「打ち上がるもの」と「飛び散るもの」という、2種類のものを、別々の配列で扱う
  • 引数を変えて、花火の種類を増やす

花火を、粒に分けて考える

花火の全体を、いきなり描こうとすると、難しく感じます。1つの粒だけに、注目します。


花火の粒は、次のように、動きます。

順番 粒に起きること
1 破裂した場所から、ある向きへ、勢いよく飛び出す
2 空気に押し返されて、だんだん、遅くなる
3 重力に引かれて、下へ、落ち始める
4 光が弱くなって、消える

すべての粒が、この同じ決まりに、従っています。違うのは、最初に飛び出す向きと速さだけです。その違いだけで、粒は、ばらばらの方向へ散り、全体として、丸い花の形になります。


だから、作る順番は、こうなります。

順番 作るもの
1 粒1つを表す、クラス
2 粒を、たくさん、丸く飛び散らせる
3 色と、消え方
4 尾と、光の重なり
5 打ち上げ
6 花火の種類

ステップ1:粒のクラスを作る

粒1つを、Spark というクラスにします。Spark は、「火花」という意味です。

javascript
class Spark {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.speedX = random(-3, 3);
    this.speedY = random(-3, 3);
    this.life = 70;
  }
  // ...
}
プロパティ 意味
x、y 位置
speedX、speedY 速度。1フレームに進む量。でたらめに決める
life 寿命。残りのフレーム数

メソッドは、3つです。

メソッド すること
update() 重力を加え、位置を進め、寿命を減らす
isDead() 寿命が尽きたら、true を返す
show() 粒を描く

クリックされた場所から、80 個の粒を、いっぺんに作ります。

sketch.js
let sparks = [];

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(10, 10, 30);

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  for (let i = 0; i < 80; i++) {
    sparks.push(new Spark(mouseX, mouseY));
  }
}

class Spark {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.speedX = random(-3, 3);
    this.speedY = random(-3, 3);
    this.life = 70;
  }

  update() {
    this.speedY += 0.05;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(255);
    circle(this.x, this.y, 4);
  }
}

クリックすると、白い粒が、飛び散ります。重力に引かれて、下へ流れ、70 フレーム(約 1.2 秒)で、消えます。


draw の中では、配列を、後ろから前へたどっています。寿命が尽きた粒を、途中で取り除くので、前からたどると、粒を1つ、飛ばしてしまうからです。

形が、四角い

飛び散り方を、よく見てください。粒の広がりが、四角い形をしています。花火らしく、ありません。


原因は、速度の決め方に、あります。

sketch.js
this.speedX = random(-3, 3);
this.speedY = random(-3, 3);

横の速さも、縦の速さも、−3 から 3 の間で、同じ出やすさで、選んでいます。横が 3、縦が 3 の、斜めに速い粒も、同じくらい、たくさん生まれます。斜めの粒は、まっすぐ横へ飛ぶ粒より、遠くまで飛びます。だから、四隅が、とがった形になります。

ステップ2:丸く飛び散らせる

random を、randomGaussian に、取り替えます。

sketch.js
this.speedX = randomGaussian(0, 2.2);
this.speedY = randomGaussian(0, 2.2);

randomGaussian(真ん中, ばらつき) は、真ん中の値が出やすく、離れた値ほど、出にくい乱数です。0 の近くの、小さな速さが、よく出て、大きな速さは、たまにしか、出ません。

sketch.js
let sparks = [];

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(10, 10, 30);

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  for (let i = 0; i < 80; i++) {
    sparks.push(new Spark(mouseX, mouseY));
  }
}

class Spark {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, 2.2);
    this.speedY = randomGaussian(0, 2.2);
    this.life = 70;
  }

  update() {
    this.speedY += 0.05;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(255);
    circle(this.x, this.y, 4);
  }
}

広がりが、丸くなりました。変えたのは、2行だけです。


横も、縦も、この乱数で決めると、「横も縦も、大きい」という粒は、めったに生まれなくなります。両方が、めずらしい値になる必要があるからです。その結果、四隅が削られて、どの方向にも、同じように広がる、丸い形になります。


真ん中の近くに、粒が多く、外側ほど、まばらになります。中心が明るく、まわりが淡い、花火らしい濃淡も、ついでに、生まれています。

乱数 広がりの形 粒の密度
random(-3, 3) 四角 どこも、同じ
randomGaussian(0, 2.2) 丸 中心が濃く、外側が薄い

ステップ3:色と、消え方

白い粒を、色の付いた光に、します。

1発ごとに、色を決める

色は、色相・彩度・明るさで、指定します。setup の中に、colorMode(HSB); を、書きます。


色相は、1発の花火につき、1つ、決めます。80 個の粒が、すべて、同じ色です。

sketch.js
function mousePressed() {
  let h = random(360);
  for (let i = 0; i < 80; i++) {
    sparks.push(new Spark(mouseX, mouseY, h));
  }
}

let h = random(360); は、ループの外に、あります。ループの中に書くと、粒ごとに、ばらばらの色になり、まとまりが、なくなります。決めた色相は、コンストラクタの、3つ目の引数として、粒に、渡します。

だんだん、薄くする

粒が、ぱっと消えるのは、不自然です。寿命が減るにつれて、薄くします。

sketch.js
fill(this.hue, 80, 100, this.life / 70);

4つ目の数字は、透明度です。色相・彩度・明るさで指定するとき、透明度の範囲は、0 から 1 です。


this.life / 70 は、生まれたときに 1、寿命が尽きるときに 0 になります。寿命の最初の値 70 で割って、残りの割合に、直しています。

空気の抵抗で、遅くする

本物の花火の粒は、飛び出した直後が、いちばん速く、すぐに、失速します。毎フレーム、速度を、少しだけ小さくします。

sketch.js
this.speedX *= 0.97;
this.speedY *= 0.97;

1フレームごとに、速度が、97% になります。これを、繰り返すと、30 フレームで、約 40% まで、落ちます。


この2行があると、粒は、ぱっと広がって、ふわりと止まり、そこから、ゆっくり落ちるようになります。花火が、空中で、一瞬、形を保つ、あの見え方です。

sketch.js
let sparks = [];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
}

function draw() {
  background(240, 60, 8);

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  let h = random(360);
  for (let i = 0; i < 80; i++) {
    sparks.push(new Spark(mouseX, mouseY, h));
  }
}

class Spark {
  constructor(x, y, h) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, 2.2);
    this.speedY = randomGaussian(0, 2.2);
    this.hue = h;
    this.life = 70;
  }

  update() {
    this.speedX *= 0.97;
    this.speedY *= 0.97;
    this.speedY += 0.05;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(this.hue, 80, 100, this.life / 70);
    circle(this.x, this.y, 4);
  }
}

クリックするたびに、違う色の花火が、開きます。

クイズ

update() の中の、this.speedX *= 0.97; と this.speedY *= 0.97; を、取り除いたとします。花火の形は、どう変わるでしょう?

▶ 答えを確認する

粒が、止まらずに、広がり続けます。花火は、どんどん大きくなりながら、全体が、下へ流れていきます。


速度が、減らないので、横向きの速さは、最後まで、変わりません。粒は、消えるまで、外へ外へと、進み続けます。丸い形は、保たれますが、「開いて、止まる」という、花火らしい間が、なくなります。


空気の抵抗は、形を、ある大きさで、落ち着かせる役目を、しています。

ステップ4:尾と、光の重なり

光らしく見せる、仕上げを、2つ、加えます。

半透明の背景で、尾を引かせる

draw の最初の background を、半透明に、します。

sketch.js
background(240, 60, 8, 0.25);

前のフレームの絵が、完全には消えず、うっすらと、残ります。粒が通ったあとが、少しずつ薄くなりながら残り、尾に、なります。


setup の中でも、一度、background(240, 60, 8); と、不透明な色で、塗っておきます。最初のフレームから、暗い夜空に、するためです。

色を、光のように足し合わせる

blendMode(ADD); は、色の重ね方を、足し算に、変えます。重なったところが、足し合わされて、明るくなります。光を重ねたときと、同じ見え方です。


花火の中心は、粒が、たくさん重なっています。そこが、白く、まぶしく、光ります。


気を付けることが、1つ、あります。背景を塗るときは、重ね方を、ふつうに、戻しておきます。

sketch.js
blendMode(BLEND);
background(240, 60, 8, 0.25);
blendMode(ADD);

足し算のままで、背景を塗ると、暗い色を足すことになり、古い絵を、覆い隠せません。尾が、いつまでも消えずに、残ってしまいます。ふつうの重ね方で背景を塗り、足し算に切り替えてから、粒を描く。この順番を、毎フレーム、繰り返します。

sketch.js
let sparks = [];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
  background(240, 60, 8);
}

function draw() {
  blendMode(BLEND);
  background(240, 60, 8, 0.25);
  blendMode(ADD);

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  let h = random(360);
  for (let i = 0; i < 80; i++) {
    sparks.push(new Spark(mouseX, mouseY, h));
  }
}

class Spark {
  constructor(x, y, h) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, 2.2);
    this.speedY = randomGaussian(0, 2.2);
    this.hue = h;
    this.life = 70;
  }

  update() {
    this.speedX *= 0.97;
    this.speedY *= 0.97;
    this.speedY += 0.05;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(this.hue, 80, 100, this.life / 70);
    circle(this.x, this.y, 4);
  }
}

粒が、尾を引き、重なったところが、白く光ります。夜空の花火に、なりました。

ステップ5:打ち上げる

いまの花火は、クリックした場所で、いきなり、開きます。下から、打ち上がるようにします。

2種類のもの

打ち上げを、足すと、画面には、ふるまいの違う、2種類のものが、現れます。

種類 クラス ふるまい 最後
打ち上がる玉 Rocket 下から、まっすぐ上へ進む 目標の高さに着いたら、破裂する
飛び散る粒 Spark 破裂した場所から、四方へ散る 寿命が尽きたら、消える

それぞれを、別のクラスにして、別の配列で、管理します。

sketch.js
let rockets = [];
let sparks = [];

Rocket クラス

sketch.js
class Rocket {
  constructor(x, targetY) {
    this.x = x;
    this.y = 400;
    this.targetY = targetY;
  }

  update() {
    this.y -= 6;
  }

  isDone() {
    return this.y <= this.targetY;
  }

  show() {
    noStroke();
    fill(40, 30, 100);
    circle(this.x, this.y, 4);
  }
}

玉は、キャンバスの下の端(y = 400)から、生まれます。毎フレーム、6 ずつ、上へ進みます。targetY は、破裂する高さです。isDone() は、その高さに、届いたかどうかを、返します。

玉が、粒を生む

draw の中で、玉が、目標の高さに届いたら、その場所で、粒を作り、玉を、配列から取り除きます。

sketch.js
if (rockets[i].isDone()) {
  explode(rockets[i].x, rockets[i].y);
  rockets.splice(i, 1);
}

粒を作る処理は、explode という関数に、まとめます。「破裂する」という意味です。

sketch.js
function explode(x, y) {
  let h = random(360);
  for (let i = 0; i < 80; i++) {
    sparks.push(new Spark(x, y, h));
  }
}

順番が、大事です。先に、explode を呼び出して、粒を作ります。そのあとで、玉を、取り除きます。逆にすると、取り除いたあとでは、rockets[i] が、もう別の玉(または、何もない)を指していて、破裂する場所が、わからなくなります。


クリックされたときは、粒ではなく、玉を1つ、足します。

sketch.js
function mousePressed() {
  rockets.push(new Rocket(mouseX, mouseY));
}
sketch.js
let rockets = [];
let sparks = [];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
  background(240, 60, 8);
}

function draw() {
  blendMode(BLEND);
  background(240, 60, 8, 0.25);
  blendMode(ADD);

  for (let i = rockets.length - 1; i >= 0; i--) {
    rockets[i].update();
    rockets[i].show();
    if (rockets[i].isDone()) {
      explode(rockets[i].x, rockets[i].y);
      rockets.splice(i, 1);
    }
  }

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  rockets.push(new Rocket(mouseX, mouseY));
}

function explode(x, y) {
  let h = random(360);
  for (let i = 0; i < 80; i++) {
    sparks.push(new Spark(x, y, h));
  }
}

class Rocket {
  constructor(x, targetY) {
    this.x = x;
    this.y = 400;
    this.targetY = targetY;
  }

  update() {
    this.y -= 6;
  }

  isDone() {
    return this.y <= this.targetY;
  }

  show() {
    noStroke();
    fill(40, 30, 100);
    circle(this.x, this.y, 4);
  }
}

class Spark {
  constructor(x, y, h) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, 2.2);
    this.speedY = randomGaussian(0, 2.2);
    this.hue = h;
    this.life = 70;
  }

  update() {
    this.speedX *= 0.97;
    this.speedY *= 0.97;
    this.speedY += 0.05;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(this.hue, 80, 100, this.life / 70);
    circle(this.x, this.y, 4);
  }
}

クリックすると、その真下から、光の玉が上がり、クリックした高さで、開きます。


1つのものが、消えるときに、別の種類のものを、たくさん生む。玉が、粒に、バトンを渡す形です。配列を分けておいたので、玉の動きと、粒の動きを、別々に、考えられます。

ステップ6:種類を増やす

花火に、種類を、持たせます。

粒の性質を、引数にする

花火の見え方を決めているのは、粒の、次の数字です。

数字 いまの値 変えると
飛び出す勢い 2.2 大きいと、大きく開く
重力 0.05 大きいと、すぐに垂れ下がる
寿命 70 大きいと、長く光る
色相 でたらめ 花火の色

これらを、コンストラクタの引数に、します。

sketch.js
constructor(x, y, h, power, gravity, life) {
  this.x = x;
  this.y = y;
  this.speedX = randomGaussian(0, power);
  this.speedY = randomGaussian(0, power);
  this.hue = h;
  this.gravity = gravity;
  this.life = life;
  this.maxLife = life;
}

maxLife は、寿命の最初の値です。透明度を、this.life / this.maxLife で求めるために、覚えておきます。寿命が、粒によって違うようになったので、70 と、決まった数で、割れなくなったからです。


これで、同じクラスのまま、渡す数字を変えるだけで、違う花火が、作れます。

種類ごとに、粒の作り方を変える

explode に、種類を表す引数 kind を、足します。

kind 花火 粒の数 勢い 重力 寿命 色
0 丸く開く 80 2.2 0.05 70 でたらめな1色
1 しだれる 100 1.5 0.09 130 金色
2 2色 50 + 50 2.8 と 1.4 0.05 70 2つの色

「しだれる」花火は、勢いが弱く、重力が強く、寿命が長い粒で、できています。あまり広がらず、すぐに垂れ下がり、長く、尾を引きます。柳の枝のような形になります。


「2色」の花火は、勢いの違う粒を、2組、作ります。勢いの強い組が、外側の輪に、弱い組が、内側の芯に、なります。

玉が、種類を持つ

どの種類になるかは、玉が生まれたときに、決めます。

sketch.js
let pick = random(1);
this.kind = 0;
if (pick > 0.4) {
  this.kind = 1;
}
if (pick > 0.7) {
  this.kind = 2;
}
pick の値 kind 確率
0.4 以下 0 40%
0.4 より大きく、0.7 以下 1 30%
0.7 より大きい 2 30%

まず 0 にしておき、pick が境目を超えるたびに、上書きしています。


破裂するときに、玉の kind を、explode に、渡します。

sketch.js
explode(rockets[i].x, rockets[i].y, rockets[i].kind);
sketch.js
let rockets = [];
let sparks = [];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
  background(240, 60, 8);
}

function draw() {
  blendMode(BLEND);
  background(240, 60, 8, 0.25);
  blendMode(ADD);

  for (let i = rockets.length - 1; i >= 0; i--) {
    rockets[i].update();
    rockets[i].show();
    if (rockets[i].isDone()) {
      explode(rockets[i].x, rockets[i].y, rockets[i].kind);
      rockets.splice(i, 1);
    }
  }

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  rockets.push(new Rocket(mouseX, mouseY));
}

function explode(x, y, kind) {
  if (kind === 0) {
    // 丸く開く
    let h = random(360);
    for (let i = 0; i < 80; i++) {
      sparks.push(new Spark(x, y, h, 2.2, 0.05, 70));
    }
  }
  if (kind === 1) {
    // しだれる、金色
    for (let i = 0; i < 100; i++) {
      sparks.push(new Spark(x, y, 45, 1.5, 0.09, 130));
    }
  }
  if (kind === 2) {
    // 2色
    let h = random(200);
    for (let i = 0; i < 50; i++) {
      sparks.push(new Spark(x, y, h, 2.8, 0.05, 70));
      sparks.push(new Spark(x, y, h + 150, 1.4, 0.05, 70));
    }
  }
}

class Rocket {
  constructor(x, targetY) {
    this.x = x;
    this.y = 400;
    this.targetY = targetY;

    let pick = random(1);
    this.kind = 0;
    if (pick > 0.4) {
      this.kind = 1;
    }
    if (pick > 0.7) {
      this.kind = 2;
    }
  }

  update() {
    this.y -= 6;
  }

  isDone() {
    return this.y <= this.targetY;
  }

  show() {
    noStroke();
    fill(40, 30, 100);
    circle(this.x, this.y, 4);
  }
}

class Spark {
  constructor(x, y, h, power, gravity, life) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, power);
    this.speedY = randomGaussian(0, power);
    this.hue = h;
    this.gravity = gravity;
    this.life = life;
    this.maxLife = life;
  }

  update() {
    this.speedX *= 0.97;
    this.speedY *= 0.97;
    this.speedY += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(this.hue, 80, 100, this.life / this.maxLife);
    circle(this.x, this.y, 4);
  }
}

何度か、クリックしてください。3種類の花火が、でたらめに、上がります。

完成

最後に、クリックしなくても、ときどき、花火が上がるようにします。

sketch.js
if (random(1) < 0.015) {
  rockets.push(new Rocket(random(60, 340), random(80, 220)));
}

random(1) < 0.015 は、1.5% の確率で、成り立ちます。1秒間に 60 回、調べるので、平均すると、1秒に、約1発です。決まった間隔ではなく、でたらめな間隔で上がるので、続けて上がったり、間が空いたりします。本物の花火大会のような、リズムになります。

sketch.js
let rockets = [];
let sparks = [];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
  background(240, 60, 8);
}

function draw() {
  blendMode(BLEND);
  background(240, 60, 8, 0.25);
  blendMode(ADD);

  // ときどき、自動で打ち上げる
  if (random(1) < 0.015) {
    rockets.push(new Rocket(random(60, 340), random(80, 220)));
  }

  for (let i = rockets.length - 1; i >= 0; i--) {
    rockets[i].update();
    rockets[i].show();
    if (rockets[i].isDone()) {
      explode(rockets[i].x, rockets[i].y, rockets[i].kind);
      rockets.splice(i, 1);
    }
  }

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  rockets.push(new Rocket(mouseX, mouseY));
}

function explode(x, y, kind) {
  if (kind === 0) {
    // 丸く開く
    let h = random(360);
    for (let i = 0; i < 80; i++) {
      sparks.push(new Spark(x, y, h, 2.2, 0.05, 70));
    }
  }
  if (kind === 1) {
    // しだれる、金色
    for (let i = 0; i < 100; i++) {
      sparks.push(new Spark(x, y, 45, 1.5, 0.09, 130));
    }
  }
  if (kind === 2) {
    // 2色
    let h = random(200);
    for (let i = 0; i < 50; i++) {
      sparks.push(new Spark(x, y, h, 2.8, 0.05, 70));
      sparks.push(new Spark(x, y, h + 150, 1.4, 0.05, 70));
    }
  }
}

class Rocket {
  constructor(x, targetY) {
    this.x = x;
    this.y = 400;
    this.targetY = targetY;

    let pick = random(1);
    this.kind = 0;
    if (pick > 0.4) {
      this.kind = 1;
    }
    if (pick > 0.7) {
      this.kind = 2;
    }
  }

  update() {
    this.y -= 6;
  }

  isDone() {
    return this.y <= this.targetY;
  }

  show() {
    noStroke();
    fill(40, 30, 100);
    circle(this.x, this.y, 4);
  }
}

class Spark {
  constructor(x, y, h, power, gravity, life) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, power);
    this.speedY = randomGaussian(0, power);
    this.hue = h;
    this.gravity = gravity;
    this.life = life;
    this.maxLife = life;
  }

  update() {
    this.speedX *= 0.97;
    this.speedY *= 0.97;
    this.speedY += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(this.hue, 80, 100, this.life / this.maxLife);
    circle(this.x, this.y, 4);
  }
}

プログラムの全体の形を、まとめます。

部分 役割
rockets、sparks 玉と、粒を、入れておく配列
draw 背景を、半透明で塗る。玉を進め、着いたら破裂させる。粒を進め、消えたら取り除く
mousePressed 玉を1つ、足す
explode 種類に応じて、粒を、まとめて作る
Rocket クラス 玉1つの、ふるまい
Spark クラス 粒1つの、ふるまい

draw の中には、花火の形についての記述が、1つも、ありません。玉が、どう上がるか。粒が、どう散るか。それは、すべて、クラスの中に、書かれています。draw は、「進めて、描いて、終わったら、取り除く」を、繰り返しているだけです。

粒の数について

1発の花火で、80 個から 100 個の粒が、生まれます。続けて、クリックすると、画面には、何百個もの粒が、同時に、存在することになります。

同時に出ている花火 粒の数の目安
1 発 約 100
5 発 約 500
20 発 約 2000

数千個までは、なめらかに動きます。粒は、寿命が尽きると、配列から取り除かれるので、数は、増え続けません。作る数と、消える数が、つり合うところで、落ち着きます。


粒を取り除く処理を、書き忘れると、見えない粒が、配列に残り続け、だんだん、動きが重くなります。生まれたものは、必ず、消す。パーティクルを扱うときの、大事な決まりです。

やってみよう

次の3つを作ってください。

  1. 粒が、消える直前に、白く、きらっと光るようにする。寿命が、残り 8 フレームより少なくなったら、白い、少し大きな円で描く
  2. クリックすると、3発が、同時に上がるようにする。クリックした場所と、その左下、右下に、1発ずつ
  3. 4つ目の種類を、足す。粒の数が多く、勢いが強く、すぐ消える、大きな花火にする。出る確率は、15% にする
▶ 1. 消える直前に、光る

show() の中で、寿命を見て、描き方を分けます。彩度を 0、明るさを 100 にすると、白になります。

sketch.js
let rockets = [];
let sparks = [];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
  background(240, 60, 8);
}

function draw() {
  blendMode(BLEND);
  background(240, 60, 8, 0.25);
  blendMode(ADD);

  // ときどき、自動で打ち上げる
  if (random(1) < 0.015) {
    rockets.push(new Rocket(random(60, 340), random(80, 220)));
  }

  for (let i = rockets.length - 1; i >= 0; i--) {
    rockets[i].update();
    rockets[i].show();
    if (rockets[i].isDone()) {
      explode(rockets[i].x, rockets[i].y, rockets[i].kind);
      rockets.splice(i, 1);
    }
  }

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  rockets.push(new Rocket(mouseX, mouseY));
}

function explode(x, y, kind) {
  if (kind === 0) {
    // 丸く開く
    let h = random(360);
    for (let i = 0; i < 80; i++) {
      sparks.push(new Spark(x, y, h, 2.2, 0.05, 70));
    }
  }
  if (kind === 1) {
    // しだれる、金色
    for (let i = 0; i < 100; i++) {
      sparks.push(new Spark(x, y, 45, 1.5, 0.09, 130));
    }
  }
  if (kind === 2) {
    // 2色
    let h = random(200);
    for (let i = 0; i < 50; i++) {
      sparks.push(new Spark(x, y, h, 2.8, 0.05, 70));
      sparks.push(new Spark(x, y, h + 150, 1.4, 0.05, 70));
    }
  }
}

class Rocket {
  constructor(x, targetY) {
    this.x = x;
    this.y = 400;
    this.targetY = targetY;

    let pick = random(1);
    this.kind = 0;
    if (pick > 0.4) {
      this.kind = 1;
    }
    if (pick > 0.7) {
      this.kind = 2;
    }
  }

  update() {
    this.y -= 6;
  }

  isDone() {
    return this.y <= this.targetY;
  }

  show() {
    noStroke();
    fill(40, 30, 100);
    circle(this.x, this.y, 4);
  }
}

class Spark {
  constructor(x, y, h, power, gravity, life) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, power);
    this.speedY = randomGaussian(0, power);
    this.hue = h;
    this.gravity = gravity;
    this.life = life;
    this.maxLife = life;
  }

  update() {
    this.speedX *= 0.97;
    this.speedY *= 0.97;
    this.speedY += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    if (this.life < 8) {
      fill(0, 0, 100);
      circle(this.x, this.y, 6);
    } else {
      fill(this.hue, 80, 100, this.life / this.maxLife);
      circle(this.x, this.y, 4);
    }
  }
}

花火が消える瞬間に、粒が、いっせいに、白くまたたきます。

▶ 2. 3発を、同時に上げる

mousePressed の中で、玉を、3つ足します。位置を、少しずつ、ずらします。

sketch.js
let rockets = [];
let sparks = [];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
  background(240, 60, 8);
}

function draw() {
  blendMode(BLEND);
  background(240, 60, 8, 0.25);
  blendMode(ADD);

  // ときどき、自動で打ち上げる
  if (random(1) < 0.015) {
    rockets.push(new Rocket(random(60, 340), random(80, 220)));
  }

  for (let i = rockets.length - 1; i >= 0; i--) {
    rockets[i].update();
    rockets[i].show();
    if (rockets[i].isDone()) {
      explode(rockets[i].x, rockets[i].y, rockets[i].kind);
      rockets.splice(i, 1);
    }
  }

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  rockets.push(new Rocket(mouseX, mouseY));
  rockets.push(new Rocket(mouseX - 70, mouseY + 40));
  rockets.push(new Rocket(mouseX + 70, mouseY + 40));
}

function explode(x, y, kind) {
  if (kind === 0) {
    // 丸く開く
    let h = random(360);
    for (let i = 0; i < 80; i++) {
      sparks.push(new Spark(x, y, h, 2.2, 0.05, 70));
    }
  }
  if (kind === 1) {
    // しだれる、金色
    for (let i = 0; i < 100; i++) {
      sparks.push(new Spark(x, y, 45, 1.5, 0.09, 130));
    }
  }
  if (kind === 2) {
    // 2色
    let h = random(200);
    for (let i = 0; i < 50; i++) {
      sparks.push(new Spark(x, y, h, 2.8, 0.05, 70));
      sparks.push(new Spark(x, y, h + 150, 1.4, 0.05, 70));
    }
  }
}

class Rocket {
  constructor(x, targetY) {
    this.x = x;
    this.y = 400;
    this.targetY = targetY;

    let pick = random(1);
    this.kind = 0;
    if (pick > 0.4) {
      this.kind = 1;
    }
    if (pick > 0.7) {
      this.kind = 2;
    }
  }

  update() {
    this.y -= 6;
  }

  isDone() {
    return this.y <= this.targetY;
  }

  show() {
    noStroke();
    fill(40, 30, 100);
    circle(this.x, this.y, 4);
  }
}

class Spark {
  constructor(x, y, h, power, gravity, life) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, power);
    this.speedY = randomGaussian(0, power);
    this.hue = h;
    this.gravity = gravity;
    this.life = life;
    this.maxLife = life;
  }

  update() {
    this.speedX *= 0.97;
    this.speedY *= 0.97;
    this.speedY += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(this.hue, 80, 100, this.life / this.maxLife);
    circle(this.x, this.y, 4);
  }
}

左右の2発は、目標の高さが、40 低いので、真ん中の1発より、少し早く、開きます。

▶ 3. 4つ目の種類を足す

直すのは、2か所です。玉が種類を決めるところに、境目を1つ足し、explode に、kind === 3 の場合を、足します。

sketch.js
let rockets = [];
let sparks = [];

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
  background(240, 60, 8);
}

function draw() {
  blendMode(BLEND);
  background(240, 60, 8, 0.25);
  blendMode(ADD);

  // ときどき、自動で打ち上げる
  if (random(1) < 0.015) {
    rockets.push(new Rocket(random(60, 340), random(80, 220)));
  }

  for (let i = rockets.length - 1; i >= 0; i--) {
    rockets[i].update();
    rockets[i].show();
    if (rockets[i].isDone()) {
      explode(rockets[i].x, rockets[i].y, rockets[i].kind);
      rockets.splice(i, 1);
    }
  }

  for (let i = sparks.length - 1; i >= 0; i--) {
    sparks[i].update();
    sparks[i].show();
    if (sparks[i].isDead()) {
      sparks.splice(i, 1);
    }
  }
}

function mousePressed() {
  rockets.push(new Rocket(mouseX, mouseY));
}

function explode(x, y, kind) {
  if (kind === 0) {
    // 丸く開く
    let h = random(360);
    for (let i = 0; i < 80; i++) {
      sparks.push(new Spark(x, y, h, 2.2, 0.05, 70));
    }
  }
  if (kind === 1) {
    // しだれる、金色
    for (let i = 0; i < 100; i++) {
      sparks.push(new Spark(x, y, 45, 1.5, 0.09, 130));
    }
  }
  if (kind === 2) {
    // 2色
    let h = random(200);
    for (let i = 0; i < 50; i++) {
      sparks.push(new Spark(x, y, h, 2.8, 0.05, 70));
      sparks.push(new Spark(x, y, h + 150, 1.4, 0.05, 70));
    }
  }
  if (kind === 3) {
    // 大きく広がる、白に近い色
    for (let i = 0; i < 160; i++) {
      sparks.push(new Spark(x, y, 200, 3.6, 0.03, 50));
    }
  }
}

class Rocket {
  constructor(x, targetY) {
    this.x = x;
    this.y = 400;
    this.targetY = targetY;

    let pick = random(1);
    this.kind = 0;
    if (pick > 0.4) {
      this.kind = 1;
    }
    if (pick > 0.7) {
      this.kind = 2;
    }
    if (pick > 0.85) {
      this.kind = 3;
    }
  }

  update() {
    this.y -= 6;
  }

  isDone() {
    return this.y <= this.targetY;
  }

  show() {
    noStroke();
    fill(40, 30, 100);
    circle(this.x, this.y, 4);
  }
}

class Spark {
  constructor(x, y, h, power, gravity, life) {
    this.x = x;
    this.y = y;
    this.speedX = randomGaussian(0, power);
    this.speedY = randomGaussian(0, power);
    this.hue = h;
    this.gravity = gravity;
    this.life = life;
    this.maxLife = life;
  }

  update() {
    this.speedX *= 0.97;
    this.speedY *= 0.97;
    this.speedY += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY;
    this.life -= 1;
  }

  isDead() {
    return this.life <= 0;
  }

  show() {
    noStroke();
    fill(this.hue, 80, 100, this.life / this.maxLife);
    circle(this.x, this.y, 4);
  }
}

Rocket クラスにも、Spark クラスにも、draw にも、手を付けていません。粒の性質を、引数にしておいたので、数字の組み合わせを1つ足すだけで、新しい花火が、作れます。

まとめ

  • 花火は、同じ決まりで動く、たくさんの粒で、できている。粒1つのふるまいを、クラスにまとめる
  • 速度を randomGaussian で決めると、粒が、丸く広がり、中心が濃くなる
  • 色は、1発につき1つ、ループの外で決めて、粒に渡す
  • 透明度を、寿命の残りの割合にすると、だんだん薄くなって消える
  • 速度に、毎フレーム、1 より少し小さい数を掛けると、開いたあと、ふわりと止まる
  • 半透明の背景で、尾が残る。blendMode(ADD) で、重なりが明るく光る。背景を塗る前に、重ね方を戻す
  • ふるまいの違うものは、別のクラス、別の配列にする。玉が終わるときに、粒を生む
  • 粒の性質を、コンストラクタの引数にすると、同じクラスで、違う種類の花火を作れる
  • 生まれたものは、必ず、配列から取り除く