我正在尝试使用 Windows 8 / WinJS 创建 HTML5 画布。这是我所拥有的:
var body = document.getElementById("body");
var canvas = document.createElement("canvas");
canvas.id = "myCanvas";
body.appendChild(canvas);
var canvasContext = canvas.getContext("2d");
canvas.width = document.width;
canvas.height = document.height;
canvasContext.fillStyle = "#f00";
canvasContext.fillRect(canvas.width - 100, canvas.height - 50, 100, 50);
console.log("canvas width " + canvas.width + ", height " + canvas.height);
其中大部分是直接来自教程,但对于我见过的所有教程,宽度和高度都设置为硬编码数字,例如:
canvas.width = 800;
canvas.height = 600;
诚然,这确实有效,但我希望画布与屏幕一样大,无论分辨率如何。我如何实现这一目标?