HTML + CSS

画面幅いっぱいに表示させたいのでcanvasをdivで囲んでます。
canvas-txtにcanvasの上に表示されるコンテンツを配置します。

html

  1. <div id="canvas-wrap">
  2. <canvas id="canvas-container"></canvas>
  3. <div id="canvas-txt"><p>CONTENTS</p></div>
  4. </div>

CSSです。
今回はwidth,heightとも100%を指定してますが、pxで数値を指定しても問題ありません。

css

  1. html,body {
  2. height: 100%;
  3. }
  4.  
  5. #canvas-wrap {
  6. width: 100%;
  7. height: 100%;
  8. position: relative;
  9. }
  10. #canvas-txt {
  11. font-size: 60px;
  12. display: flex;
  13. justify-content: center;
  14. align-items: center;
  15. position: absolute;
  16. left: 0;
  17. top: 0;
  18. width: 100%;
  19. height: 100%;
  20. }

画面サイズいっぱいにCanvasを配置する

単純にcanvasを100%にすると、比率が崩れてしまいます。
一旦親のサイズを取得して設定することで100%表示にすることができます。

javascript

  1. var canvasWrap = document.querySelector('#canvas-wrap');
  2. var canvas = document.querySelector('#canvas-container');
  3. var ctx = canvas.getContext('2d');
  4. canvas.setAttribute('width', canvasWrap.offsetWidth);
  5. canvas.setAttribute('height', canvasWrap.offsetHeight);

ランダムな方向に移動するパーティクル

ランダムにパーティクルを配置して、ランダムな方向に移動させてみます。

javascript

  1. window.requestAnimFrame = (function () {
  2. return window.requestAnimationFrame ||
  3. window.webkitRequestAnimationFrame ||
  4. window.mozRequestAnimationFrame ||
  5. window.oRequestAnimationFrame ||
  6. window.msRequestAnimationFrame ||
  7. function (callback) {
  8. window.setTimeout(callback, 1000 / 60);
  9. };
  10. })();
  11.  
  12. window.onload = function() {
  13. var canvasWrap = document.querySelector('#canvas-wrap');
  14. var canvas = document.querySelector('#canvas-container');
  15. var ctx = canvas.getContext('2d');
  16.  
  17. var center = {}; // Canvas中央
  18. var dots = []; // パーティクル配列
  19. var density = 70; //パーティクルの数
  20. var colors = ['#eeb900', '#6DD0A5', '#f799db'];
  21. var baseSize = 3; // 大きさ
  22. var baseSpeed = 10; // スピード
  23.  
  24. var Dot = function () {
  25. this.size = Math.floor( Math.random() * 6 ) + baseSize; //大きさ
  26. this.color = colors[~~(Math.random() * 3)]; //色
  27. this.speed = this.size / baseSpeed; // 大きさによって速度変更
  28. this.pos = { // 位置
  29. x: Math.random() * canvas.width,
  30. y: Math.random() * canvas.height
  31. };
  32. var rot = Math.random() * 360; // ランダムな角度
  33. var angle = rot * Math.PI / 180;
  34.  
  35. this.vec = { // 移動方向
  36. x: Math.cos(angle) * this.speed,
  37. y: Math.sin(angle) * this.speed
  38. };
  39. };
  40. Dot.prototype = {
  41. update: function() {
  42. this.draw();
  43.  
  44. this.pos.x += this.vec.x;
  45. this.pos.y += this.vec.y;
  46.  
  47. // 画面外に出たら反対へ再配置
  48. if(this.pos.x > canvas.width + 10) {
  49. this.pos.x = -5;
  50. } else if(this.pos.x < 0 - 10) {
  51. this.pos.x = canvas.width + 5;
  52. } else if(this.pos.y > canvas.height + 10) {
  53. this.pos.y = -5;
  54. } else if(this.pos.y < 0 - 10) {
  55. this.pos.y = canvas.height + 5;
  56. }
  57. },
  58.  
  59. draw: function() {
  60. ctx.fillStyle = this.color;
  61. ctx.beginPath();
  62. ctx.arc(this.pos.x, this.pos.y, this.size, 0, 2 * Math.PI, false);
  63. ctx.fill();
  64. }
  65. };
  66.  
  67. function update() {
  68. requestAnimFrame(update);
  69. // 描画をクリアー
  70. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  71.  
  72. for (var i = 0; i < density; i++) {
  73. dots[i].update();
  74. }
  75. }
  76.  
  77. function init() {
  78. // canvasにコンテンツサイズをセット
  79. canvas.setAttribute("width", canvasWrap.offsetWidth);
  80. canvas.setAttribute("height", canvasWrap.offsetHeight);
  81.  
  82. // canvas中央をセット
  83. center.x = canvas.width / 2;
  84. center.y = canvas.height / 2;
  85.  
  86. // densityの数だけパーティクルを生成
  87. for (var i = 0; i < density; i++) {
  88. dots.push(new Dot());
  89. }
  90. update();
  91. }
  92. init();
  93. }

