メインコンテンツへスキップ
見出し画像

AA風 花火アニメーションのメイキング

    忙しかったり体調くずしたりで書けなかった↓の作品メイキングを書きます!!!元気!!!!!

    参加するぞ! #CreativeCoding花火大会 #p5js でアスキーアート風の花火を打ち上げてみたよ🎇
    createDiv()で描いてるからテキストとして選択もできる!

    OpenProcessinghttps://t.co/kwuahNVciw
    NEORThttps://t.co/ZSaUD66LML pic.twitter.com/qKVlRfDfol

    — レオナ (@reona396) August 1, 2024

    アスキーアート化のしくみについてはこちらを参考にしていました。

    上記の講座の中にある「ASCII Image DOM」のコードを特に参考にしました。

    しくみとしてはこんな感じのようです。

    1. densityで置き換え後の文字列を濃度順になるように定義
      const density = "Ñ@#W$9876543210?!abc;:+=-,._ ";

    2. 画像を読み込む

    3. 画像のピクセルをひとつずつチェックして、そのピクセルのRGB値の平均(ざっくりとした明るさ)をとる

    4. 明るさに応じてdensityの文字に置き換える。明るいところであればÑや@といった文字、暗いところであれば_やスペースに置き換える

    5. 置き換え作業した文字列を1行ずつcreateDivでDOMにdiv要素として挿入していく

    ここでサンプルとして利用されている画像は48px x 48pxのイヌの写真です。かなり小さな画像ですね。
    全ピクセルを参照するので大きすぎる画像は利用するのが難しそうです。
    また、サンプルコードでは画像ファイルを利用されていますが、p5.jsのcreateGraphics関数を利用すればスケッチ内で作成したビジュアルを読み込ませることができそうです。


    というわけで自分の作品のメイキングです。
    コードはOpenProcessingやNEORTにも投稿してあります。
    が、今回のメイキング記事では掲載にあたってちょこっと整えてあるので差があっても気にしないでください…

    まずは花火のアニメーションの土台をつくっていきます。
    10個の円を同じ位置のまま打ち上げます。
    一定の高さになったら下からまた打ち上げるように戻します。
    AA化の準備のため、あらかじめcreateGraphicsしてそこに描画していきます。
    画面の大きさも作業しやすいようにちょっと大きめにしておきます。

    画像
    花火の打ち上げ部分
    let shapes = [];
    const shapesNum = 10;
    let topHeight;
    
    function setup() {
      createCanvas(200, 200);
      pixelDensity(1);
    
      pg = createGraphics(width, height);
      pg.angleMode(DEGREES);
      pg.pixelDensity(1);
    
      pg.background(0);
      pg.noStroke();
    
      topHeight = pg.height * 0.45;
    
      for (let i = 0; i < shapesNum; i++) {
        shapes.push(new Shape(i));
      }
    }
    
    function draw() {
      pg.background(0);
    
      for (let i = 0; i < shapes.length; i++) {
        shapes[i].move();
        shapes[i].display();
      }
    
      image(pg, 0, 0);
    }
    
    class Shape {
      constructor(tmpIndex) {
        this.x = pg.width / 2;
        this.d = pg.width * 0.05;
        this.y = pg.height + this.d / 2;
        this.t = (tmpIndex * 360) / shapesNum;
      }
    
      move() {
        this.y -= 2;
        if (this.y < topHeight) {
          this.y = pg.height + this.d / 2;
        }
      }
    
      display() {
        pg.fill(255);
        pg.circle(this.x, this.y, this.d);
      }
    }

    花火の打ち上げ後、火花が広がるアニメーションを作っていきます。
    「打ち上げ中」の状態が終わり次第、火花が放射状に広がるように実装していきます。

    画像
    花火の広がり
    let shapes = [];
    const shapesNum = 10;
    let topHeight;
    let hanabiSize;
    
    function setup() {
      createCanvas(200, 200);
      pixelDensity(1);
    
      pg = createGraphics(width, height);
      pg.angleMode(DEGREES);
      pg.pixelDensity(1);
    
      pg.background(0);
      pg.noStroke();
    
      topHeight = pg.height * 0.45;
      hanabiSize = pg.width * 0.2;
    
      for (let i = 0; i < shapesNum; i++) {
        shapes.push(new Shape(i));
      }
    }
    
    function draw() {
      pg.background(0);
    
      for (let i = 0; i < shapes.length; i++) {
        shapes[i].move();
        shapes[i].display();
      }
    
      image(pg, 0, 0);
    }
    
    class Shape {
      constructor(tmpIndex) {
        this.x = pg.width / 2;
        this.d = pg.width * 0.05;
        this.y = pg.height + this.d / 2;
        this.t = (tmpIndex * 360) / shapesNum;
        this.isLaunching = true;
        this.r = 0;
      }
    
      move() {
        if (this.isLaunching) {
          this.y -= 2;
          if (this.y < topHeight) {
            this.isLaunching = false;
          }
        } else {
          this.x = this.r * pg.cos(this.t) + pg.width / 2;
          this.y = this.r * pg.sin(this.t) + topHeight;
    
          this.r += 0.65;
    
          if (this.r > hanabiSize) {
            this.r = 0;
            this.y = pg.height + this.d / 2;
            this.x = pg.width / 2;
            this.isLaunching = true;
          }
        }
      }
    
      display() {
        pg.fill(255);
        pg.circle(this.x, this.y, this.d);
      }
    }

    火花の消え方を工夫していきます。
    火花が広がりながら、火花のサイズを小さく・透明度を高くなるように調整して、アニメーション全体を整えていきます。
    また、アニメーション速度をゆっくりめに調整しておきます。

    画像
    花火のアニメーションを整えていく

    現在、createGraphicsで作成したサイズは画面サイズと同じく200 x 200なので、これを50 x 50に調整します。
    この50 x 50のcreateGraphicsの範囲を走査し、明るさに応じて文字に置き換えます。
    また、noCanvasを利用してcanvasをなくし、DOM操作をしてdivの行として文字を表示していきます。
    また、背景の透明度やフレームレートを調整し、火花の尾が引くようなアニメーションにします。

    画像
    アニメーションを文字に置き換え
    let shapes = [];
    const shapesNum = 10;
    let topHeight;
    let hanabiSize;
    const density = "Ñ@#W$9876543210?!abc;:+=-,_ ";
    
    function setup() {
      noCanvas();
      frameRate(20);
    
      pg = createGraphics(50, 50);
      pg.angleMode(DEGREES);
      pg.pixelDensity(1);
    
      pg.background(0);
      pg.noStroke();
    
      topHeight = pg.height * 0.45;
      hanabiSize = pg.width * 0.4;
    
      for (let i = 0; i < shapesNum; i++) {
        shapes.push(new Shape(i));
      }
    }
    
    function draw() {
      removeElements();
    
      pg.background(0, 150);
    
      for (let i = 0; i < shapes.length; i++) {
        shapes[i].move();
        shapes[i].display();
      }
    
      pg.loadPixels();
      for (let j = 0; j < pg.height; j++) {
        let tmpRow = "";
        for (let i = 0; i < pg.width; i++) {
          const pixelIndex = (i + j * pg.width) * 4;
          const r = pg.pixels[pixelIndex + 0];
          const g = pg.pixels[pixelIndex + 1];
          const b = pg.pixels[pixelIndex + 2];
    
          const avg = (r + g + b) / 3;
    
          const len = density.length;
          const charIndex = floor(map(avg, 0, 255, len - 1, 0));
    
          const c = density.charAt(charIndex);
          if (c == " ") tmpRow += "&nbsp;";
          else tmpRow += c;
        }
    
        createDiv(tmpRow);
      }
    }
    
    class Shape {
      constructor(tmpIndex) {
        this.t = (tmpIndex * 360) / shapesNum;
        this.init();
      }
    
      init() {
        this.x = pg.width / 2;
        this.d = pg.width * 0.05;
        this.y = pg.height + this.d / 2;
        this.isLaunching = true;
        this.r = 0;
        this.alpha = 100;
      }
    
      move() {
        if (this.isLaunching) {
          this.y -= 0.75;
          if (this.y < topHeight) {
            this.isLaunching = false;
          }
        } else {
          this.x = this.r * pg.cos(this.t) + pg.width / 2;
          this.y = this.r * pg.sin(this.t) + topHeight;
    
          this.r += 0.65;
          this.alpha = map(this.r, 0, hanabiSize, 100, 0);
          this.d = map(this.r, 0, hanabiSize, pg.width * 0.05, pg.width * 0.01);
    
          if (this.r > hanabiSize) {
            this.init();
          }
        }
      }
    
      display() {
        pg.fill(255, this.alpha);
        pg.circle(this.x, this.y, this.d);
      }
    }

    表示にあたってはCSSも調整しておきます。
    フォントや文字サイズ、背景色などを設定します。

    body {
            margin: 0;
            padding: 0;
            background-color: #000;
            font-family: "Courier";
            color: #fff;
            line-height: 1;
            font-size: 1rem;
    }

    これだけでもいいんですが、より花火っぽく見せるために地面を描き足します。
    ついでにコードにコメントを追加しておきました。

    画像
    地面を追加
    let shapes = [];
    const shapesNum = 10;
    let topHeight;  // 花火が打ち上がる高さ
    let hanabiSize;  // 花火が広がるサイズ
    
    // 文字の置き換え順番
    // 左側の方が明るい部分に置き換わる
    const density = "Ñ@#W$9876543210?!abc;:+=-,_ ";
    
    function setup() {
      // キャンバスは設置しない
      noCanvas();
      frameRate(20);
    
      // アニメーション生成用にcreateGraphicsする
      pg = createGraphics(50, 50);
      pg.angleMode(DEGREES);
      pg.pixelDensity(1);
    
      pg.background(0);
      pg.noStroke();
    
      topHeight = pg.height * 0.45;
      hanabiSize = pg.width * 0.4;
    
      for (let i = 0; i < shapesNum; i++) {
        shapes.push(new Shape(i));
      }
    }
    
    function draw() {
      // divの内容を消す
      // 実質、背景の更新
      removeElements();
    
      // アニメーションの背景更新
      pg.background(0, 150);
      
      // 地面の描画
      pg.fill(255);
      pg.rect(0, pg.height - pg.height * 0.1, pg.width, pg.height * 0.1);
    
      // 火花のオブジェクトの処理
      for (let i = 0; i < shapes.length; i++) {
        shapes[i].move();
        shapes[i].display();
      }
    
      // createGraphicしたアニメーションに対して
      // ピクセル情報を読み込む
      pg.loadPixels();
      for (let j = 0; j < pg.height; j++) {
    
        // 1行ごとに処理
        let tmpRow = "";
        for (let i = 0; i < pg.width; i++) {
          const pixelIndex = (i + j * pg.width) * 4;
          const r = pg.pixels[pixelIndex + 0];
          const g = pg.pixels[pixelIndex + 1];
          const b = pg.pixels[pixelIndex + 2];
    
          // ピクセルの明るさを取得
          const avg = (r + g + b) / 3;
    
          // ピクセルの明るさに応じて文字と置き換える
          const len = density.length;
          const charIndex = floor(map(avg, 0, 255, len - 1, 0));
          const c = density.charAt(charIndex);
          
          // スペースの場合は "&nbsp;" として行に挿入
          if (c == " ") tmpRow += "&nbsp;";
          else tmpRow += c;
        }
    
        // 行をdivとして追加
        createDiv(tmpRow);
      }
    }
    
    class Shape {
      constructor(tmpIndex) {
        this.t = (tmpIndex * 360) / shapesNum;
        this.init();
      }
    
      // 初期化用関数
      init() {
        this.x = pg.width / 2;
        this.d = pg.width * 0.05;
        this.y = pg.height + this.d / 2;
        this.isLaunching = true;
        this.r = 0;
        this.alpha = 100;
      }
    
      // 火花の動きをコントロールする関数
      move() {
        // 打ち上げている時
        if (this.isLaunching) {
          this.y -= 0.75;
          
          // 指定した高さになったら打ち上げをやめる
          if (this.y < topHeight) {
            this.isLaunching = false;
          }
        }
        // 火花が広がっている時 
        else {
          this.x = this.r * pg.cos(this.t) + pg.width / 2;
          this.y = this.r * pg.sin(this.t) + topHeight;
    
          // 火花の広がりを広げる
          this.r += 0.65;
          // 火花が広がるにつれて火花が透明になるように
          this.alpha = map(this.r, 0, hanabiSize, 100, 0);
          // 火花が広がるにつれて火花のサイズが小さくなるように
          this.d = map(this.r, 0, hanabiSize, pg.width * 0.05, pg.width * 0.01);
    
          // 広がりが指定の高さになったら初期化
          if (this.r > hanabiSize) {
            this.init();
          }
        }
      }
    
      // 表示用関数
      display() {
        pg.fill(255, this.alpha);
        pg.circle(this.x, this.y, this.d);
      }
    }

    これで完成です!


    createGraphicsで作ったアニメーションを文字に置き換えてAA風アニメーションつくるメイキングでした〜
    簡単につくれるのでぜひぜひやってみてください!!

    ちなみにDOM操作なのでキャンバスや動画ではなく文字情報になっています。
    ためしにOpenProcessing上で全選択コピー→メモ帳にペーストしてみてください〜

     
     
    プログラミングでおえかきしています!p5.js(Processing)が大好きなインタラクションエンジニア/フロントエンドエンジニア/クリエイティブコーダー! 技術ブログや作品などの更新をお知らせします。 月末や年末にエッセイを書くのも好きです。

    あなたへのおすすめ