简体   繁体   English

当窗口更改大小时,缩放全屏画布以适合内容

[英]Scale full screen canvas to fit content as window changes size

Basically, I have a canvas that draws a grid. 基本上,我有一个绘制网格的画布。 When the window changes sizes, it refreshes the grid and scene to fill the entire screen with a grid. 当窗口更改大小时,它将刷新网格和场景以用网格填充整个屏幕。

When I resize my browser window to a smaller size, I would like the grid and everything inside the canvas to shrink with it, while keeping the grid boxes a square shape. 当我将浏览器窗口调整为较小的尺寸时,我希望网格和画布中的所有内容都随其收缩,同时保持网格框为正方形。 Websites like Agar.io use this with their canvas. Agar.io这样的网站都将其与画布一起使用。

 var canvas = document.getElementById("mainCanvas"); var ctx = canvas.getContext("2d"); function resize() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } function createMap() { var gridOptions = { majorLines: { separation: 30, color: '#e8e8e8' } }; drawGridLines(canvas, gridOptions.majorLines); return; } function drawGridLines(canvas, lineOptions) { var iWidth = canvas.width; var iHeight = canvas.height; ctx.strokeStyle = lineOptions.color; ctx.strokeWidth = 1; ctx.beginPath(); var iCount = null; var i = null; var x = null; var y = null; iCount = Math.floor(iWidth / lineOptions.separation); for (i = 1; i <= iCount; i++) { x = (i * lineOptions.separation); ctx.moveTo(x, 0); ctx.lineTo(x, iHeight); ctx.stroke(); } iCount = Math.floor(iHeight / lineOptions.separation); for (i = 1; i <= iCount; i++) { y = (i * lineOptions.separation); ctx.moveTo(0, y); ctx.lineTo(iWidth, y); ctx.stroke(); } ctx.closePath(); return; } function refresh() { resize(); createMap(); } window.onresize = function() { refresh(); } refresh(); 
 body, html { align-content: center; overflow: hidden; border: 0; margin: 0; padding: 0; } #mainCanvas { top: 0; left: 0; position: absolute; overflow: hidden; border: 0; margin: 0; padding: 0; } 
 <canvas id="mainCanvas"></canvas> 

In other words, the view of the canvas is like a bird's eye, and if there are 10 grid boxes from the center to the left side of the screen. 换句话说,画布的视图就像鸟瞰图,并且从屏幕的中心到左侧有10个网格框。 When the window is enlarged completely, the screen would keep that same number or grid boxes from the center to the left side as it would when the browser window is resized to a smaller state. 当窗口完全放大时,屏幕将保持与从浏览器窗口调整到较小状态时相同的数字或网格框(从中心到左侧)。 (Of course, depending on the screen ratio, it could be 10 grid boxes more or less, but roughly around there) (当然,取决于屏幕比例,它可能或多或少是10个网格框,但大概在附近)

I tried setting the canvas size with CSS to 100% on the width and height, but that caused the grid squares to become blurry rectangles. 我尝试将CS​​S的画布大小设置为宽度和高度的100% ,但这导致网格正方形变成模糊的矩形。

What can I add to cause the elements in the canvas to shrink and grow with the windows size? 我可以添加什么来使画布中的元素随着窗口大小而缩小和增长?

Here is a sample where the elements in the canvas shrink and grow with the windows size: 这是一个示例,其中画布中的元素随窗口大小而收缩和增长:

<style>body{margin:0}</style>
<canvas id="mainCanvas"></canvas>


<script>
var canvas = document.getElementById("mainCanvas");
var ctx = canvas.getContext("2d");

function resize() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    var gridOptions = {
        majorLines: {
        separation: 20,
        color: '#e8e8e8'
        }
    };
    drawGridLines(canvas, gridOptions.majorLines);
}

function drawGridLines(canvas, lineOptions) {
    ctx.strokeStyle = lineOptions.color;
    ctx.strokeWidth = 1;
    ctx.beginPath();

    var size = Math.floor(canvas.width / lineOptions.separation);
    for (var i = 1; i <= lineOptions.separation; i++) {
        var x = (i * size);
        ctx.moveTo(x, 0);
        ctx.lineTo(x, canvas.height);
        ctx.stroke();
    }
    for (var i = 1; i <= Math.floor(canvas.height / size); i++) {
        var y = (i * size);
        ctx.moveTo(0, y);
        ctx.lineTo(canvas.width, y);
        ctx.stroke();
    }
    ctx.closePath();
}

window.onresize = function() { resize(); }
resize();
</script>

Here is a live sample: 这是一个实时示例:

https://raw.githack.com/heldersepu/hs-scripts/master/HTML/canvasResize2.html https://raw.githack.com/heldersepu/hs-scripts/master/HTML/canvasResize2.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM