由于我忘記添加示例,所以進行了編輯
自從我開始在這個游戲上工作,畫布周圍有一個白色的邊界,我不明白它的原因,有人說這是因為clearRect函數,但我試過了,它不起作用。
示例:
這是代碼:
注意:我正在工作的畫布叫做& quot游戲畫布& quot
- HTML -(部分)
<body>
<div class="container">
<canvas id="gameCanvas"></canvas>
<!-- OUTER ELEMENTS -->
<div id="canvasSpace">
</div>
</div>
<!-- SCRIPTS -->
<script type="module" src="./game/Main.js"></script>
</body>
- CSS -
身體
body{
overflow: hidden;
border: 0;
margin: 0;
padding: 0;
left: 0;
right: 0;
}
容器
.container{
position: absolute;
width: 100%;
height: 100%;
border: 0;
margin: 0;
padding: 0;
left: 0;
right: 0;
display: flex;
justify-content: center;
align-items: center;
}
游戲畫布
#gameCanvas{
position: relative;
border: 0;
margin: 0;
padding: 0;
left: 0;
right: 0;
image-rendering: optimizeSpeed;
z-index: 1;
}
- JavaScript -(只是為了預防起見)
function draw() //^ draw() has the purpose to ONLY draw (and also call requestAnimationFrame() to mantain the loop) on the canvas [Xavier]
{
context.clearRect(0, 0, canvas.width, canvas.height); //? Clears the whole canvas space in order to print new elements over it [Xavier]
game.draw();
}
提前感謝。