Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?