【问题标题】:Make JavaScript Canvas Game Responsive with CSS3使用 CSS3 使 JavaScript 画布游戏响应式
【发布时间】:2016-10-27 20:09:34
【问题描述】:

我有一个 HTML 画布游戏,我想通过使用 CSS3 设置其宽度和高度来使其具有响应性。

JS画布游戏代码为:

    <div id="main">
        <div class="game-box">
             <div class="game-container">
                  <div id="phaser-div">

                  </div>
             </div><!-- end game-container -->
        </div><!-- end game-box -->



    <script src="js/jquery-1.11.2.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/phaser.min.js"></script>
    <script>
        var game = new Phaser.Game(832, 508, Phaser.WEBGL, 'phaser-div', { preload: preload, create: create, update: update });

var background;
var filter;

function preload() {

    var urlBase = location.href.substring(0, location.href.lastIndexOf("/") + 1);
    game.load.image('phaser', urlBase + 'games/game001/game001-logo.png');
    game.load.script('filter', urlBase + 'games/game001/marble.js');

    this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; this.game.scale.setShowAll(); window.addEventListener('resize', function () { this.game.scale.refresh(); }); this.game.scale.refresh();
}

function create() {

    var logo = game.add.sprite(game.world.centerX, game.world.centerY, 'phaser');
    logo.anchor.setTo(0.5, 0.5);

    background = game.add.sprite(0, 0);
    background.width = 832;
    background.height = 508;

    filter = game.add.filter('Marble', 832, 508);
    filter.alpha = 0.2;

    //  The following properties are available (shown at default values)

    //  filter.speed = 10.0;
    //  filter.intensity = 0.30;

    background.filters = [filter];

}

function update() {

    filter.update();

}
    </script>

直播链接:
http://revolutionarydeveloper.com/project-games/index.html

我尝试设置类似 #phaser-div canvas {width: 100%; height: 470px;} 但它似乎不起作用。如何使用 CSS3 使其具有响应性并使用媒体查询修改宽度和高度?

非常感谢

注意:本游戏使用 Phaser.JS。包括其链接以防万一。
https://cdn.jsdelivr.net/phaser/2.6.2/phaser.js

【问题讨论】:

    标签: javascript jquery html css canvas


    【解决方案1】:

    phaser 有自己的代码来使游戏响应

    Phaser.ScaleManager.SHOW_ALL;
    

    看http://phaser.io/news/2015/02/responsive-games-basics

    【讨论】:

    • 这个不错,但是有问题。我必须刷新页面才能使移相器正确缩放,即它在页面加载时响应。但是如果我调整页面大小,它就不能正确缩放。每次页面调整大小时,有没有办法让我更新它?如果没有,我可以使用 CSS 控制宽度和高度吗?
    • 您必须对其进行编码,例如将调整大小事件附加到主体并根据调整大小的属性进行决定,因为当涉及到移相器时,它会频繁绘制画布,因此它不能像引导程序(移相器画布)那样响应不仅仅是元素,我们可以说它是动画绘制)。
    • 感谢安舒的解释。如果您能指导我如何编写移相器画布的代码以根据页面宽度调整大小,我将不胜感激,因为我的 JavaScript 技能水平非常中等。
    • 我已经在代码中有这样的东西:this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; this.game.scale.setShowAll(); window.addEventListener('resize', function () { this.game.scale.refresh(); }); this.game.scale.refresh();这应该在调整窗口大小时起作用,但显然它只在刷新时起作用。
    • this.time.events.add(200, function() { MyGame.calculateDimensions(); this.scale.setGameSize(MyGame.canvasWidth, MyGame.canvasHeight); }, this);跨度>
    【解决方案2】:
    Phaser method
    
    this.time.events.add(200, function() { 
       MyGame.calculateDimensions();            
       this.scale.setGameSize(MyGame.canvasWidth, MyGame.canvasHeight);        
    }, this);
    
    
    jQuery method
    
    $(window).resize(function() { resizeGame(); } );
        function resizeGame() {
    
        var innerWidth = window.innerWidth;
        var innerHeight = window.innerHeight;
        var gameRatio = innerWidth/innerHeight;
    
        game.width = Math.ceil(320*gameRatio);
        game.height = 320;
        game.stage.bounds.width = Math.ceil(320*gameRatio);
        game.stage.bounds.height = 320;
        game.scale.refresh();
    
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多