如何使画布(canvas)的宽度和高度都达到页面的100%?
如何使画布(canvas)的宽度和高度都达到页面的100%?
这里我已经能正常工作了:Google的弹跳球是HTML5吗?,使用以下CSS:
* { margin: 0; padding: 0;}
body, html { height:100%; }
#c {
position:absolute;
width:100%;
height:100%;
}
#c 是画布元素的id。
width
为100%
和使用window.innerWidth
等其他答案时得到不同的结果,尽管这两个“值”具有相同的含义?这是怎么可能的?也许所有的答案都需要重新绘制画布图像,而不仅仅是这个答案? - root您可以不使用jQuery使用这些代码
var dimension = [document.documentElement.clientWidth, document.documentElement.clientHeight];
var c = document.getElementById("canvas");
c.width = dimension[0];
c.height = dimension[1];
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
也许那很容易?
<canvas>
标签有关。<canvas>
将导致滚动条出现。// Using jQuery to get window width + height.
canvasObject.width = $(window).width();
canvasObject.height = $(window).height();
我已经测试过了,只要在调整大小后重新绘制画布上的内容,就不会改变缩放比例。
var c = document.getElementById('can');
var ctx = canvas.getContext('2d');
ctx.rect(0, 0, canvas.width, canvas.height);
// add linear gradient
var g = ctx.createLinearGradient(0, 0, c.width, c.height);
// light blue color
g.addColorStop(0, '#8ED6FF');
// dark blue color
g.addColorStop(1, '#004CB3');
context.fillStyle = g;
context.fill();
<script>
var c = document.getElementById('can');
var ctx = canvas.getContext('2d');
ctx.rect(0, 0, canvas.width, canvas.height);
// add linear gradient
var g = ctx.createLinearGradient(0, 0, c.width, c.height);
// light blue color
g.addColorStop(0, '#8ED6FF');
// dark blue color
g.addColorStop(1, '#004CB3');
context.fillStyle = g;
context.fill();
</scrip
html,body
{
height:98.0%;
width:99.5%;
}
canvas
{
display:block;
width:100%;
height:100%;
}
<html>
<body>
<canvas id="can"></canvas>
</body>
</html>
这对你有效吗?
<html>
<body style="height: 100%; margin: 0;">
<canvas style="width: 100%; height: 100%;"></canvas>
</body>
</html>
如何让HTML中的画布元素占据整个窗口?