javascript - 如何让这个 HTML5 canvas 脚本全屏显示?

标签 javascript html css

我在尝试将 Canvas 设置为填满整个窗口时遇到问题。目前设置为 500x500..我是编码新手,非常感谢任何帮助!谢谢。

代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>Starfield effect done in HTML 5</title>
    <script>
        window.onload = function() {

            /* --- config start --- */

            var starfieldCanvasId     = "starfieldCanvas", // id of the canvas to use
                framerate             = 60,                // frames per second this animation runs at (this is not exact)
                numberOfStarsModifier = 0.15,               // Number of stars, higher numbers have performance impact
                flightSpeed           = 0.003;              // speed of the flight, the higher this number the faster

            /* ---- config end ---- */

            var canvas        = document.getElementById(starfieldCanvasId),
                context       = canvas.getContext("2d"),
                width         = canvas.width,
                height        = canvas.height,
                numberOfStars = width * height / 1000 * numberOfStarsModifier,
                dirX          = width / 2,
                dirY          = height / 2,
                stars         = [],
                TWO_PI        = Math.PI * 2;

            // initialize starfield
            for(var x = 0; x < numberOfStars; x++) {
                stars[x] = {
                    x: range(0, width),
                    y: range(0, height),
                    size: range(0, 1)
                };
            }

            // when mouse moves over canvas change middle point of animation
            canvas.onmousemove = function(event) {
                dirX = event.offsetX,
                dirY = event.offsetY;
            }

            // start tick at specified fps
            window.setInterval(tick, Math.floor(1000 / framerate));

            // main routine
            function tick() {
                var oldX,
                    oldY;

                // reset canvas for next frame
                context.clearRect(0, 0, width, height);

                for(var x = 0; x < numberOfStars; x++) {
                    // save old status
                    oldX = stars[x].x;
                    oldY = stars[x].y;

                    // calculate changes to star
                    stars[x].x += (stars[x].x - dirX) * stars[x].size * flightSpeed;
                    stars[x].y += (stars[x].y - dirY) * stars[x].size * flightSpeed;
                    stars[x].size += flightSpeed;

                    // if star is out of bounds, reset
                    if(stars[x].x < 0 || stars[x].x > width || stars[x].y < 0 || stars[x].y > height) {
                        stars[x] = {
                            x: range(0, width),
                            y: range(0, height),
                            size: 0
                        };
                    }

                    // draw star
                    context.strokeStyle = "rgba(255, 255, 255, " + Math.min(stars[x].size, 1) + ")";
                    context.lineWidth = stars[x].size;
                    context.beginPath();
                    context.moveTo(oldX, oldY);
                    context.lineTo(stars[x].x, stars[x].y);
                    context.stroke();
                }
            }

            // get a random number inside a range
            function range(start, end) {
                return Math.random() * (end - start) + start;
            }
        };
    </script>
</head>
<body style="background:#000;">
    <canvas width="500" height="500" id="starfieldCanvas"></canvas>
</body>
</html>

原始代码和来源:https://www.timewasters-place.com/starfield-animation-done-in-html-5/

最佳答案

我假设您的意思是您希望脚本自动确定屏幕大小,然后将 Canvas 设置为全屏而不是当前硬编码的 500 x 500。

您可以使用以下代码以编程方式确定视口(viewport)大小,其中宽度和高度分别如下所示。 (Source) :

var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;

var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;

然后您可以按如下方式设置 Canvas 的尺寸:

canvas.width  = width;
canvas.height = height;

因此,这将是您案例中的完整代码:

<!DOCTYPE html>
<html>
<head>
    <title>Starfield effect done in HTML 5</title>
    <script>
        window.onload = function() {

            /* --- config start --- */

            var starfieldCanvasId     = "starfieldCanvas", // id of the canvas to use
                framerate             = 60,                // frames per second this animation runs at (this is not exact)
                numberOfStarsModifier = 0.15,               // Number of stars, higher numbers have performance impact
                flightSpeed           = 0.003;              // speed of the flight, the higher this number the faster

            var width = window.innerWidth
            || document.documentElement.clientWidth
            || document.body.clientWidth;

            var height = window.innerHeight
            || document.documentElement.clientHeight
            || document.body.clientHeight;

            /* ---- config end ---- */

            var canvas        = document.getElementById(starfieldCanvasId),
                context       = canvas.getContext("2d"),
                stars         = [],
                TWO_PI        = Math.PI * 2;

                canvas.width  = width;
                canvas.height = height;

                numberOfStars = width * height / 1000 * numberOfStarsModifier;
                dirX          = width / 2;
                dirY          = height / 2;

            // initialize starfield
            for(var x = 0; x < numberOfStars; x++) {
                stars[x] = {
                    x: range(0, width),
                    y: range(0, height),
                    size: range(0, 1)
                };
            }

            // when mouse moves over canvas change middle point of animation
            canvas.onmousemove = function(event) {
                dirX = event.offsetX,
                dirY = event.offsetY;
            }

            // start tick at specified fps
            window.setInterval(tick, Math.floor(1000 / framerate));

            // main routine
            function tick() {
                var oldX,
                    oldY;

                // reset canvas for next frame
                context.clearRect(0, 0, width, height);

                for(var x = 0; x < numberOfStars; x++) {
                    // save old status
                    oldX = stars[x].x;
                    oldY = stars[x].y;

                    // calculate changes to star
                    stars[x].x += (stars[x].x - dirX) * stars[x].size * flightSpeed;
                    stars[x].y += (stars[x].y - dirY) * stars[x].size * flightSpeed;
                    stars[x].size += flightSpeed;

                    // if star is out of bounds, reset
                    if(stars[x].x < 0 || stars[x].x > width || stars[x].y < 0 || stars[x].y > height) {
                        stars[x] = {
                            x: range(0, width),
                            y: range(0, height),
                            size: 0
                        };
                    }

                    // draw star
                    context.strokeStyle = "rgba(255, 255, 255, " + Math.min(stars[x].size, 1) + ")";
                    context.lineWidth = stars[x].size;
                    context.beginPath();
                    context.moveTo(oldX, oldY);
                    context.lineTo(stars[x].x, stars[x].y);
                    context.stroke();
                }
            }

            // get a random number inside a range
            function range(start, end) {
                return Math.random() * (end - start) + start;
            }
        };
    </script>
</head>
<body style="background:#000;">
    <canvas id="starfieldCanvas"></canvas>
</body>
</html>

我还在 Fiddle 上对此进行了测试。这是链接:Fiddle

希望对您有所帮助!让我知道它是否有效。

关于javascript - 如何让这个 HTML5 canvas 脚本全屏显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43162740/

相关文章:

javascript - 如何显示我的 GridX

javascript - 有没有办法让这个幻灯片自动移动?

javascript - 为什么期望 Flash 可用/启用是可以接受的,但 JavaScript 不是?

html - Bootstrap 导航栏 : brand on left - login on right - collapsible menu on center

javascript - 来自多个来源的 jQuery 动画 - 影响多个对象

javascript - 如何在谷歌脚本中将数据添加/附加到多维数组

javascript - 如何从 Bootstrap 轮播中删除白色背景?

html - CSS 溢出规则包括对象边框?

html - 使用 bootstrap 在文本末尾对齐一行中的列

css - IE 中的 Calc 函数舍入值并使元素推送到下一行