简体   繁体   English

使用 css 或 js 将 Canvas 缩放到屏幕的最佳方法是什么? 以及如何使画布的宽度等于高度?

[英]What is the best way to scale an Canvas with css or js to the screen? And how do I make the width of the canvas equal to the height?

I want to make an TIC TAC TOE and im pretty new to js.我想做一个 TIC TAC TOE,我对 js 很陌生。 I want to draw the x's and 0's to the canvas.我想将 x 和 0 绘制到画布上。 1. How do I make the height equal to the width of the canvas? 1. 如何使高度等于画布的宽度? 2. What is the best way to scale the canvas to use this for example also on phones and have the same experience? 2. 缩放画布以在例如手机上使用它并具有相同体验的最佳方法是什么?

You can make a full-screen canvas and use it to display your code.您可以制作全屏画布并使用它来显示您的代码。
Here is a boilerplate to get you started:这是一个样板,可以帮助您入门:

HTML HTML

<canvas id="canvas"></canvas>

CSS CSS

* {
  margin: 0;
}

canvas {
  display: block;
}

JS JS

window.onload = function() {
  var canvas = document.getElementById("canvas"),
      context = canvas.getContext("2d"),
      width = canvas.width = window.innerWidth,
      height = canvas.height = window.innerHeight;

  context.fillRect(0, 0, width, height);
};

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

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