2 回答
TA贡献2036条经验 获得超8个赞
2种方法 :
1)用相机偏移调整要绘制的对象的位置:
deltaX=object.x-cameraX
deltaY=object.y-cameraY
if(deltaX + object.width > 0
&& deltaX - object.width < cameraWidth
&&deltaY + object.height > 0
&& deltaY - object.height < cameraHeight){
ctx.drawImage(character,deltaX,deltaY);
}
2)有两个上下文并将一个打印到另一个
ctxCamera.drawImage(ctx,cameraX,cameraY);
TA贡献1789条经验 获得超8个赞
我不知道我是否为时已晚,但是您可以使用 div 标签包含画布标签。
HTML:
<div id = "viewport">
<canvas id="canvas"></canvas>
</div>
CSS:
#viewport{
overflow: hidden; //so you can't see outside of the div
width: /*insert desired amount*/px;
height: /*insert desired amount*/px;
}
Javascript:
function scrollCamera(camx, camy){
var viewport = document.getElementById('viewport');
viewport.scrollTop = camy;
viewport.scrollLeft = camx;
}
scrollCamera(x-/*desired amount*/, y-/*desired amount*/); //makes the camera follow the player
添加回答
举报