Chrome的全屏画布?

时间:2013-09-20 23:08:56

标签: css html5 google-chrome canvas

我正在尝试在Google Chrome上制作全屏画布。

<style>
#canvas:-webkit-full-screen {
    height:100%;
    width: 100%;
}    
</style>
<button onclick="fullScreen()">Full Screen</button>
<canvas id="canvas"></canvas>
<script>
    function fullScreen() {
        var c = document.getElementById("canvas");
        c.webkitRequestFullScreen();
    }
</script>

此代码使画布全屏显示,但只有高度为全屏。

2 个答案:

答案 0 :(得分:2)

尝试使用带有canvas元素的fixed的CSS规则position

position:fixed;
left:0;
top:0;
width:100%;
height:100%;

您可能需要设置宽度/高度为htmlbody:100%。

如果你想避免缩放,你需要直接设置画布元素的宽度和高度(使用window.innerHeight.innerWidth)。

答案 1 :(得分:-1)

我以这种方式做全屏画布:

更多详细信息,请参见here

var canvas = document.getElementById("mycanvas");
var ctx = canvas.getContext("2d");

function refreshCanvas() {//refresh size canvas function
  document.body.style.width = window.innerWidth+'px';
  document.body.style.height = window.innerHeight+'px';
  canvas.setAttribute('width', window.innerWidth+'px');
  canvas.setAttribute('height', window.innerHeight+'px');
}
window.addEventListener('resize', refreshCanvas); //refresh when resize window
window.addEventListener('load', refreshCanvas); //refresh when load window
body {
  margin: 0px;
}
#mycanvas {
  background-image: url('https://gmer.io/resource/community/backgroundCanvas.png');
}
<canvas id="mycanvas"></canvas>