LoginSignup
1
2

More than 5 years have passed since last update.

canvasのサイズをjQueryで変更する

Last updated at Posted at 2017-11-11

jQueryでcanvasのサイズを設定する。
cssでサイズ設定していると、初期サイズ(幅300px、高さ150px)からの拡大・縮小となる。

index.html
    <canvas id="idCanvas"></canvas>
main.js
   $('#idCanvas').attr('width' , 500);
   $('#idCanvas').attr('height', 500);
main.css
  /* 初期サイズからの拡大・縮小 */
  #idCanvas {
    width  : 200px;
    height : 200px;
  }

追記
cssでのサイズ指定は、canvasを拡大・縮小するものであり、サイズを直接変えるものではありません(画像が縮んだり伸びたりします)。
サイズを直接変える場合は、下記を使用します。

main.js
    //canvas
    canvas = document.getElementById('idCanvas');

    //サイズ指定
    canvas.width  = 500;
    canvas.height = 500;
1
2
4

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
1
2