18
12

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の解像度

Last updated at Posted at 2017-05-22

解像度2倍
= 750pxのcanvasをつくって、375pxで表示

canvas.width = 750px
ctx.scale(2,2);
canvas.style.width = 375px

となればいい。

const dpr = window.devicePixelRatio || 1;

function onWindowResize() {
    width = window.innerWidth;
    height = window.innerHeight;
    
    canvas.width = width * dpr;
    canvas.height = height * dpr;
  
    ctx.scale(dpr,dpr);
    
    canvas.style.width = width + 'px';
    canvas.style.height = height + 'px';
}

canvasが重くなければ、
const dpr = window.devicePixelRatio || 1;
で解像度ぴったりのcanvasを描画

逆にcanvasが重い場合は、
dpr = 0.5などで強制的に解像度を下げる。

参考
https://codepen.io/mo4_9/pen/LyXzob
http://dotinstall.com/lessons/basic_canvas_v2/39905

18
12
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
18
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?