36

How can I make canvas be 100% in width and height of the page?

Sergei Basharov
  • 47,526
  • 63
  • 186
  • 320
  • 1
    possible duplicate of [Resize HTML5 canvas to fit window](http://stackoverflow.com/questions/1664785/resize-html5-canvas-to-fit-window) – Jukka Suomela Jun 20 '14 at 14:28

7 Answers7

37

Well I have it working here: Are Google's Bouncing Balls HTML5? by using the following CSS:

* { margin: 0; padding: 0;}

body, html { height:100%; }

#c {
    position:absolute;
    width:100%;
    height:100%;
}

Where #c is the id of the canvas element.

Ian Devlin
  • 18,092
  • 5
  • 56
  • 71
  • 23
    No, this doesn't work. It just stretches the image to fill the page, but it doesn't change the internal width and height of the canvas. The result is just a blurry mess. – zett42 Aug 07 '19 at 19:55
16

you can use these codes without jquery

var dimension = [document.documentElement.clientWidth, document.documentElement.clientHeight];
var c = document.getElementById("canvas");
c.width = dimension[0];
c.height = dimension[1];
Didats Triadi
  • 1,492
  • 2
  • 14
  • 13
9
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

maybe that easy?

Pixsa
  • 481
  • 6
  • 15
8

This has something to do with <canvas> tag.

when create fullscreen canvas, <canvas> will cause scrollbar if not set to display:block.

detail: http://browser.colla.me/show/canvas_cannot_have_exact_size_to_fullscreen

sunderls
  • 672
  • 7
  • 7
1

on my observations this runs effectively, and gives a blue shade


var c = document.getElementById('can');
  var ctx = canvas.getContext('2d');
  ctx.rect(0, 0, canvas.width, canvas.height);
  // add linear gradient
  var g = ctx.createLinearGradient(0, 0, c.width, c.height);
  // light blue color
  g.addColorStop(0, '#8ED6FF');   
  // dark blue color
  g.addColorStop(1, '#004CB3');
  context.fillStyle = g;
  context.fill();

<script>
var c = document.getElementById('can');
      var ctx = canvas.getContext('2d');
      ctx.rect(0, 0, canvas.width, canvas.height);

      // add linear gradient
      var g = ctx.createLinearGradient(0, 0, c.width, c.height);
      // light blue color
      g.addColorStop(0, '#8ED6FF');   
      // dark blue color
      g.addColorStop(1, '#004CB3');
      context.fillStyle = g;
      context.fill();
</scrip
html,body
{
 height:98.0%;
 width:99.5%;
}
canvas
{
 display:block;
 width:100%;
 height:100%;
}
<html>
<body>
<canvas id="can"></canvas>
</body>
  </html>
Gilles
  • 8,961
  • 4
  • 32
  • 51
1

You can programatically set the canvas width + height:

// Using jQuery to get window width + height.
canvasObject.width = $(window).width();
canvasObject.height = $(window).height();

I've tested this and it as long as you redraw what's on the canvas after you've resized it won't change the scaling.

Castrohenge
  • 7,876
  • 5
  • 36
  • 60
0

Does this work for you?

<html>
  <body style="height: 100%; margin: 0;">
    <canvas style="width: 100%; height: 100%;"></canvas>
  </body>
</html>

from Force canvas element in html to take up the entire window?

Community
  • 1
  • 1
Kintaro
  • 1,277
  • 11
  • 17
  • 5
    It sets width to 100%, but doesn't make it in height 100%. More than that, the objects is scaled too and looks like a small raster image had been enlarged so it looks weird. What can I do about this? – Sergei Basharov Sep 14 '10 at 08:28