Skip to content

Axmol Web Game can be done window size? #2928

@maitryeuc-stack

Description

@maitryeuc-stack

basically i am converted cocos2dx project to axmol project.....conversion is done,,,but when i am running that project in web...its not resizing to window screen.,,,,,it comes in fixed size,,,,i need to rescale that canvas,.,.,.that is not happening,,,,,pasting you my code,,,,,please do check,,,and give opossible solutions,,,,

<!doctype html>

<title>My Axmol Web Game</title>

<style>
    html, body {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
        background: #000;
    }

    #canvas {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        display: block;
        background: #000;
    }
</style>
<canvas class="emscripten" id="canvas" tabindex="-1"
    oncontextmenu="event.preventDefault()"></canvas>

<script>
    // MAIN RESIZE HANDLER (Correct for Axmol Web)
    function resizeGame() {
        const canvas = Module.canvas;

        // Correct size inside iframe
        const w = document.documentElement.clientWidth;
        const h = document.documentElement.clientHeight;

        // Resize HTML canvas
        canvas.width = w;
        canvas.height = h;
        canvas.style.width = w + "px";
        canvas.style.height = h + "px";

        // Internal emscripten GL update
        if (typeof Browser !== 'undefined' && Browser.updateCanvasDimensions) {
            Browser.updateCanvasDimensions(canvas, true, true);
        }

        // Axmol's viewport update
        if (Module.setCanvasSize) {
            Module.setCanvasSize(w, h);
        }
    }

    // Module object REQUIRED by Axmol/Emscripten
    var Module = {
        canvas: document.getElementById("canvas"),

        // Called when WASM is ready
        onRuntimeInitialized() {
            resizeGame();
        },

        // Axmol calls this on ANY size change (important)
        onResize(width, height) {
            resizeGame();
        }
    };

    // Listen for browser/iframe changes
    window.addEventListener("load", resizeGame);
    window.addEventListener("resize", resizeGame);
    window.addEventListener("orientationchange", resizeGame);
</script>

<!-- Load Axmol engine -->
<script async src="MyNewWebGame.js"></script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions