LoginSignup
13
13

More than 5 years have passed since last update.

canvasサイズをwindowサイズにする方法

Last updated at Posted at 2014-07-07

canvasはcssでwidth:100%;とかheight:100%;とか使えないみたいなので、jsでwindowのresizeイベントで都度widthとheightを設定していく。
※canvasのcssではなく属性のwidthとheightで直接設定しないとダメみたい。
※ところどころjQueryも使用(楽なので)。

javascript
window.onload = function(){

    // stageを生成
    // ※canvasタグのid名を引数に指定
    var stage = new createjs.Stage('canvas');

    // windowのリサイズ設定
    var id;
    $(window).on('resize', function(e){
        clearTimeout(id);
        id = setTimeout(function(){
            stage.canvas.width = $(e.target).width();
            stage.canvas.height = $(e.target).height();
        }, 100);
    });
    $(window).trigger('resize');

}

resizeイベントの度に再設定するとパフォーマンス的にアレみたいなので、setTimeoutを使用して設定頻度を調整している。(とりあえず100msにしてみた。)

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13