Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  101] [ 1]  / answers: 1 / hits: 40524  / 8 Years ago, sun, september 18, 2016, 12:00:00

How would one make the JavaScript canvas (or whatever the 400px by 400px drawing area is called) larger than 400x400?



I have a 1440p screen and when I run my JS files through an HTML file, the canvas is not surprisingly a small 400px by 400px box in the top left.



Is there anything I can do to expand the canvas to a specified height and width?


More From » html

 Answers
17

The following jsfiddle demonstrates how to resize the canvas. https://jsfiddle.net/intrinsica/msj0cwx3/





(function() {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d');

// Event handler to resize the canvas when the document view is changed
window.addEventListener('resize', resizeCanvas, false);

function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

// Redraw everything after resizing the window
drawStuff();
}
resizeCanvas();

function drawStuff() {
// Do drawing here
context.strokeRect(10,10, 230,100);
context.font = '16px serif';
context.fillText('The canvas is the blue', 30, 30);
context.fillText('background color seen here.', 30, 50);
context.fillText('It will resize if the window', 30, 70);
context.fillText('size is adjusted.', 30, 90);
}
})();

* { margin:0; padding:0; } /* to remove the top and left whitespace */

html, body { width:100%; height:100%; } /* just to be sure these are full screen*/

canvas {
background: #77f; /* to show the canvas bounds */
display:block; /* to remove the scrollbars */
}

<canvas id=canvas></canvas>




[#60677] Thursday, September 15, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mollys

Total Points: 183
Total Questions: 95
Total Answers: 85

Location: Vanuatu
Member since Fri, May 13, 2022
2 Years ago
;