ほとんどがCanvasアニメーションの基本的な部分になりますが、重要なところは32行目あたりからです。
横方向の場合単純にx軸の値を変更していけばよかったのですが、斜めに移動させたいときはy軸も変動させる必要があります。
角度から移動方向のx、yを決めるには三角関数で結果がでます。
実際は難しい事考える必要はなく、例えば45度に移動したいときは下記のようなコードになります。

  1. var angle = 45 * Math.PI / 180;
  2. this.vec = {
  3. x: Math.cos(angle),
  4. y: Math.sin(angle)
  5. };

vecが移動方向になりますので、updateのときにこと値を足す事で指定した方向へ移動することができます。

  1. this.pos.x += this.vec.x;
  2. this.pos.y += this.vec.y;

中央から拡散するパーティクル

中央から外側に拡散するような動きをしてみます。
Dot部分のみ変更します。

javascript

  1. var Dot = function () {
  2. this.size = Math.floor( Math.random() * 6 ) + baseSize; //大きさ
  3. this.color = colors[~~(Math.random() * 3)]; //色
  4. this.speed = this.size / baseSpeed; // 大きさによって速度変更
  5.  
  6. this.pos = { // 位置
  7. x: canvas.width / 2,
  8. y: canvas.height / 2
  9. };
  10.  
  11. var rot = Math.random() * 360; // ランダムな角度
  12. var angle = rot * Math.PI / 180;
  13.  
  14. this.vec = { // 移動方向
  15. x: Math.cos(angle) * this.speed,
  16. y: Math.sin(angle) * this.speed
  17. };
  18.  
  19. // ランダムに配置
  20. var startRandom = Math.random();
  21. this.pos.x += this.vec.x * (startRandom * center.x);
  22. this.pos.y += this.vec.y * (startRandom * center.y);
  23. };
  24.  
  25. Dot.prototype = {
  26. update: function() {
  27. this.pos.x += this.vec.x;
  28. this.pos.y += this.vec.y;
  29.  
  30. if(this.pos.x > canvas.width + baseSize
  31. || this.pos.x < 0 - baseSize
  32. || this.pos.y > canvas.height + baseSize
  33. || this.pos.y < 0 - baseSize) {
  34. this.pos.x = center.x;
  35. this.pos.y = center.y;
  36. }
  37. },
  38. draw: function() {
  39. ctx.fillStyle = this.color;
  40. ctx.beginPath();
  41. ctx.arc(this.pos.x, this.pos.y, this.size, 0, 2 * Math.PI, false);
  42. ctx.fill();
  43. }
  44. };

開始位置のposをcanvas中央に指定して、画面外に出た時も再配置も同じくcanvas中央にしているだけですね。

この作りだとパーティクルの数を直接指定しているので、画面サイズによって密度が変わってきてしまいます。
この部分は画面サイズによって変動させるなどの処理が必要になってくると思います。