从相位画布获取高度和宽度

Kva*_*sir 8 html javascript css jquery phaser-framework

所以我使用phaser.io,但我想打印一个数组,所以我在我的页面html中创建它.

要设置数组的高度和宽度,我需要知道画布的高度和宽度.

所以我应该这样做:

$('canvas').height();
Run Code Online (Sandbox Code Playgroud)

任务完成.

但是没有一个陷阱,我的画布看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

每次它返回高度值而不是样式中的值.

所以我尝试其他方式:

$('canvas').css('height');
$('canvas').[0].style.height;
$('canvas')[0].style.cssText;
Run Code Online (Sandbox Code Playgroud)

但我总是得到错误的结果

什么是超级奇怪的是,当我这样做:

console.log($('canvas')[0].style)
Run Code Online (Sandbox Code Playgroud)

我在元素'height'和'cssText'中看到了我想要的结果.我把结果的一部分放在这里:

 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"
    ...
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我为什么?并给我一个解决方案也可以很好.

=======编辑=======

当我执行$('canvas').height()点击一个元素时,我得到一些新闻.Actualy我可以处理它但我仍然不明白为什么当我这样做:

console.log($('canvas')[0].style.cssText);
console.log($('canvas').height());
Run Code Online (Sandbox Code Playgroud)

只是一个接一个地得到不同的结果.当然是加载时间的问题......但对我来说仍然听起来不错.

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');
}
Run Code Online (Sandbox Code Playgroud)
<!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>
Run Code Online (Sandbox Code Playgroud)

cod*_*ode 1

在 Phaser (v3) 中,您可以访问game.canvas,这是 Phaser 运行的画布(元素),因此:

const game = new Phaser.Game({ ... });
console.log(game.canvas.width, game.canvas.height);
Run Code Online (Sandbox Code Playgroud)