【发布时间】:2011-04-12 01:14:38
【问题描述】:
如何使画布的页面宽度和高度为 100%?
【问题讨论】:
标签: javascript html canvas
如何使画布的页面宽度和高度为 100%?
【问题讨论】:
标签: javascript html canvas
我在这里工作:Are Google's Bouncing Balls HTML5? 使用以下 CSS:
* { margin: 0; padding: 0;}
body, html { height:100%; }
#c {
position:absolute;
width:100%;
height:100%;
}
其中 #c 是画布元素的 id。
【讨论】:
你可以在没有 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> 不设置为 display:block 时会产生滚动条。
详情:http://browser.colla.me/show/canvas_cannot_have_exact_size_to_fullscreen
【讨论】:
您可以通过编程方式设置画布宽度 + 高度:
// 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>
来自Force canvas element in html to take up the entire window?
【讨论】: