Get height and width from the phaser canvas

So I use phaser.io , but I want to print an array to create it on my html page.

To set the height and width of my array, I need to know the height and width of my canvas.

So, I just have to do:

$('canvas').height();

Mission completed.

But there will be no trap, my canvas looks like this:

<canvas width="1261" height="1101" style="display: block; touch-action: none; user-select: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 1059px; height: 925px; cursor: inherit; margin-left: 1px; margin-right: 0px; margin-top: 0px;"></canvas>

And every time he returns me a value from a height not out of style.

So I try differently:

$('canvas').css('height');
$('canvas').[0].style.height;
$('canvas')[0].style.cssText;

But I always get the wrong result

which is super weird when I do this:

console.log($('canvas')[0].style)

I definitely see the result that I want in the element "height" and "cssText". Here I give a part of the result:

 0:"display"
    1:"touch-action"
    2:"user-select"
    3:"-webkit-tap-highlight-color"
    4:"width"
    5:"height"
    6:"margin-left"
    7:"margin-right"
    8:"margin-top"
    ...
    cssText:"display: block; touch-action: none; user-select: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 1059px; height: 925px; margin-left: 1px; margin-right: 0px; margin-top: 0px;"
    ...
    height:"925px"
    ...

Can anyone tell me why? and giving me a solution can be nice too.

======= EDIT ========

, $('canvas'). height() , . , , , :

console.log($('canvas')[0].style.cssText);
console.log($('canvas').height());

. , ... .

window.onload = function() {
	
	var Level = {
		preload: function()
		{
			console.log("toto");
		},
        create: function()
		{
			console.log($('canvas').height());
			console.log($('canvas')[0].style.cssText);
			
			this.scale.pageAlignHorizontally = true;
		    this.scale.pageAlignVertically = true;
		    this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
            this.scale.refresh();
		}
    }
	const SAFE_ZONE_WIDTH=640;
	const SAFE_ZONE_HEIGHT=1101;

	var w = window.innerWidth ;
	var h = window.innerHeight ;
	var aspectRatioDevice = w/h;

	var aspectRatioSafeZone = SAFE_ZONE_WIDTH / SAFE_ZONE_HEIGHT;
	var extraWidth = 0, extraHeight = 0, offsetWidth = 0;
	if (aspectRatioSafeZone < aspectRatioDevice) 
	{
		// have to add game pixels horizontally in order to fill the device screen
		extraWidth = aspectRatioDevice * SAFE_ZONE_HEIGHT - SAFE_ZONE_WIDTH;
		offsetWidth = extraWidth/2;
	} 
	else 
	{
		// have to add game pixels vertically
		extraHeight = SAFE_ZONE_WIDTH / aspectRatioDevice - SAFE_ZONE_HEIGHT;
	}

	var game = new Phaser.Game( SAFE_ZONE_WIDTH + extraWidth, SAFE_ZONE_HEIGHT + extraHeight, Phaser.CANVAS, 'game_div');

	game.state.add('Level', Level);
	game.state.start('Level');
}
<!DOCTYPE HTML>
<html>
<head>

<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, height = device-height, user-scalable=no, target-densitydpi = device-dpi" />
<title>niuniu</title>
<script>
    screen.orientation.lock('portrait');
</script>
<script src="https://github.com/photonstorm/phaser-ce/releases/download/v2.9.4/phaser.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
	
</body>
</html>
Hide result
+4
1

$('canvas').height()

$('canvas').attr('height')

$('canvas').css('height')

console.log($('canvas').height());
console.log($('canvas').css('height'));
console.log($('canvas').attr('height'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas width="1261" height="1101" style="display: block; touch-action: none; user-select: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 1059px; height: 925px; cursor: inherit; margin-left: 1px; margin-right: 0px; margin-top: 0px;"></canvas>
Hide result
0

Source: https://habr.com/ru/post/1692091/


All Articles