67

How do I markup a page with an HTML5 canvas such that the canvas

  1. Takes up 80% of the width

  2. Has a corresponding pixel height and width which effectively define the ratio (and are proportionally maintained when the canvas is stretched to 80%)

  3. Is centered both vertically and horizontally

You can assume that the canvas is the only thing on the page, but feel free to encapsulate it in divs if necessary.

Vukašin Manojlović
  • 2,645
  • 2
  • 21
  • 26
user122147
  • 1,323
  • 1
  • 12
  • 13

13 Answers13

51

This will center the canvas horizontally:

#canvas-container {
   width: 100%;
   text-align:center;
}

canvas {
   display: inline;
}

HTML:

<div id="canvas-container">
   <canvas>Your browser doesn't support canvas</canvas>
</div>
Vukašin Manojlović
  • 2,645
  • 2
  • 21
  • 26
Ali OKTAY
  • 527
  • 4
  • 2
26

Looking at the current answers I feel that one easy and clean fix is missing. Just in case someone passes by and looks for the right solution. I am quite successful with some simple CSS and javascript.

Center canvas to middle of the screen or parent element. No wrapping.

HTML:

<canvas id="canvas" width="400" height="300">No canvas support</canvas>

CSS:

#canvas {
    position: absolute;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;
    margin:auto;
}

Javascript:

window.onload = window.onresize = function() {
    var canvas = document.getElementById('canvas');
    canvas.width = window.innerWidth * 0.8;
    canvas.height = window.innerHeight * 0.8;
}

Works like a charm - tested: firefox, chrome

fiddle: http://jsfiddle.net/djwave28/j6cffppa/3/

Daniel
  • 4,816
  • 3
  • 27
  • 31
15

easiest way

put the canvas into paragraph tags like this:

<p align="center">
  <canvas id="myCanvas" style="background:#220000" width="700" height="500" align="right"></canvas>
</p>
Dorian
  • 22,759
  • 8
  • 120
  • 116
J the Helper
  • 151
  • 1
  • 2
  • I don't think this addresses OPs desire to have the canvas scale to 80% of the width of its parent while maintaining its aspect ratio of width to height. – Le Mot Juiced Mar 12 '21 at 14:37
7

Tested only on Firefox:

<script>
window.onload = window.onresize = function() {
    var C = 0.8;        // canvas width to viewport width ratio
    var W_TO_H = 2/1;   // canvas width to canvas height ratio
    var el = document.getElementById("a");

    // For IE compatibility http://www.google.com/search?q=get+viewport+size+js
    var viewportWidth = window.innerWidth;
    var viewportHeight = window.innerHeight;

    var canvasWidth = viewportWidth * C;
    var canvasHeight = canvasWidth / W_TO_H;
    el.style.position = "fixed";
    el.setAttribute("width", canvasWidth);
    el.setAttribute("height", canvasHeight);
    el.style.top = (viewportHeight - canvasHeight) / 2;
    el.style.left = (viewportWidth - canvasWidth) / 2;

    window.ctx = el.getContext("2d");
    ctx.clearRect(0,0,canvasWidth,canvasHeight);
    ctx.fillStyle = 'yellow';
    ctx.moveTo(0, canvasHeight/2);
    ctx.lineTo(canvasWidth/2, 0);
    ctx.lineTo(canvasWidth, canvasHeight/2);
    ctx.lineTo(canvasWidth/2, canvasHeight);
    ctx.lineTo(0, canvasHeight/2);
    ctx.fill()
}
</script>

<body>
<canvas id="a" style="background: black">
</canvas>
</body>
Nickolay
  • 31,095
  • 13
  • 107
  • 185
  • 1
    @Mark you can not call the `onload`-event in an `onload`-event ;) http://jsfiddle.net/FUCur/121/ – yckart Apr 25 '13 at 08:46
  • @yckart: I just copied and pasted what Nickolay put; I didn't even think about that. Explains why it doesn't render initially though :-) – mpen Apr 25 '13 at 18:05
  • Good answer. You can clean up the drawing code by doing a single scale() call at the beginning, then you can eliminate all the canvas relative drawing code. Example here: https://stackoverflow.com/a/63642064/670530 – DoomGoober Aug 29 '20 at 00:10
