ANTON072
1/15/2015 - 3:32 PM

canvas particle http://bl.ocks.org/ANTON072/raw/06262651e9b67934c52b/

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body {
      margin: 0;
      padding: 0;
      background-color: #000;
    }
  </style>
</head>
<body>

<script>
  window.onload = function() {
    // Creating the Canvas
    var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');
    var particles = {};
    var particleIndex = 0;

    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    canvas.id = 'motion';
    document.body.appendChild(canvas);

    context.fillStyle = 'black';
    context.fillRect(0, 0, canvas.width, canvas.height);

    var y_fourth = Math.floor(canvas.height / 4);
    var y_second_fourth = Math.floor(y_fourth * 2);

    function Particle() {
      var x = 0;
      var random_y = Math.floor(Math.random() * y_second_fourth + y_fourth) + 1;
//      console.log(random_x, random_y);
      this.x = x;
      this.y = random_y;
      this.vx = Math.random() * 5 - 2;
      this.vy = Math.random() * 2 - 1;
      this.gravity = 0;
      particleIndex++;
      particles[particleIndex] = this;
//      console.log(particles);
      this.id = particleIndex;
      this.size = Math.random() * 6 - 2;
      // 色相、彩度、明度、アルファ値
      // 明度をランダムにしている
      this.color = 'hsla(0, 0%,' + parseInt(Math.random() * 100, 0) + '%, 1)';
      this.color2 = 'hsla(360, 100%,' + parseInt(Math.random() * 100, 0) + '%, 1)';
      this.color3 = 'hsla(210, 100%,' + parseInt(Math.random() * 100, 0) + '%, 1)';
      // 恣意的な偏りのランダム値を生成するための変数
      this.color_selector = Math.random() * 150 - 1;
    }

    Particle.prototype.draw = function() {
      this.x += this.vx;
      this.y += this.vy;
      this.vy += this.gravity; // 揚力
      if (this.x > canvas.width || this.y > canvas.height) {
        delete particles[this.id]; // 削除するためにオブジェクトに保存
      }
//      console.log('a');
      if (this.color_selector < 30 && this.color_selector > 10) {
        context.fillStyle = this.color2;
//        context.fillStyle = 'red';
      }
      else if(this.color_selector < 10){
        context.fillStyle = this.color3;
//        context.fillStyle = 'blue';
      }
      else {
        // ここが一番多い
//        context.fillStyle = 'yellow';
        context.fillStyle = this.color;
      }
      context.fillRect(this.x, this.y, this.size, this.size);
    };

    setInterval(function () {
      context.fillStyle = 'black'; // 上塗り
      context.fillRect(0, 0, canvas.width, canvas.height);
      new Particle();
      for (var i in particles) {
        particles[i].draw();
      }
    }, 30);



  };
</script>
</body>
</html>