сделать HTML5 холст полную страницу с JQuery - PullRequest
0 голосов
/ 31 января 2019

Я пытался сделать html canvas полной страницей.В моем коде это предупреждает, что размер холста = размер окна, но он не показывает правильный вывод.

Вот HTML ->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <canvas></canvas>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="main.js"></script>
</body>
</html>

CSS ->

body
{
    margin: 0;
}
canvas
{
    /*border: 1px solid black;*/
    background: green;
}

и jquery ->

$(document).ready(function(){
    $.fn.showMSG = function(){
        var canvas = $("canvas");

        var canvasWidth = canvas.width();
        var canvasHeight = canvas.height();
        var windowWidth = $(window).width();
        var windowHeight = $(window).height();

        canvasWidth = windowWidth;
        canvasHeight = windowHeight;

        var txt = "";
        txt += "Canvas width/height: " + canvasWidth;
        txt += "x" + canvasHeight + "\n";
        txt += "Window width/height: " + windowWidth;
        txt += "x" + windowHeight;
        alert(txt);
    }

    $(window).ready(function(){
        $.fn.showMSG();
    });
});

1 Ответ

0 голосов
/ 31 января 2019

Мне удалось исправить ваш код.Позволь мне объяснить.Ооо, так как width() и height() мне показались похожими на функции set / get, я попытался сделать следующее:

canvas.width(windowWidth);
canvas.height(windowHeight);

вместо (что вы сделали):

canvasWidth = windowWidth;
canvasHeight = windowHeight;

И это сработало.Попробуйте!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...