Responsive Canvas using JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Responsive Canvas using JavaScript

What is the best, and least complicated, way to make a JavaScript created canvas automatically adjust to the width and height of the device screen?

21st Sep 2018, 11:24 PM
ThThinka
ThThinka - avatar
2 Answers
+ 1
Add this window.onload = function() { var canvas = document.getElementsByTagName("canvas")[0]; canvas.width = window.screen.availWidth; canvas.height = window.screen.availHeight; } https://code.sololearn.com/WWFBdeU0nnZf/?ref=app
22nd Sep 2018, 2:06 AM
Calviղ
Calviղ - avatar
+ 1
Thank you! This worked perfectly!
22nd Sep 2018, 12:58 PM
ThThinka
ThThinka - avatar