Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to make HTML5 Canvas goes full screen

Posted on February 13, 2017August 4, 2020 by Habibie

Sometime you want to create HTML5 with specific dimension, and then you make browser zooms on it to full screen.

A way to make it happened is to style the canvas tag so it will fill the screen. For example: style=”width: 100%”. With this method the canvas width will be the width of the screen (browser). However, this will stretch your contents and may not be satisfying.

But sometime you want to create dynamic HTML5 canvas that its dimension derived from user’s browser’s width and height without canvas’s contents. What we can do is first to style body element like this: Then we create canvas, and after creating canvas, we create script for it like this:

<script>
	var canvas = document.getElementById("canvas");
	var screenw = window.innerWidth;
	var screenh = window.innerHeight;
	canvas.width = screenw;
	canvas.height = screenh;
	var ctx = canvas.getContext("2d");
	ctx.moveTo(0,0);
	ctx.lineTo(screenw,screenh);
	ctx.stroke();
	ctx.font = "30px Arial";
	ctx.fillText("Hello World",100,100);
</script>

Viola… and the result is this:

And here is the full script:

<!DOCTYPE html>
<html>
	<head>
		<title>FullScreen Canvas</title>
	</head>
	<body style="margin:0 auto; overflow:hidden">
		<canvas id="canvas">
		Your browser does not support the HTML5 canvas tag.</canvas>
		<script>
			var canvas = document.getElementById("canvas");
			var screenw = window.innerWidth;
			var screenh = window.innerHeight;
			canvas.width = screenw;
			canvas.height = screenh;
			var ctx = canvas.getContext("2d");
			ctx.moveTo(0,0);
			ctx.lineTo(screenw,screenh);
			ctx.stroke();
			ctx.font = "30px Arial";
			ctx.fillText("Hello World",100,100);
		</script>
	</body>
</html>
Post Views: 488
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme