调整合适的画布尺寸(游戏)

1、处理尺寸调整

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
        <title>处理尺寸调整</title>
    </head>
    <style>
        body{margin: 0 auto;overflow-x: hidden;}
        #container{margin:0 auto;width:480px;height: 100%;}
        canvas{background-color:#04be02}
    </style>
    <script src="../build/jquery.min.js"></script>
    <script src="vconsole.min.js"></script>

    <body>
        <div id="container">
            <canvas id="game" width="480" height="480"></canvas>
        </div>
    </body>
    <script>         

        //等待document.ready回调
        $(function(){
            window.scrollTo(0,1);//去除地址栏

            var maxWidth = 480;
            var maxHeight = 440;
            var initalWidth = $("#game").attr("width");
            var initalHeight = $("#game").attr("height");
            var handleResize = function(){
                //获得窗口宽度和高度
                var w = window.innerWidth,h = window.innerHeight,newDim;

                console.log(w +"---" + h + "---" + newDim +"---" + initalWidth + "---" + initalHeight + "---" + (w <= maxWidth));

                if(w <= maxWidth){
                    newDim = {
                        width:Math.min(w,maxWidth),
                        height:Math.min(h,maxHeight)    
                    };
                    $("#game").css({"position":"absolute",left:0,top:0,"z-index":10000});
                    $("#container").css("width","auto");
                }else{
                    //
                    newDim = {
                        width:initalWidth,
                        height:initalHeight
                    };
                    $("#game").css("position","relative");
                    $("#container").css("width",maxWidth);
                }
                $("#game").attr(newDim);//让游戏知道页面大小
            };

            $(window).bind("resize",handleResize)
            handleResize();

            //阻止防止滚动、缩放。
            $(document).on("touchmove",function(event){
                event.preventDefault();
            });

        });

    </script>

</html>


2、去除地址栏

可以使用另一个技巧来获取更多一点的页面实际使用面积,那就是去除IOS设备上的地址栏,可以使用页面加载完之后稍稍滚动页面的招术来实现。

利用:

window.scrollTo(0,1)//去除地址栏

PS:只有在页面内容长于一整页时,这一招才奏效;那么,就出现问题了,地址栏的移除还会影响所获取的页面的 innerHeight。你希望画布的大小调整成占据整个页面,解决这一问题,可以简单的把容器元素的高度设置成一个比没有没有地址栏情况下的最终高度还要大得已知值,然后滚动窗口,来重新计算 innerHeight

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
        <title>处理尺寸调整</title>
    </head>
    <style>
        body{margin: 0 auto;overflow-x: hidden;}
        #container{margin:0 auto;width:480px;height: 100%;}
        canvas{background-color:#04be02;}
    </style>
    <script src="../build/jquery.min.js"></script>
    <script src="vconsole.min.js"></script>

    <body>
        <div id="container">
            <canvas id="game" width="480" height="480"></canvas>
        </div>
    </body>
    <script> 
        //去除地址栏,等待document.ready回调
        $(function(){

            var maxWidth = 480;
            var maxHeight = 480;
            var initalWidth = $("#game").attr("width");
            var initalHeight = $("#game").attr("height");
            var touchDevice = !!('ontouchstart' in document);

            var handleResize = function(){
                //获得窗口宽度和高度
                var w = window.innerWidth,h = window.innerHeight,newDim;

                console.log("w:"+w +"---h:" + h  +"---initalWidth:" + initalWidth + "---initalHeight:" + initalHeight + "---" + (w <= maxWidth));

                //确保内容比页面大
                if(w <= maxWidth && touchDevice){
                    $("#container").css({height : h * 2});
                }

                window.scrollTo(0,1)//去除地址栏

                //得到的高度了,scrollto可能已经改变了innerheight
                h = window.innerHeight;
                if(w <= maxWidth){
                    newDim = {
                        width:Math.min(w,maxWidth),
                        height:Math.min(h,maxHeight)    
                    };
                    $("#game").css({"position":"absolute",left:0,top:0});
                    $("#container").css("width","auto");
                }else{
                    //
                    newDim = {
                        width:initalWidth,
                        height:initalHeight
                    };
                    $("#game").css("position","relative");
                    $("#container").css("width",maxWidth);
                }
                $("#game").attr(newDim);//让游戏知道页面大小

            };
            var resizeEvent = touchDevice ? 'orientationchange' : 'resize';//用来判断是
            $(window).on(resizeEvent,handleResize)

            //阻止防止滚动、缩放。
            $(document).on("touchmove",function(event){
                event.preventDefault();
            }); 
            handleResize();

        });

    </script>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rkatsiteli

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值