4

in order to center the canvas within the window +"px" should be added to el.style.top and el.style.left.

el.style.top = (viewportHeight - canvasHeight) / 2 +"px";
el.style.left = (viewportWidth - canvasWidth) / 2 +"px";
Shawn
  • 549
  • 5
  • 5
1

Simple:

<body>
    <div>
        <div style="width: 800px; height:500px; margin: 50px auto;">
            <canvas width="800" height="500" style="background:#CCC">
             Your browser does not support HTML5 Canvas.
            </canvas>
        </div>
    </div>
</body>
  • I don't think this addresses OPs desire to have the canvas scale to 80% of the width of its parent while maintaining its aspect ratio of width to height. – Le Mot Juiced Mar 12 '21 at 14:37
1

Resizing canvas using css is not a good idea. It should be done using Javascript. See the below function which does it

function setCanvas(){

   var canvasNode = document.getElementById('xCanvas');

   var pw = canvasNode.parentNode.clientWidth;
   var ph = canvasNode.parentNode.clientHeight;

   canvasNode.height = pw * 0.8 * (canvasNode.height/canvasNode.width);  
   canvasNode.width = pw * 0.8;
   canvasNode.style.top = (ph-canvasNode.height)/2 + "px";
   canvasNode.style.left = (pw-canvasNode.width)/2 + "px";


}

demo here : http://jsfiddle.net/9Rmwt/11/show/

.

Diode
  • 24,570
  • 8
  • 40
  • 51
0

Given that canvas is nothing without JavaScript, use JavaScript too for sizing and positionning (you know: onresize, position:absolute, etc.)

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
0

Same codes from Nickolay above, but tested on IE9 and chrome (and removed the extra rendering):

window.onload = window.onresize = function() {
   var canvas = document.getElementById('canvas');
   var viewportWidth = window.innerWidth;
   var viewportHeight = window.innerHeight;
   var canvasWidth = viewportWidth * 0.8;
   var canvasHeight = canvasWidth / 2;

   canvas.style.position = "absolute";
   canvas.setAttribute("width", canvasWidth);
   canvas.setAttribute("height", canvasHeight);
   canvas.style.top = (viewportHeight - canvasHeight) / 2 + "px";
   canvas.style.left = (viewportWidth - canvasWidth) / 2 + "px";
}

HTML:

<body>
  <canvas id="canvas" style="background: #ffffff">
     Canvas is not supported.
  </canvas>
</body>

The top and left offset only works when I add px.

yckart
  • 32,460
  • 9
  • 122
  • 129
laishiekai
  • 841
  • 1
  • 13
  • 26
0

Make a line in the center and make it transparent. This line will be the fulcrum to center the content in the canvas

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.strokeStyle = 'transparent';
context.moveTo(width/2, 0);
context.lineTo(width/2, height);
context.stroke();
context.textAlign = 'center';

with width height being the size of the html canvas

Dev Lởm
  • 11
  • 2
0

With bootstrap 5

<div class="container d-flex justify-content-center">
    <div style="width: 800px;"><canvas id="acquisitions"></canvas></div>
</div>

centered canvas image

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
0

As to the CSS suggestion:

#myCanvas { 
 width: 100%;
 height: 100%;
}

By the standard, CSS does not size the canvas coordinate system, it scales the content. In Chrome, the CSS mentioned will scale the canvas up or down to fit the browser's layout. In the typical case where the coordinate system is smaller than the browser's dimensions in pixels, this effectively lowers the resolution of your drawing. It most likely results in non-proportional drawing as well.

jerseyboy
  • 1,298
  • 13
  • 13
-1

Wrapping it with div should work. I tested it in Firefox, Chrome on Fedora 13 (demo).

#content {
   width: 95%;
   height: 95%;
   margin: auto;
}

#myCanvas {
   width: 100%;
   height: 100%;
   border: 1px solid black;
}

And the canvas should be enclosed in tag

<div id="content">
    <canvas id="myCanvas">Your browser doesn't support canvas tag</canvas>
</div>

Let me know if it works. Cheers.

Vukašin Manojlović
  • 2,645
  • 2
  • 21
  • 26
Sainath Mallidi
  • 515
  • 1
  • 7
  • 17