LoginSignup
0
0

More than 5 years have passed since last update.

【メモ】CANVASの領域をなんちゃってレスポン

Posted at

仕様は
常にウインドウサイズ一杯まで広がる。
640px未満は通常の半分の高さになる。

sample.html
<canvas id="mycanvas">
</canvas>
sample.js
window.onload = function(){
  draw();
}
window.onresize = function(){
  draw();
}
function draw(){
  var canvas = document.getElementById('mycanvas');
  if (!canvas || !canvas.getContext) return false;
  canvas.width = document.documentElement.clientWidth;
  if(canvas.width > 640){
    canvas.height = 300;
  }else{
    canvas.height = 300 * 0.5;
  }
  var ctx = canvas.getContext('2d');
}

課題
onloadとonresizeで同じdraw()を実行させてるのが気持ち悪い。
同時に書けたりするのか調べる
JQuery的にこういうの
$(window).on('load resize', function(){
})

0
0
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
0
0