HTML5 Is Far Left - Any Way To Center HTML5 In Browser?(SOLVED)

Hi,

Is there any way to center horizontally the game’s screen on HTML5 version?
Let me know, thanks!

J.

Yes, you can edit the html. If you want you can build from a template. See https://www.defold.com/manuals/html5/

1 Like

I’ll check that out, thanks!

J.

Hi,

I have the Defold HTML5 canvas centered horizontally now,
but the loading bar and [Full Screen] button are still all the way on the left?

How would I center everything?

Here is my code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

		<title>Red Light Racer 0.0</title>
		<style>
			/* Disable user selection to avoid strange bug in Chrome on Windows:
			 * Selecting a text outside the canvas, then clicking+draging would
			 * drag the selected text but block mouse down/up events to the engine.
			 */
			body {
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				-khtml-user-select: none;
				-moz-user-select: none;
				-ms-user-select: none;
				user-select: none;
			}

			.canvas-app-container {
			  background: rgb(255,255,255) no-repeat center url("try_game.png");
			  /* A positioned parent for loading visuals */
			  position: relative;
			}

			.canvas-app-container:-webkit-full-screen {
			  /* Auto width and height in Safari/Chrome fullscreen. */
			  width: auto;
			  height: auto;
			}

			.canvas-app-canvas {
				max-height: 100vh;
				max-width: 100%;
			}

			.canvas-app-progress {
			  position: absolute;
			  background-color: rgb(245, 245, 245);
			  height: 20px;
			  /* Progress same width as canvas. */
			  width: 360px;
			  bottom: 0px;
			}

			.canvas-app-progress-bar {
			  font-size: 12px;
			  height: 20px;
			  color: rgb(255, 255, 255);
			  background-color: rgb(30, 100, 234);
			  text-align: center;
			  line-height: 20px;
			}

			.button {
			  color: #fff;
			  background-color: #1e64ea;
			  border-color: transparent;
			  padding: 10px 20px;
			}
canvas {
    padding-left: 0;
    padding-right: 0;
    margin-left: auto;
    margin-right: auto;
    display: block;
    width: 360px;
}
    </style>
		
	</head>

	<body>
    <div id="fb-root"></div>

	<div id="app-container" class="canvas-app-container">
	    <canvas id="canvas" class="canvas-app-canvas" tabindex="1" width="360" height="640"></canvas>
	</div>

	<button id="fullscreen" class="button">Fullscreen</button>

	<!-- -->
	  
	  <script type='text/javascript' src="dmloader.js"></script>
	  <script type='text/javascript' src="Red Light Racer.js" async onload=""></script>
	<!-- -->

	<script type='text/javascript'>
		var extra_params = {
			archive_location_filter: function( path ) {
				return ("archive" + path + "");
			},
            
			splash_image: "splash_image.png",
			custom_heap_size: 268435456
		}
		
        Module.runApp("canvas", extra_params);

	    /* Fullscreen button */
	    document.getElementById("fullscreen").onclick = function (e) {
	      Module.toggleFullscreen();
	    };
	</script>
</body>
</html>

Thanks!

J.

canvas {
    padding-left: 0;
    padding-right: 0;
    margin-left: auto;
    margin-right: auto;
    display: block;
    width: 360px;
}

Maybe you can edit in this css

don’t understand, just want everything centered horizontally…

J.

Ok, made some progress…
I now have the HTML5 canvas and [Full Screen] button centered horizontally.
I can’t seem to center the progress bar though?

Here is the changes to the code:

			.button {
			  color: #fff;
			  background-color: #1e64ea;
			  border-color: transparent;
			  padding: 10px 20px;
			  
              padding-left: 0;
              padding-right: 0;
              margin-left: auto;
              margin-right: auto;
              display: block;
              width: 360px;
			}
			
            canvas {
                padding-left: 0;
                padding-right: 0;
                margin-left: auto;
                margin-right: auto;
                display: block;
                width: 360px;
            }
    </style>

Any help would be appreciated, thanks!

J.

Something like this should work

		<style>
			/* Disable user selection to avoid strange bug in Chrome on Windows:
			 * Selecting a text outside the canvas, then clicking+draging would
			 * drag the selected text but block mouse down/up events to the engine.
			 */
			body {
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				-khtml-user-select: none;
				-moz-user-select: none;
				-ms-user-select: none;
				user-select: none;
			}

			.canvas-app-container {
			  background: rgb(255,255,255) no-repeat center url("try_game.png");
			  /* A positioned parent for loading visuals */
			  position: relative;
			   width: 100%;
			   text-align:center;			  
			}

			.canvas-app-container:-webkit-full-screen {
			  /* Auto width and height in Safari/Chrome fullscreen. */
			  width: auto;
			  height: auto;
			}

			.canvas-app-canvas {
				max-height: 100vh;
				max-width: 100%;
				display: inline;
			}

			.canvas-app-progress {
			  background-color: rgb(245, 245, 245);
			  height: 20px;
			  /* Progress same width as canvas. */
			  width: 640px;
			  max-width: 640px;
			  bottom: 0px;
			  text-align: center;
			  margin: 0 auto;
			}

			.canvas-app-progress-bar {
			  font-size: 12px;
			  height: 20px;
			  color: rgb(255, 255, 255);
			  background-color: rgb(30, 100, 234);
			  text-align: center;
			  line-height: 20px;
			  max-width: 640px;
			  
			}

			.button {
			  color: #fff;
			  background-color: #1e64ea;
			  border-color: transparent;
			  padding: 10px 20px;
			}
			
			
		</style>

		
	</head>

	<body>
    <div id="fb-root"></div>

	<div id="app-container" class="canvas-app-container">
	    <canvas id="canvas" class="canvas-app-canvas" tabindex="1" width="640" height="1136"></canvas>
		<br>
		<button id="fullscreen" class="button">Fullscreen</button>
	</div>
1 Like

Thanks for the reply.
I tried your code, but it only shows the HTML5 canvas and not the loading bar or [Full Screen] button?
Any ideas, thanks!

J.

I don’t know, you probably used it improperly.

had to change below line to my game’s dimensions:

<canvas id="canvas" class="canvas-app-canvas" tabindex="1" width="360" height="640"></canvas>

Thanks!, moving forward…

J.