読者です 読者をやめる 読者になる 読者になる

Fly higher! Sky is the limit!

webの現場で働く人のブログ

【canvas】画像を回転させながら落とす

最近、インタラクティブな実装に興味を持ち始めました。
覚え書きとして、canvasで画像を用いて回転させながら落ちるアニメーションの実装方法を記しておきます。

DEMO

補足
- 画像を複数個使用することを想定して、配列を用いています。

JavaScriptコード

$(function(){
var canvas = document.getElementById ('canvas');
var ctx = canvas.getContext ('2d');

function canvasbg(pageY,pageX,flag){
  var counter1 = 0;
  var counter2 = 0;
  var TO_RADIANS = Math.PI/180;
  var SPEED = 5;
  var X = pageX;
  var Y = pageY;

  function start() {

    x = [];
    y = [];

    /*初期座標*/
    x[0] = X / 2;
    y[0] = 0;
    
    x[1] = 150;
    y[1] = 0;

    img = [];
    img[0] = new Image ();
    img[0].src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAxCAMAAACvdQotAAAABGdBTUEAALGPC/xhBQAAAwBQTFRFTS8SjHlse1Q4iXRisKCUtJmBlHVdkH5qmoVwqJF5iGNHZ0UpZ0csXC8SUSYTg2pUXjwgbU41g2xQYEAkVDYa1cKxdFRBl3Vbk3dkYz8giGpXZUYwj3ZippWDzbina00zsZ2Jbkouup6KdVc9a084n4h4nIdxgmpVmHxpf2JPhGdPeVpBwqmWeVlF1L+wqpeGvKubw7GhY0QpXzwkVjMWdVQ85dDB59jJUzMXrpiG49C/vKOQ4NDC2se13Mi1u6mYAAAAjmxYjmpIo56dsbe3k2BEl31uqI5ywMHCxK+QsZR7n5CD9/DeqWhKCg0RWllZaWVkNDMysKmlYmBhi4ySJyUk7dvGU1JUjWE9gVQucEkrilgtekkkfUwWnmREUCQLm2A0dEkSy7qpVRwC5uryx7qyHRwZbG1rd3d33+Xlg1lBhm5XimREyLOd5e3v6ufk08a8XSgNrJF2nGY8iE0mnGI3fkUjYjkRej8S4+LhAQABERAOCgoKfFk8FBQTgmFDyM3QLCwt5NbLbUQjooJtjWpNhWtP1sS3Z0ccfU0n+ff31c3MkVclWC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fLqRsAUe18KThFAt0R9BYpbWjo4UMCoQeHfIDEpeOC2gRchpxCkUgQCIQUzNZRogKITerASylDIBDQwC8SsVg8ONjd3T02NtadPAjjd4HAYGKJIhSZlGoAdlNJeyO1HADB0NAAGN4Ohve8UYQgHkwGfHCwC4MhiMWEhwOIVyhTvcmt1DNsEJIRFImY4I+rRRFRKFQtLiMjo62tDaNYbGysp7u9vSgjI70P+6XF9IHRVcb3NVCpVCLecOsK1U2bdmvoGzZ0ElF9nQ0N1L6+Pt+GTiwRa7hVVdf632NpbLN2u5HRdjUzq+mHSNtKx2zHLlWt7evXa2hofP2V1todZlaWs9qFtYW6mY26xcz+ZmlibrFOfedOGxsb9Z3rLMyt3/t2sfiXq3nzNyGTwPWnqzsiAAAAAElFTkSuQmCC";
    // img[2] = new Image ();
    // img[2].src = "";
    // img[3] = new Image ();
    // img[3].src = "";
    // img[4] = new Image ();
    // img[4].src = "";
    // img[5] = new Image ();
    // img[5].src = "";

    var time;
    time = setInterval(function(){
      draw(X,Y);
    },SPEED);

  }

  function draw(X,Y) {
    ctx.clearRect (0 , 0 , pageX , pageY);
    drawRotatedImage(img[0],x[0], y[0],counter1);
    drawRotatedImage(img[1],x[1], y[1],counter2);

    /*回転用 角度*/
    counter1 += 0.5;
    counter2 += 0.3; 

    /*落下スピード*/
    x[0] -= 1 * Math.random();
    y[0] += 2;
  
    x[1] -= 1 * Math.random();
    y[1] += 1;

    /*再描画判定座標*/
    if(y[0] > pageY + 20){
      x[0] = pageX / 2;
      y[0] = 0;
    }
    if(y[1] > pageY + 20){
      x[1] = pageX / 3;
      y[1] = 0;
    }
  }

  /*回転*/
  function drawRotatedImage(image, x, y, angle) {
    ctx.save(); 
    ctx.translate(x, y);
    ctx.rotate(angle * TO_RADIANS);
    ctx.drawImage(image, -(image.width/2), -(image.height/2));
    ctx.restore();
  }

  /*ループ*/
  window.requestAnimationFrame = (function(){
    return  window.requestAnimationFrame   ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame    ||
        window.oRequestAnimationFrame      ||
        window.msRequestAnimationFrame     ||
        function(callback){
          window.setTimeout(callback, 1000 / 60);
        };
  })();

  requestAnimationFrame(start);
  sizing();
}

  function sizing(){
    $('#canvas').attr({height:$('#wrap').height()});
    $('#canvas').attr({width:$('#wrap').width()});
  }

  $(window).load(function(){
    pageY = $('#wrap').height();
    pageX = $('#wrap').width();
    canvasbg(pageY,pageX,0);
  });
  
});
  • 初期座標、イメージパスを指定する。
  • 画像を描画する際に回転用の関数を使用する。
  • 角度をつける(インクリメント)
  • 落下スピードを指定する。
  • 最下部までいったら元の位置に戻す。

応用として、「雪」「落ち葉」「雨」などにも適応できそうです。