Sunday 13 December 2020

Canvas device orientation scaling

I made a racetrack game. I am trying to make it scalable on mobile devices.

I achieved to make it look ok when device orientation is normal (vertical) :

![enter image description here

But when I rotate the device to a horizontal view this happens : (how to zoom it out a bit )?

enter image description here

What would you recommend doing? I used display: none to hide the images. I would really appreciate any help.

Edit: I've managed to do this but the view is still too much zoomed, any ideas?

const width = 800;
const height = 600;
const pixelRatio = window.devicePixelRatio || 1;
canvas.width = width * pixelRatio;
canvas.height = height * pixelRatio;

canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;

// for sprites scaled up to retina resolution
canvas.mozImageSmoothingEnabled = false;
canvas.imageSmoothingEnabled = false;

c.scale(pixelRatio, pixelRatio);
<!DOCTYPE html>
<html lang="pl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, height=device-height">

    <!-- <meta name="viewport" content="width=device-width, initial-scale=1"> -->
    <title>Canvas Story</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        canvas {

            display: block;
        }
        body {
            margin: 0;
        } 
        #container {
            margin: 0 auto;
            width: 800px;
            height: 600px;
            overflow: hidden;
            position: relative;
            border: 1px solid #000;
            border-radius: 10px;
            margin-top: 2px;
        } 
    </style>
</head>

<body>
    <br/>
    
    <img id="obstacle" src="obstacle.png" style="display: none;" />
        <img id="bonus" src="bonus.png" style="display: none;" />
        <img id="bullet" src="bullet.png" style="display: none;" />
        <img id="car" src="car.png" style="display: none;" />

    <div id="container">
        <canvas id="ltpcanvas"></canvas>
    </div>

    <div class="circleBase" id="rotateMode" style="margin:0 auto;">
        <button id="left" onmousedown="leftKeyPressed()" onmouseup="leftKeyReleased()" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-arrow-left"></span></button>
        <button id="right" onmousedown="rightKeyPressed()" onmouseup="rightKeyReleased()" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-arrow-right"></span></button>
        <button id="middle" onclick="bulletsPush()" class="btn btn-default btn-sm"><span class="glyphicon glyphicon glyphicon-record"></span></button>
        <button id="up" onmousedown="upKeyPressed()" onmouseup="upKeyReleased()" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-arrow-up"></span></button>
        <button id="down" onmousedown="downKeyPressed()" onmouseup="downKeyReleased()" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-arrow-down"></span></button>
    </div>
    
    
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="css.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="canvas.js"></script>

    

</body>

</html>


from Canvas device orientation scaling

No comments:

Post a Comment