簡體   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?

我想做一個 TIC TAC TOE,我對 js 很陌生。 我想將 x 和 0 繪制到畫布上。 1. 如何使高度等於畫布的寬度? 2. 縮放畫布以在例如手機上使用它並具有相同體驗的最佳方法是什么?

您可以制作全屏畫布並使用它來顯示您的代碼。
這是一個樣板,可以幫助您入門:

HTML

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

CSS

* {
  margin: 0;
}

canvas {
  display: block;
}

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