【发布时间】:2018-05-31 10:38:32
【问题描述】:
我的 JS 代码:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var mouse = {x:0,y:0}
const times = [];
let fps;
function refreshLoop() {
window.requestAnimationFrame(() => {
const now = performance.now();
while (times.length > 0 && times[0] <= now - 1000) {
times.shift();
}
times.push(now);
fps = times.length;
refreshLoop();
});
}
refreshLoop();
function draw() {
ctx.fillStyle = "black"
ctx.fillRect(0, 0, c.width, c.height);
ctx.strokeStyle = "white"
ctx.beginPath();
var e = window.event;
ctx.arc(mouse.x, mouse.y, 40, 0, 2*Math.PI);
ctx.stroke();
ctx.font = "30px Comic Sans MS";
ctx.fillStyle = "red";
ctx.textAlign = "center";
ctx.fillText(fps, c.width/2, c.height/2);
}
setInterval(draw, 0);
document.addEventListener('mousemove', function(event){
mouse = { x: event.clientX, y: event.clientY }
})
我的 HTML 只是画布声明。
据我了解,setinterval(x, 0) 应该尽可能快地运行,但它永远不会超过 60fps。我正在尝试达到 240+ fps 以减少输入延迟。
【问题讨论】:
-
你看过这个答案,有帮助吗?:stackoverflow.com/questions/32947439/… 我对 HTML 画布不太熟悉,但我的游戏经验会让我认为提高 FPS 不会减少输入延迟(我可能是错的)。我还可以想象尝试让浏览器以更高的 FPS 渲染会影响性能。在 Game Dev 堆栈交换中寻找答案可能也值得一看。
-
您希望渲染速度超过 60 fps 的浏览器是什么?
-
@GingerSquirrel 不,我没有看到。谢谢,我去看看。
-
@RogerC Chrome 和 Firefox 桌面版。
-
Here 是一个有用的视频,介绍了
setTimeout和requestAnimationFrame之间的区别以及何时使用它们。
标签: javascript canvas low-latency vsync